943,729 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 983
  • Java RSS
Feb 13th, 2009
0

problem with Binary tree.. plse help

Expand Post »
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
Reputation Points: 9
Solved Threads: 1
Junior Poster
ezkonekgal is offline Offline
109 posts
since Sep 2008
Feb 13th, 2009
0

Re: problem with Binary tree.. plse help

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
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Feb 19th, 2009
0

Re: problem with Binary tree.. plse help

the structure that i want to use is node or link..
do i use recursion on this?
Reputation Points: 9
Solved Threads: 1
Junior Poster
ezkonekgal is offline Offline
109 posts
since Sep 2008
Feb 19th, 2009
0

Re: problem with Binary tree.. plse help

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 !!!
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Mar 2nd, 2009
0

Re: problem with Binary tree.. plse help

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:

java Syntax (Toggle Plain Text)
  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...
Reputation Points: 9
Solved Threads: 1
Junior Poster
ezkonekgal is offline Offline
109 posts
since Sep 2008
Mar 2nd, 2009
0

Re: problem with Binary tree.. plse help

Use recursion to insert nodes. Recursion is basically a function calling itself. A simple example of recursion is as follows:
java Syntax (Toggle Plain Text)
  1. int factorial(int n) {
  2. if (n == 0)
  3. return 1;
  4. else
  5. return (n * factorial(n-1));
  6. }
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Mar 3rd, 2009
0

Re: problem with Binary tree.. plse help

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...
Reputation Points: 9
Solved Threads: 1
Junior Poster
ezkonekgal is offline Offline
109 posts
since Sep 2008
Mar 4th, 2009
0

Re: problem with Binary tree.. plse help

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.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Mar 9th, 2009
0

Re: problem with Binary tree.. plse help

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)?
Reputation Points: 9
Solved Threads: 1
Junior Poster
ezkonekgal is offline Offline
109 posts
since Sep 2008
Mar 9th, 2009
0

Re: problem with Binary tree.. plse help

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.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: System.out.println??
Next Thread in Java Forum Timeline: how can i do dis.login name & password should be same as System Login id and password





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC