-
October 1st, 2016, 07:18 PM
#1
What kind of tree structure is this code creating
Good day, I am new to java and new to this forum but I request a bit of help. I am learning java on my own and I am currently learning binary trees. I have come an algorithm that is giving me a little issue. It is a binary tree code that basically inserts five nodes into a tree structure and then traverses the tree. My problem is that I do not understand what kind of tree structure is being created. Can you help? Here is the code:
Code:
import Prog1Tools.IOTools;
class Node {
Node left;
Node right;
int value;
public Node(int value) {
this.value = value;
}
}
public class GeneralTreeTest {
public static void main(String[] args) {
// build a simple tree add 5 nodes to the tree
Node root = new Node(5);
System.out.println("Tree Example");
System.out.println("Building tree with root value " + root.value);
insert(root, 1);
insert(root, 8);
insert(root, 6);
insert(root, 3);
insert(root, 9);
System.out.println("Traversing tree ");
printOrder(root);
}
public static void insert(Node node, int value) {
if (value < node.value) {
if (node.left != null) {
insert(node.left, value);
} else {
System.out.println(" Inserted " + value + " to left of "
+ node.value);
node.left = new Node(value);
}
} else if (value > node.value) {
if (node.right != null) {
insert(node.right, value);
} else {
System.out.println(" Inserted " + value + " to right of "
+ node.value);
node.right = new Node(value);
}
}
}
public static void printOrder(Node node) {
if (node != null) {
printOrder(node.left);
System.out.println(" Traversed " + node.value);
printOrder(node.right);
}
}
}
-
October 2nd, 2016, 04:45 AM
#2
Re: What kind of tree structure is this code creating
Yes.
I do not understand what kind of tree structure is being created
Sorry, but I'm not sure what you are not understanding as you say the code is for a binary tree?
The structure Node has members for the left and right branches. The insert() recursive function inserts a new value into the tree depending upon whether the value to insert is less than or greater than the value of the node being examined - less than to the left of the node, greater than to the right.
The printOrder() recursive function will display the values in the tree in increasing sorted order - as it first displays the values in the left of the tree to the given node, the value of the node and then the values for the right of the tree for the given node. As this is the order in which the values were stored by insert() the values will be displayed in increasing sorted order.
See also
http://algorithms.tutorialhorizon.co...mplementation/
http://www.cs.cmu.edu/~adamchik/15-1...ees/trees.html
http://javabeat.net/binary-search-tree-traversal-java/
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|