Binary Tree Help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 91
Reputation: ezkonekgal is an unknown quantity at this point 
Solved Threads: 1
ezkonekgal ezkonekgal is offline Offline
Junior Poster in Training

Binary Tree Help

 
0
  #1
Jul 20th, 2009
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.




  1.  
  2. package assignments;
  3.  
  4.  
  5. public class BinaryTree {
  6.  
  7.  
  8. int root;
  9. BinaryTree leftSubTree;
  10. BinaryTree rightSubTree;
  11. static BinaryTree myTree;
  12.  
  13.  
  14. BinaryTree(){
  15. root = 0;
  16. leftSubTree = null;
  17. rightSubTree = null;
  18.  
  19. }
  20.  
  21. BinaryTree(Integer n){
  22. root = n;
  23. }
  24.  
  25. BinaryTree(Integer n, BinaryTree left, BinaryTree right){
  26. root = n;
  27. leftSubTree = left;
  28. rightSubTree = right;
  29. }
  30.  
  31.  
  32. public int getRoot(){
  33. return(root);
  34. }
  35.  
  36. public BinaryTree getLeftChild(){
  37. return(leftSubTree);
  38. }
  39.  
  40. public BinaryTree getRightChild(){
  41. return (rightSubTree);
  42. }
  43.  
  44.  
  45. public int getHeight(){
  46.  
  47. int left = -1;
  48. int right = -1;
  49. if( leftSubTree != null ){
  50. left = leftSubTree.getHeight();
  51. }
  52. if( rightSubTree != null ){
  53. right = rightSubTree.getHeight();
  54. }
  55. if( left > right ){
  56. return left + 1;
  57. }return right + 1;
  58. }
  59.  
  60.  
  61.  
  62. public boolean isEmpty(){
  63. boolean ai = false;
  64. if(root == 0){
  65. return true;
  66. }return ai;
  67. }
  68.  
  69. public int countNodes(){
  70. int count = 1;
  71. if (this.getLeftChild() != null){
  72. count += this.getLeftChild().countNodes();
  73. }
  74. if (this.getRightChild() != null){
  75. count += this.getRightChild().countNodes();
  76. }return count;
  77. }
  78.  
  79. public int countLeaves(BinaryTree t){
  80. int count = 0;
  81. if(root == 0){
  82. countLeaves(leftSubTree);
  83. if(leftSubTree.root != 0 && rightSubTree.root != 0){
  84. count++;
  85. }
  86. countLeaves(rightSubTree);
  87. }return count;
  88. }
  89.  
  90.  
  91. public void destroy(){
  92.  
  93. }
  94.  
  95. public void preorderTraversal(){
  96. if (root != 0) {
  97. System.out.println(root);
  98. new BinaryTree(leftSubTree.root).preorderTraversal();
  99. new BinaryTree(rightSubTree.root).preorderTraversal();
  100. }
  101. }
  102.  
  103. public void inorderTraversal(){
  104. if (root != 0) {
  105. new BinaryTree(leftSubTree.root).inorderTraversal();
  106. System.out.println(root);
  107. new BinaryTree(rightSubTree.root).inorderTraversal();
  108. }
  109. }
  110.  
  111. public void postorderTraversal(){
  112. if (root != 0) {
  113. new BinaryTree(leftSubTree.root).postorderTraversal();
  114. new BinaryTree(rightSubTree.root).postorderTraversal();
  115. System.out.println(root);
  116. }
  117. }
  118.  
  119. public void levelorderTraversal(){
  120.  
  121. }
  122.  
  123. public void treeInsert(BinaryTree t, int newItem) {
  124. // Add the item to the binary sort tree to which the parameter
  125. // "root" refers. Note that root is passed by reference since
  126. // its value can change in the case where the tree is empty.
  127. if ( t.root == 0 ) {
  128. // The tree is empty. Set root to point to a new node containing
  129. // the new item. This becomes the only node in the tree.
  130. t = new BinaryTree(newItem);
  131. // NOTE: The left and right subtrees of root
  132. // are automatically set to NULL by the constructor.
  133. // This is important!
  134. return;
  135. } else if ( newItem < t.root) {
  136. treeInsert( leftSubTree, newItem );
  137. }else {
  138. treeInsert(rightSubTree, newItem );
  139. }
  140. }
  141.  
  142.  
  143. public static void main(String args[]){
  144.  
  145. myTree = new BinaryTree();
  146. myTree.insert();
  147. System.out.println("Height of tree is: "+ myTree.getHeight());
  148. System.out.println("Number of nodes: "+ myTree.countNodes());
  149. System.out.println("Number of leaves: "+ myTree.countLeaves(myTree));
  150. System.out.println("Preorder: ");
  151. myTree.preorderTraversal();
  152. System.out.println("Inorder: ");
  153. myTree.inorderTraversal();
  154. System.out.println("PostOrder: ");
  155. myTree.postorderTraversal();
  156. System.out.println("LevelOrder: ");
  157. myTree.levelorderTraversal();
  158. }
  159.  
  160. public void insert(){
  161. int arrNum[] = {50,17,76,9,23,54,14,19,72,12,67};
  162.  
  163. for(int i=0; i<=10; i++){
  164. treeInsert(arrNum[i]);
  165. }
  166.  
  167. }
  168.  
  169.  
  170. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Binary Tree Help

 
0
  #2
Jul 20th, 2009
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

  1. if ( t.root == 0 ) {
  2. // The tree is empty. Set root to point to a new node containing
  3. // the new item. This becomes the only node in the tree.
  4. t = new BinaryTree(newItem);
  5. // NOTE: The left and right subtrees of root
  6. // are automatically set to NULL by the constructor.
  7. // This is important!
  8. 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 91
Reputation: ezkonekgal is an unknown quantity at this point 
Solved Threads: 1
ezkonekgal ezkonekgal is offline Offline
Junior Poster in Training

Re: Binary Tree Help

 
0
  #3
Jul 22nd, 2009
  1. public void BTreeInsert(BinaryTree myTree, int newItem) {
  2. if ( myTree == null ) {
  3. myTree = new BinaryTree(newItem);
  4. } else {
  5. if ( newItem < myTree.root) {
  6. BTreeInsert(leftSubTree, newItem );
  7. }else {
  8. BTreeInsert(rightSubTree, newItem );
  9. }
  10. }
  11. }

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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Binary Tree Help

 
0
  #4
Jul 22nd, 2009
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:
  1. public boolean isEmpty(){
  2. boolean ai = false;
  3. if(root == 0){
  4. return true;
  5. }return ai;
  6. }

is a very long way to say

  1. public boolean isEmpty(){
  2. return root == 0;
  3. }
Last edited by JamesCherrill; Jul 22nd, 2009 at 10:13 am. Reason: added ps, pps
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 91
Reputation: ezkonekgal is an unknown quantity at this point 
Solved Threads: 1
ezkonekgal ezkonekgal is offline Offline
Junior Poster in Training

Re: Binary Tree Help

 
0
  #5
Jul 22nd, 2009
Originally Posted by JamesCherrill View Post
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]
I don't think i've fix it.

here's what i did

  1.  
  2. public void BTreeInsert(BinaryTree myTree, int newItem) {
  3. if ( myTree == null ) {
  4. myTree = new BinaryTree(newItem);
  5. } else {
  6. if ( newItem < myTree.root) {
  7. BTreeInsert(leftSubTree, newItem );
  8. }else {
  9. BTreeInsert(rightSubTree, newItem );
  10. }
  11. }
  12. }
  13.  
  14.  
  15. public static void main(String args[]){
  16. myTree = new BinaryTree();
  17. myTree.insert();
  18. myTree.display();
  19. }
  20.  
  21. public void insert(){
  22. int arrNum[] = {50,17,76,9,23,54,14,19,72,12,67};
  23. for(int i=0; i<=10; i++){
  24. BTreeInsert(myTree, arrNum[i]);
  25. }
  26.  
  27. }
  28.  
  29. public void display(){
  30. System.out.println("Height of tree is: "+ getHeight(myTree));
  31. System.out.println("Number of nodes: "+ countNodes(myTree));
  32. System.out.println("Number of leaves: "+ countLeaves(myTree));
  33. System.out.println("Preorder: " + preorderTraversal(myTree) );
  34. System.out.println("Inorder: " + inorderTraversal(myTree));
  35. System.out.println("Postorder: " + postorderTraversal(myTree));
  36. System.out.println("Levelorder: " + levelorderTraversal());
  37.  
  38.  
  39. }

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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Binary Tree Help

 
0
  #6
Jul 22nd, 2009
You still have the same problem with this code:

  1. public void BTreeInsert(BinaryTree myTree, int newItem) {
  2. if ( myTree == null ) {
  3. myTree = new BinaryTree(newItem);
  4. } 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 91
Reputation: ezkonekgal is an unknown quantity at this point 
Solved Threads: 1
ezkonekgal ezkonekgal is offline Offline
Junior Poster in Training

Re: Binary Tree Help

 
0
  #7
Jul 24th, 2009
yes, i know. but i don't know how
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Binary Tree Help

 
0
  #8
Jul 24th, 2009
Originally Posted by ezkonekgal View Post
.. but i don't know how
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 91
Reputation: ezkonekgal is an unknown quantity at this point 
Solved Threads: 1
ezkonekgal ezkonekgal is offline Offline
Junior Poster in Training

Re: Binary Tree Help

 
0
  #9
Jul 24th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 91
Reputation: ezkonekgal is an unknown quantity at this point 
Solved Threads: 1
ezkonekgal ezkonekgal is offline Offline
Junior Poster in Training

Re: Binary Tree Help

 
0
  #10
Jul 24th, 2009
Originally Posted by JamesCherrill View Post
...and you don't really have a root node for the whole tree...

do i have to have a root node? what am i going to do with this class?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC