| | |
Binary Tree Help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 91
Reputation:
Solved Threads: 1
Hello every one.
Basically i am trying to construct a binary tree. but there's a problem. When i run the program, there is an error. somewhere in the treeInsert method(). I am having difficulty because the root is not node, it is an integer value. then the left and right subtree is a binary tree. before, i used nodes to construct binary trees.. i can't root.left. if sumone could just look and see what is the problem.
Basically i am trying to construct a binary tree. but there's a problem. When i run the program, there is an error. somewhere in the treeInsert method(). I am having difficulty because the root is not node, it is an integer value. then the left and right subtree is a binary tree. before, i used nodes to construct binary trees.. i can't root.left. if sumone could just look and see what is the problem.
java Syntax (Toggle Plain Text)
package assignments; public class BinaryTree { int root; BinaryTree leftSubTree; BinaryTree rightSubTree; static BinaryTree myTree; BinaryTree(){ root = 0; leftSubTree = null; rightSubTree = null; } BinaryTree(Integer n){ root = n; } BinaryTree(Integer n, BinaryTree left, BinaryTree right){ root = n; leftSubTree = left; rightSubTree = right; } public int getRoot(){ return(root); } public BinaryTree getLeftChild(){ return(leftSubTree); } public BinaryTree getRightChild(){ return (rightSubTree); } public int getHeight(){ int left = -1; int right = -1; if( leftSubTree != null ){ left = leftSubTree.getHeight(); } if( rightSubTree != null ){ right = rightSubTree.getHeight(); } if( left > right ){ return left + 1; }return right + 1; } public boolean isEmpty(){ boolean ai = false; if(root == 0){ return true; }return ai; } public int countNodes(){ int count = 1; if (this.getLeftChild() != null){ count += this.getLeftChild().countNodes(); } if (this.getRightChild() != null){ count += this.getRightChild().countNodes(); }return count; } public int countLeaves(BinaryTree t){ int count = 0; if(root == 0){ countLeaves(leftSubTree); if(leftSubTree.root != 0 && rightSubTree.root != 0){ count++; } countLeaves(rightSubTree); }return count; } public void destroy(){ } public void preorderTraversal(){ if (root != 0) { System.out.println(root); new BinaryTree(leftSubTree.root).preorderTraversal(); new BinaryTree(rightSubTree.root).preorderTraversal(); } } public void inorderTraversal(){ if (root != 0) { new BinaryTree(leftSubTree.root).inorderTraversal(); System.out.println(root); new BinaryTree(rightSubTree.root).inorderTraversal(); } } public void postorderTraversal(){ if (root != 0) { new BinaryTree(leftSubTree.root).postorderTraversal(); new BinaryTree(rightSubTree.root).postorderTraversal(); System.out.println(root); } } public void levelorderTraversal(){ } public void treeInsert(BinaryTree t, int newItem) { // Add the item to the binary sort tree to which the parameter // "root" refers. Note that root is passed by reference since // its value can change in the case where the tree is empty. if ( t.root == 0 ) { // The tree is empty. Set root to point to a new node containing // the new item. This becomes the only node in the tree. t = new BinaryTree(newItem); // NOTE: The left and right subtrees of root // are automatically set to NULL by the constructor. // This is important! return; } else if ( newItem < t.root) { treeInsert( leftSubTree, newItem ); }else { treeInsert(rightSubTree, newItem ); } } public static void main(String args[]){ myTree = new BinaryTree(); myTree.insert(); System.out.println("Height of tree is: "+ myTree.getHeight()); System.out.println("Number of nodes: "+ myTree.countNodes()); System.out.println("Number of leaves: "+ myTree.countLeaves(myTree)); System.out.println("Preorder: "); myTree.preorderTraversal(); System.out.println("Inorder: "); myTree.inorderTraversal(); System.out.println("PostOrder: "); myTree.postorderTraversal(); System.out.println("LevelOrder: "); myTree.levelorderTraversal(); } public void insert(){ int arrNum[] = {50,17,76,9,23,54,14,19,72,12,67}; for(int i=0; i<=10; i++){ treeInsert(arrNum[i]); } } }
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 145
treeInsert(BinaryTree t, int newItem) is a method that requires two parameters
[I]treeInsert(arrNum); is a method call that has only one parameter.
Error!
Plus: this doesn't do anything
You create a new BinaryTree, make t ( a parameter - hence local) a reference to it, then exit. t goes out of scope, the new BinaryTree will be garbage collected. You may have made this mistake because earlier you commented
Note that root is passed by reference
Java does not support parameter passing by reference. All parameters are passed by value.
[I]treeInsert(arrNum); is a method call that has only one parameter.
Error!
Plus: this doesn't do anything
Java Syntax (Toggle Plain Text)
if ( t.root == 0 ) { // The tree is empty. Set root to point to a new node containing // the new item. This becomes the only node in the tree. t = new BinaryTree(newItem); // NOTE: The left and right subtrees of root // are automatically set to NULL by the constructor. // This is important! return;
You create a new BinaryTree, make t ( a parameter - hence local) a reference to it, then exit. t goes out of scope, the new BinaryTree will be garbage collected. You may have made this mistake because earlier you commented
Note that root is passed by reference
Java does not support parameter passing by reference. All parameters are passed by value.
Last edited by JamesCherrill; Jul 20th, 2009 at 8:45 am.
•
•
Join Date: Sep 2008
Posts: 91
Reputation:
Solved Threads: 1
java Syntax (Toggle Plain Text)
public void BTreeInsert(BinaryTree myTree, int newItem) { if ( myTree == null ) { myTree = new BinaryTree(newItem); } else { if ( newItem < myTree.root) { BTreeInsert(leftSubTree, newItem ); }else { BTreeInsert(rightSubTree, newItem ); } } }
and i change my main to this with additional methods
[codes=java]
public static void main(String args[]){
myTree = new BinaryTree();
myTree.insert();
myTree.display();
}
public void insert(){
int arrNum[] = {50,17,76,9,23,54,14,19,72,12,67};
for(int i=0; i<=10; i++){
BTreeInsert(myTree, arrNum[i]);
}
}
[/codes]
When i run it, because the task is to output the heigth, number of nodes, number of leaves, and the three traversals
height is 0
number of nodes is 0
No. of leaves is 0.
traversals ... none.
i don't know if i successfully inserted the numbers in the tree or not. some how, the BTreeInsert method is wrong. pls enlighten me.
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 145
System.out.println is your new best friend.
At each stage of your code, put in temporary System.out.println statements that print the values that you have just set/created. That way you can see:
1. Whether the code is being executed at all
2. Whether all the (local) values are being set as you expected.
When you see where it's going wrong you can add more temporary System.out.println's to narrow down the problem until it becomes obvious.
It's no good running the whole program then just looking at the results at the end. If it goes wrong you have no idea where or why.
ps Did you fix the error with new BinaryTree that I described before?
pps:
is a very long way to say
At each stage of your code, put in temporary System.out.println statements that print the values that you have just set/created. That way you can see:
1. Whether the code is being executed at all
2. Whether all the (local) values are being set as you expected.
When you see where it's going wrong you can add more temporary System.out.println's to narrow down the problem until it becomes obvious.
It's no good running the whole program then just looking at the results at the end. If it goes wrong you have no idea where or why.
ps Did you fix the error with new BinaryTree that I described before?
pps:
JAVA Syntax (Toggle Plain Text)
public boolean isEmpty(){ boolean ai = false; if(root == 0){ return true; }return ai; }
is a very long way to say
JAVA Syntax (Toggle Plain Text)
public boolean isEmpty(){ return root == 0; }
Last edited by JamesCherrill; Jul 22nd, 2009 at 10:13 am. Reason: added ps, pps
•
•
Join Date: Sep 2008
Posts: 91
Reputation:
Solved Threads: 1
•
•
•
•
System.out.println is your new best friend.
At each stage of your code, put in temporary System.out.println statements that print the values that you have just set/created. That way you can see:
1. Whether the code is being executed at all
2. Whether all the (local) values are being set as you expected.
When you see where it's going wrong you can add more temporary System.out.println's to narrow down the problem until it becomes obvious.
It's no good running the whole program then just looking at the results at the end. If it goes wrong you have no idea where or why.
ps Did you fix the error with new BinaryTree that I described before?
}[/CODE]
here's what i did
java Syntax (Toggle Plain Text)
public void BTreeInsert(BinaryTree myTree, int newItem) { if ( myTree == null ) { myTree = new BinaryTree(newItem); } else { if ( newItem < myTree.root) { BTreeInsert(leftSubTree, newItem ); }else { BTreeInsert(rightSubTree, newItem ); } } } public static void main(String args[]){ myTree = new BinaryTree(); myTree.insert(); myTree.display(); } public void insert(){ int arrNum[] = {50,17,76,9,23,54,14,19,72,12,67}; for(int i=0; i<=10; i++){ BTreeInsert(myTree, arrNum[i]); } } public void display(){ System.out.println("Height of tree is: "+ getHeight(myTree)); System.out.println("Number of nodes: "+ countNodes(myTree)); System.out.println("Number of leaves: "+ countLeaves(myTree)); System.out.println("Preorder: " + preorderTraversal(myTree) ); System.out.println("Inorder: " + inorderTraversal(myTree)); System.out.println("Postorder: " + postorderTraversal(myTree)); System.out.println("Levelorder: " + levelorderTraversal()); }
ask what i've said, When i run it, because the task is to output the heigth, number of nodes, number of leaves, and the three traversals
height is 0
number of nodes is 0
No. of leaves is 0.
traversals ... none.
i don't know if i successfully inserted the numbers in the tree or not. some how, the BTreeInsert method is wrong..
i'll try to do the temporary sys.out thing you said
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 145
You still have the same problem with this code:
myTree is a parameter, a local variable, you cannot change the value of the myTree declared in main(...) by doing anything to the local myTree in BTreeInsert.
Because the only reference to your new BinaryTree is held in the local parameter, that reference will be lost when the method ends, and your new BinaryTree will be garbage collected.
I'm sure this isn'ty what you intended.
Java Syntax (Toggle Plain Text)
public void BTreeInsert(BinaryTree myTree, int newItem) { if ( myTree == null ) { myTree = new BinaryTree(newItem); } else ...
myTree is a parameter, a local variable, you cannot change the value of the myTree declared in main(...) by doing anything to the local myTree in BTreeInsert.
Because the only reference to your new BinaryTree is held in the local parameter, that reference will be lost when the method ends, and your new BinaryTree will be garbage collected.
I'm sure this isn'ty what you intended.
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 145
how to do what exactly? I confess that I've been dealing with specific Java syntax problems and not the overall design of your code because I can't really follow it. It looks like your BinaryTree class is really a Node class, and the the root variable is really the node data, and you don't really have a root node for the whole tree, but maybe that's just because I'm confused.
•
•
Join Date: Sep 2008
Posts: 91
Reputation:
Solved Threads: 1
that's exactly why i'm confused too. In a node, there's a root for the info and a left and right link. and what i know is that a binary tree uses nodes.
In this exercise, this is what was given to us, and i have noticed that the binary tree class is somewhat like a node class.
in a binary tree with node class, you can do root.left or root.right. because root is a node.
in my class, i'm confused. i can't root.left or root.right because root is data (int). leftSubTree is a binarytree.
is my binary class the same as a node class? because i was thinking of extending a node class. but then i was looking at the constuctors, and now i'm confused.
In this exercise, this is what was given to us, and i have noticed that the binary tree class is somewhat like a node class.
in a binary tree with node class, you can do root.left or root.right. because root is a node.
in my class, i'm confused. i can't root.left or root.right because root is data (int). leftSubTree is a binarytree.
is my binary class the same as a node class? because i was thinking of extending a node class. but then i was looking at the constuctors, and now i'm confused.
![]() |
Similar Threads
- complete binary tree using an array help (C++)
- binary tree traversal .. (C++)
- Binary Tree Traversal (C)
- Question about binary tree & heaps (Computer Science)
- binary tree class (C++)
- C++ complete binary tree using an array. Unexpected end file (C++)
- coding a complete binary tree with Dev-C++ (C++)
Other Threads in the Java Forum
- Previous Thread: Binary Tree
- Next Thread: adding column in JDBC with MS access
| Thread Tools | Search this Thread |
actuate android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component database defaultmethod development dice dragging ebook eclipse error event exception formatingtextintooltipjava fractal froglogic game givemetehcodez graphics gui hql html ide image infinite input integer intersect invokingapacheantprogrammatically j2me java javaprojects jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie numbers openjavafx parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads time tree windows





