Java recursive binary tree

Reply

Join Date: Oct 2009
Posts: 16
Reputation: TigerGirl is an unknown quantity at this point 
Solved Threads: 0
TigerGirl TigerGirl is offline Offline
Newbie Poster

Java recursive binary tree

 
0
  #1
Oct 16th, 2009
Hi.
I have a recursive public static method search that takes a Tree node (any arbitrary binary tree, not necessarily a search tree) and returns whether or not that tree satisfies the order property for a binary search tree. So, my method is
  1. public static boolean search(TN t)
  2. {
  3. if (t == null)
  4. return true;
  5. else
  6. if (t.value == t.next.value)
  7. return search(?); // I don't know what I should put there
  8. else if (toFind < t.value)
  9. return search(t.left, t.next.value);
  10. else
  11. return search(t.right,t.next.value);
  12. }
Last edited by TigerGirl; Oct 16th, 2009 at 3:27 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 19
Reputation: bentlogic is an unknown quantity at this point 
Solved Threads: 0
bentlogic bentlogic is offline Offline
Newbie Poster
 
0
  #2
Oct 16th, 2009
So do you want to stop the recursion at this point (as node found)? If yes, then what about supplying the inductive node base case? Will that 'unwind' the recursion upon treenode found? Not sure if I'm wasting your time really...
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 16
Reputation: TigerGirl is an unknown quantity at this point 
Solved Threads: 0
TigerGirl TigerGirl is offline Offline
Newbie Poster
 
0
  #3
Oct 16th, 2009
What do you mean by the inductive node base case? I thought I already have that.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 19
Reputation: bentlogic is an unknown quantity at this point 
Solved Threads: 0
bentlogic bentlogic is offline Offline
Newbie Poster
 
0
  #4
Oct 16th, 2009
This is day 2 awake so not real cognitive... I see your base case when node is null.

In terms of knowing what param to put on line 7 ( I'm so tired a 'shiny pony' will do just fine ) what about null. Can we pass back null to stop the recursion without throwing a nullpointerexception in java?

I dunno, sorry!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 19
Reputation: bentlogic is an unknown quantity at this point 
Solved Threads: 0
bentlogic bentlogic is offline Offline
Newbie Poster
 
0
  #5
Oct 16th, 2009
Let's have another stab and I promise it's the last one

that was just silly above; I don't understand the algorithm but what about ending the recursion when either condition occurs, i.e.
  1. if( (t == null) || (t.value == t.next.value) )
  2. return true;
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 16
Reputation: TigerGirl is an unknown quantity at this point 
Solved Threads: 0
TigerGirl TigerGirl is offline Offline
Newbie Poster
 
0
  #6
Oct 16th, 2009
yeah! that sounds about right, I have to check it, but what about the else do you think that the way that I used to call the method is correct??
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 23
Reputation: jmaat7 is an unknown quantity at this point 
Solved Threads: 3
jmaat7 jmaat7 is offline Offline
Newbie Poster
 
0
  #7
Oct 17th, 2009
Sorry If i'm overlooking something simple, but doesn't your method only take one parameter? (TN t)

the call to the method in the else statements try to pass 2 parameters
(t.left, t.next.value)
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 16
Reputation: TigerGirl is an unknown quantity at this point 
Solved Threads: 0
TigerGirl TigerGirl is offline Offline
Newbie Poster
 
0
  #8
Oct 17th, 2009
You're right! So would I instead call t.left then? is the code even right?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 23
Reputation: jmaat7 is an unknown quantity at this point 
Solved Threads: 3
jmaat7 jmaat7 is offline Offline
Newbie Poster
 
0
  #9
Oct 17th, 2009
Actually, I think you're missing some code. the base case looked fine the way it was, because you'll be moving down the branches until you hit a null anyway. there's no reason to compare weather they're ==.

yeah for this you would just pass t.left as the parameter for the method i mentioned above. But what are you setting toFind to? This method looks like it will run down only 1 side of the tree.

ex:
for a balanced tree, traversing from the top
5,3,7,1,9

if toFind is initially set to 7 it and t is the first node(5) it will compare 7 to 5 and recursively move to the left. but that will return a null showing that the tree is unbalanced.

I think the code is more complex than this, i had a similar problem in my data structures course. I think the way I did it was by running down each branch and setting a counter for the levels, then i would compare the counters with the other side of the branch to make sure they were balanced. I got the grade, but the teacher said it was overkill.

I think I remember using the recursive method 2x (the first one with the left, second with the right). Basically, I traversed the tree starting from the top and counted that the levels were the same at the end.

I'll try to look up my old assignment, but in the meantime good luck.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 16
Reputation: TigerGirl is an unknown quantity at this point 
Solved Threads: 0
TigerGirl TigerGirl is offline Offline
Newbie Poster
 
0
  #10
Oct 17th, 2009
Thanks, I really need an example, because right now, I have no clue what to do for the else, but I have one more question. So, instead of returning an else, should I just make statements, because the method keeps returning false no matter what.
Reply With Quote Quick reply to this message  
Reply

Tags
binary, java, recursive, tree

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




Views: 2582 | Replies: 9
Thread Tools Search this Thread



Tag cloud for binary, java, recursive, tree
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC