Results 1 to 4 of 4
Thread: Binary Tree depth help
- 04-02-2014, 07:33 PM #1
Member
- Join Date
- Feb 2014
- Posts
- 8
- Rep Power
- 0
Binary Tree depth help
I have a list of people with a matching telephone extension number, the people are organised in a hierachy tree with some colleagues at the same level and then some junior colleagues. I have been trying to write code that can find the tree height of any given member but I am unable too. Here is my code so far but the left and right are not working because I have not declared them any where in my code.
Java Code://hierarchy rank section 7 // **Depth** from node to root (bottom up) public int rank(Member p1){ //to do int currentDepth, depth; if (p1 != null) { currentDepth++; // record total depth if current depth is greater than total depth if (currentDepth > depth) { depth = currentDepth; } // recursively traverse entire tree depth(p1.left); depth(p1.right); // decrement as we traverse up the binary tree currentDepth--; } return depth; }
- 04-02-2014, 09:10 PM #2
Member
- Join Date
- Mar 2014
- Location
- Leb
- Posts
- 9
- Rep Power
- 0
Re: Binary Tree depth help
To get the depth of a BST you need to just count the steps taken to find the target.
Basically just add a counter to the search method of the tree, when done searching that counter is the depth of the searched for member...
- 04-02-2014, 10:41 PM #3
Member
- Join Date
- Feb 2014
- Posts
- 8
- Rep Power
- 0
Re: Binary Tree depth help
My problem is eclipse is highlighting an error on p1.left and p1.right because p1 has not been set as a node but just as a member. What I am trying to achieve with my method is that for any given member you should be able find their rank, with the rank being how many branches down from the head (I hope it makes sense). Do I need to find the node of the member then the p1.left and p1.right will work?
- 04-03-2014, 04:44 AM #4
Señor Member
- Join Date
- Jan 2014
- Posts
- 184
- Rep Power
- 0
Re: Binary Tree depth help
This may not be right:
When I worked with trees for writing a compression program, the "bottom" of my tree was simply another treenode with "null" branches.
AKA, your very bottom member should actually be another tree node, where you set the branches to be null. That way, you can keep going down the tree until you reach a "NULL." It's an easy way to know when to stop.
Similar Threads
-
Binary Tree Help - Find the largest sub-tree
By joshhazel in forum New To JavaReplies: 2Last Post: 01-30-2012, 03:08 AM -
Data Structures(Binary Search Tree to AVL Tree)ASAP pls
By jfAdik in forum Forum LobbyReplies: 0Last Post: 04-04-2010, 08:40 AM -
Binary Tree
By MuslimCoder in forum New To JavaReplies: 8Last Post: 11-19-2009, 06:57 PM -
Creating a tree of depth 5
By roaan in forum New To JavaReplies: 1Last Post: 07-13-2009, 10:26 AM
Bookmarks