# Thread: Binary Tree depth help

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;

}```

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...

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?

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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•