# binary tree depth

This is a discussion on binary tree depth within the C Programming forums, part of the General Programming Boards category; hi, i just want to count the levels of random binary tree. i have used a queue to do a ...

1. ## binary tree depth

hi,
i just want to count the levels of random binary tree.
i have used a queue to do a level-order traversal.
So the depth is equal to the height of the tree node
stored int the tail of the queue,
here's the algorithms i used to get the level of the tree

Code:
```int levelCtr, nodeCtr = 0;
QueueNodePtr headPtr = NULL, tailPtr = NULL, indexPtr = NULL, lastPtr = NULL;
TreeNodePtr p;/*more code*/
lastPtr = tailPtr;

p = &( lastPtr -> qData );

while ( lastPtr != headPtr ) {
while ( ( indexPtr -> qData.leftPtr != &( lastPtr -> qData ) ) || ( indexPtr -> qData.rightPtr != &( lastPtr -> qData ) ) ) {
indexPtr = indexPtr -> nextPtr;
}

lastPtr = indexPtr;

levelCtr++;
}```
but the debugger shows that p doesn't get the address of qData,

any hints are welcome,
trk

2. Code:
```int depthCount( Node *n )
{
int x=1,y=1;

if ( !n ) return 0;

x += depthCount( n->left );
y += depthCount( n->right );
return x > y ? x : y;
}```
I think that'll do the trick. I haven't tested it, I just threw it together. However, it should work for what you want, assuming I'm understanding what you want done.

Quzah.

3. but the debugger shows that p doesn't get the address of qData,
This is normal. The address of the first element of a structure is generally the address of the structure.
Code:
```#include <stdio.h>

int main (void)
{
struct
{ int x, y} myStruct;
printf ("myStruct: %p\n", &myStruct);
printf ("myStruct.x: %p\n", &myStruct.x);
printf ("myStruct.y: %p\n", &myStruct.y);

return 0;
}```
And the output
Code:
```myStruct: 8f120
myStruct.x: 8f120
myStruct.y: 8f124```
Quzah's algorithm is the correct(TM) way to go about this kind of problem though; figure out the depth of the left subtree, the right subtree, pick the biggest, and add 1.

4. The members of a structure have
the address of the structure...i got that now.
I was just trying to find another way except the usual
recursive function.

thanks and sorry for replying late,
it's been a busy week