Results 1 to 1 of 1
- 12-11-2011, 10:58 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
Recursion to check the "rightness" of a search tree.
Hi everyone. I'm having trouble programming a recursive method that determines the "rightness" of a search tree. The rightness of a node is 1 if the size of its right subtree is larger than the size of its left subtree. The rightness of a node is -1 if the size of its left subtree is larger than the size of its right subtree. The rightness of a node is 0 if the sizes are equal. The rightness of a subtree is equal to the rightness of its left subtree plus the rightness of its right subtree plus the rightness of its root. The rightness of an empty tree is 0.
What I have runs, but I don't think it is correct. Although, I honestly don't fully understand the problem:
This is the rightness method:
This is size:Java Code:public int Rightness() { return Rightness(Root); } private int Rightness(Tree_Node T) { if(T != null) { if(Size(T.Left) > Size(T.Right)) { return Rightness(T.Left) + Rightness(T.Right) - 1; } else if(Size(T.Left) < Size(T.Right)) { return Rightness(T.Left) + Rightness(T.Right) + 1; } else { return Rightness(T.Left) + Rightness(T.Right); } } return 0; }
Java Code:private int Size(Tree_Node T) { if(T != null) { return Size(T.Left) + Size(T.Right) +1; } return 0; }
Similar Threads
-
Google Error - "This site may harm your computer" on every search result?!?!
By angryboy in forum Forum LobbyReplies: 2Last Post: 03-18-2009, 08:36 PM -
Java subprocesses via Runtime.exec() and windows "end process tree"...
By fxRichard in forum Advanced JavaReplies: 2Last Post: 01-06-2009, 03:53 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
How to check a "connection" from datasource is in Container and part of Transaction
By alexendra in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 05-24-2008, 08:51 AM -
How to check a "connection" from datasource is in Container and part of Transaction
By alexendra in forum JDBCReplies: 0Last Post: 05-24-2008, 08:50 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks