problem with Binary tree.. plse 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

problem with Binary tree.. plse help

 
0
  #1
Feb 13th, 2009
I have a problem:
1.) How do you insert a sequence of integers into a binary tree? does anybody have an algorithm for it?

like input : 1, 5, 3, 4 ,2

binary tree:
1
5 3
4 2
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,701
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 228
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: problem with Binary tree.. plse help

 
0
  #2
Feb 13th, 2009
First you need to define what kind of structure to use in order to represent your tree. An array for example. Then you write code that inserts and reads the data, at and from the right place
Check out my New Bike at my Public Profile at the "About Me" tab
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: problem with Binary tree.. plse help

 
0
  #3
Feb 19th, 2009
the structure that i want to use is node or link..
do i use recursion on this?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: problem with Binary tree.. plse help

 
0
  #4
Feb 19th, 2009
Yes recursion is very efficient in case of trees. I suppose from your question in the first post that you want to know how to load the tree.
Well this depends on what purpose you want to use the tree for. If you are using the tree for sorting, you populate the tress in this way.
First element becomes the root, then for every element after that, we (recursively) decide it's position depending upon whether the element at the current node is greater than or less than the element to be inserted. If the element to be inserted is greater than the element at the current node we move to right of the current node else to left node of the current node. For elements found to be equal we can decide either way, in this case we decide to move towards the left node of the current node.

Applying the above given algorithm to the numbers you have given we proceed to build the tree this way:
Sequence of numbers : 1,5,3,4,2
1
5
3
2 4
Here number 1 is at the root, number 5 to it's right node, number 3 to the left node of 5, number 2 on the left node of 3, number 4 on the right node of number 3.
Now a normal Inorder traversal of the tree would yield you a sorted list.

In inorder traversal we first visit the left node,then the root and then the right node. We go on printing the numbers as we visit the nodes.
To traverse the tree we built above, we would first print the root element since there's no left node side of it, so we print 1, then move on to the nodewith number 5, but we don't print it since there is a left hand side of it, we move there, but there again it has a left node so we move there which does not have a left node so we print it out, thus printing 2, then we print 2's parent node which is 3 then it's right node which is 4 and then coming back to 3's parent node we print out 5, now the node with 5 does not have any right side node so we can stop printing, and as a result we have the sorted list 1,2,3,4,5 with us !!!
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
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: problem with Binary tree.. plse help

 
0
  #5
Mar 2nd, 2009
No, the tree isn't for sorting. I want to put the numbers example 1,2,3,4,5 in the binary tree. then it should be like this:

1, is in the root.
2, is in the left of the root
3, is in the right of the root
4 and 5 are now sons/children of 2 and are in the left and right of r respectively.

Now if i want to input more, like 6 and 7. then this would be inserted as being children of 3.



I have this method:

  1. public void treeInsert(String newItem) {
  2.  
  3. if ( root == null ) {
  4. // The tree is empty. Set root to point to a new node
  5. // containing the new item.
  6. root = new BinaryTreeNode( newItem );
  7. return;
  8. }
  9. BinaryTreeNode runner; // Runs down the tree to find a place for newItem.
  10. runner = root; // Start at the root.
  11. while (true){
  12. if ( runner.left == null ) {
  13. runner.left = new BinaryTreeNode( newItem );
  14. return; // New item has been added to the tree.
  15. }else if ( runner.right == null ) {
  16. runner.right = new BinaryTreeNode( newItem );
  17. return; // New item has been added to the tree.
  18. }else if (runner.left != null && runner.right != null){
  19. if(runner.left.left == null){
  20. runner.left.left = new BinaryTreeNode(newItem);
  21. return;
  22. }else if (runner.left.right == null){
  23. runner.left.right = new BinaryTreeNode(newItem);
  24. return;
  25. }
  26. runner = runner.right;
  27. if(runner.left == null){
  28. runner.right= new BinaryTreeNode(newItem);
  29. return;
  30. }else if (runner.right == null){
  31. runner.right = new BinaryTreeNode(newItem);
  32. return;
  33. }
  34. } runner = runner.left;
  35.  
  36. }

i can't seem ti get this right...
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: problem with Binary tree.. plse help

 
0
  #6
Mar 2nd, 2009
Use recursion to insert nodes. Recursion is basically a function calling itself. A simple example of recursion is as follows:
  1. int factorial(int n) {
  2. if (n == 0)
  3. return 1;
  4. else
  5. return (n * factorial(n-1));
  6. }
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
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: problem with Binary tree.. plse help

 
0
  #7
Mar 3rd, 2009
Recursions the part where i break up.. the above method i posted should be recursive but it seems that isn't. I'm having a hard time figuring out because there are a lot of considerations in inserting into a node. Like wether if it should be in the left son or right son. Its like when you delete from a complete binary tree, it is in reverse level order, right to left, bottom-up. Now if i want to insert stuffs in it, it should be from left to right, top to bottom...
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: problem with Binary tree.. plse help

 
0
  #8
Mar 4th, 2009
Read post #6 by me again, it is a typical example of recursion to calculate the factorial upto a number n. The method you have shown is not recursion, since recursion is a call to the same function within that function itself. So if you see the recursive method shown in post #6, the factorial method gives a recursive call to itself with one less than the number. Meaning a call to factorial(n) would result in a recursive call to factorial(n-1) which in turn would result in a call to factorial((n-1)-1) and so on till the termination condition of the recursion occurs which is when n finally becomes 0.

In your case, whenever you give a call to treeInsert(node) you need to check if node.left is null, if yes, create a new node, insert the data into it and assign this node to node.left, if node.left is not empty, do the same for node.right, if that too is found to be not empty, give a call, and here is where recursion comes in, to treeInsert(node.left) and the story continues in node.left.
Last edited by verruckt24; Mar 4th, 2009 at 5:11 am.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
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: problem with Binary tree.. plse help

 
0
  #9
Mar 9th, 2009
pardon me, you said treeInsert(node). but mine is treeInsert(newItem). Am i wrong in doing this? because further you said treeInsert(node.left) which i can't do.. are your saying that it should be treeInsert(node)?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: problem with Binary tree.. plse help

 
0
  #10
Mar 9th, 2009
It should be treeInsert(node, data) rather, where node is whose left and right are to be searched for a place to insert the new data. Once you find such node you create a new node with the data inside it and put its reference in the node's left or right part. Read my previous post again with the changes mentioned here.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
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



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC