| | |
Java recursive binary tree
![]() |
•
•
Join Date: Oct 2009
Posts: 16
Reputation:
Solved Threads: 0
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
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
Java Syntax (Toggle Plain Text)
public static boolean search(TN t) { if (t == null) return true; else if (t.value == t.next.value) return search(?); // I don't know what I should put there else if (toFind < t.value) return search(t.left, t.next.value); else return search(t.right,t.next.value); }
Last edited by TigerGirl; Oct 16th, 2009 at 3:27 pm.
•
•
Join Date: Oct 2009
Posts: 19
Reputation:
Solved Threads: 0
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!
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!
•
•
Join Date: Oct 2009
Posts: 19
Reputation:
Solved Threads: 0
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.

that was just silly above; I don't understand the algorithm but what about ending the recursion when either condition occurs, i.e.
java Syntax (Toggle Plain Text)
if( (t == null) || (t.value == t.next.value) ) return true;
•
•
Join Date: Sep 2009
Posts: 23
Reputation:
Solved Threads: 3
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.
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.
![]() |
Similar Threads
- Threaded binary tree in java (Java)
- writeObject method for Binary Tree using serialization (Java)
- inorder traversal in binary tree. (Java)
- Binary Tree implementation (C++)
- Recursive Binary Search Tree Header File (C++)
- Null pointer on building a binary tree (Java)
- How to implement a binary tree Using HashMap Collection in java (Java)
- Coding a Complete Binary Tree (C)
- complete binary tree using an array help (C++)
Other Threads in the Java Forum
- Previous Thread: java class reference problem - why?
- Next Thread: Best Compiler/IDE for Java?
Views: 2582 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for binary, java, recursive, tree
ajax applet array arrays banking binary build business c# c++ chat class classes client code compile compiler component conversion convert converter data db decimal delete derby design desktop development draw eclipse editor error event exception fast file forms fractal function game gui helpwithhomework hql html ide image images input int integer intellij jar java javascript jdialog jframe jmf jpanel jsp julia linux loop main method mlm mobile netbeans newbie nextline number object php problem program programming project python recursion recursive resultset search server set socket software source string swing test thread time transfer translate tree user web whileloop windows word






