MHB Finding Depth & Height of Node w/ Key $r$ in $\text{AVL tree}$

  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Properties
AI Thread Summary
The discussion focuses on developing an algorithm to find the depth and height of a node with a given key $r$ in an AVL tree. Participants agree that the algorithm should first locate the node by comparing keys and then record its depth. Once the node is found, a second phase involves recursively calculating the height by exploring its children and determining the maximum depth from that point. There is also a consensus on ensuring the algorithm handles cases where the key $r$ is not present in the tree to avoid crashes. The final approach combines both depth and height calculations within a single algorithmic structure.
evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hi! (Wave)

We have an $\text{AVL tree}$ and we are given a key $r$. I want to write an algorithm that finds the depth and height of the node, of which the key is $r$.

So, do we have to compare the key of the root with the key $r$ and if it is greater we will have to continue at the left subtree to find the element with the key $r$ and if it is smaller we will look at the right subtree?
If the root has the key $r$, the depth will be equal to $0$, right? How could we then find the height? (Thinking)
 
Technology news on Phys.org
evinda said:
Hi! (Wave)

Hey! (Smirk)

We have an $\text{AVL tree}$ and we are given a key $r$. I want to write an algorithm that finds the depth and height of the node, of which the key is $r$.

So, do we have to compare the key of the root with the key $r$ and if it is greater we will have to continue at the left subtree to find the element with the key $r$ and if it is smaller we will look at the right subtree?

Yep! (Smile)

If the root has the key $r$, the depth will be equal to $0$, right? How could we then find the height? (Thinking)

Recursively iterate through the remainder of the tree down and track what the maximum is that you go down? (Wondering)
 
Code:
depth=0;
Algorithm(pointer R,r){
  if (R->key==r){
     return depth;
  }
  else if (R->key<r){
      depth++;
      Algorithm(R->RC,r)
      depth--;
  }
  else {
      depth++;
      Algorithm(R->LC,r)
     depth--;
    }
}

Can we find like that the depth of the node with key $r$ ? (Thinking)

If so, suppose that we have this tree:

View attachment 3633

Then, these commands will be executed, right? (Thinking)

depth=0
Algorithm(A,64)
depth=1
Algorithm(C,64)
depth=2
Algorithm(F,64)
return depth
After this command is executed: [m] return depth; [/m], will the algorithm terminate? (Thinking)
 
evinda said:
Can we find like that the depth of the node with key $r$ ? (Thinking)

Yes. (Smile)
After this command is executed: [m] return depth; [/m], will the algorithm terminate? (Thinking)

Not immediately. (Thinking)

It will return to the previous recursion level, where the return value is ignored. :eek:
Then depth gets decremented.
And since there is no return statement at the end of the function, nothing will get returned.

After returning from all recursion levels, the depth will be 0 again, and nothing will have been returned. (Worried)
 
I like Serena said:
Not immediately. (Thinking)

It will return to the previous recursion level, where the return value is ignored. :eek:
Then depth gets decremented.
And since there is no return statement at the end of the function, nothing will get returned.

After returning from all recursion levels, the depth will be 0 again, and nothing will have been returned. (Worried)

So should it be maybe like that? (Thinking)

Code:
depth=0;
Algorithm(pointer R,r){
  int k;
  if (R->key==r){
     return depth;
  }
  else if (R->key<r){
      depth++;
      k=Algorithm(R->RC,r)
      depth--;
  }
  else {
      depth++;
      k=Algorithm(R->LC,r)
      depth--;
   }
   return k;
}
 
evinda said:
So should it be maybe like that? (Thinking)

Code:
depth=0;
Algorithm(pointer R,r){
  int k;
  if (R->key==r){
     return depth;
  }
  else if (R->key<r){
      depth++;
      k=Algorithm(R->RC,r)
      depth--;
  }
  else {
      depth++;
      k=Algorithm(R->LC,r)
      depth--;
   }
   return k;
}

Yes. That should work to find $r$ and to return its depth. (Smile)

However, what wil happen if $r$ is not present in the tree? (Worried)
 
I like Serena said:
Yes. That should work to find $r$ and to return its depth. (Smile)

However, what wil happen if $r$ is not present in the tree? (Worried)

So, do we have to have also a variable that gets the value $1$ if we found such a node? (Thinking)

Also, do we have to use now the depth in order to find the height? (Thinking)
 
evinda said:
So, do we have to have also a variable that gets the value $1$ if we found such a node? (Thinking)

Also, do we have to use now the depth in order to find the height? (Thinking)

Your algorithm should run through 2 phases:
  1. Find the node $r$ and record its depth.
  2. Recursively visit all children of the node with $r$ and record the maximum depth.
The height is the difference between the 2. (Nerd)Btw, you also need to make sure the algorithm behaves if $r$ cannot be found.
As it is now, it will crash! :eek:
 
I like Serena said:
Your algorithm should run through 2 phases:
  1. Find the node $r$ and record its depth.
  2. Recursively visit all children of the node with $r$ and record the maximum depth.
The height is the difference between the 2. (Nerd)

But, won't the depth of the children of the node with $r$ be always $0$ or $1$, since it is one level after this node? :confused:

Or have I understood it wrong? (Worried)
 
  • #10
evinda said:
But, won't the depth of the children of the node with $r$ be always $0$ or $1$, since it is one level after this node? :confused:

Or have I understood it wrong? (Worried)

How so? (Wondering)

Let's take a look at your example tree.
It has $50$ in its root.
Suppose we want to find $r=50$.
Then its depth is $0$, while its height is $2$. (Thinking)
 
  • #11
I like Serena said:
How so? (Wondering)

Let's take a look at your example tree.
It has $50$ in its root.
Suppose we want to find $r=50$.
Then its depth is $0$, while its height is $2$. (Thinking)

https://www.physicsforums.com/attachments/3634

If we want to find the depth and height of the node with key 100, we will see that $d=1$, right?
The children of this node are D and E.
Isn't their depth equal to $d+1$? (Sweating)
 
  • #12
evinda said:
https://www.physicsforums.com/attachments/3634

If we want to find the depth and height of the node with key 100, we will see that $d=1$, right?
The children of this node are D and E.
Isn't their depth equal to $d+1$? (Sweating)

The deepest child starting from the node with key 100 is node F.
That means that the height of node 100 is 2. (Wasntme)

See for instance: algorithm - What is the difference between tree depth and height? - Stack Overflow
 
  • #13
I like Serena said:
Your algorithm should run through 2 phases:
  1. Find the node $r$ and record its depth.
  2. Recursively visit all children of the node with $r$ and record the maximum depth.
The height is the difference between the 2. (Nerd)

Do we have to find the height inside the function [m] Algorithm [/m] ? :confused:

Also, do we have to find again, after having found the depth, the position at which we found the node with key $r$ ? (Thinking)
I like Serena said:
Btw, you also need to make sure the algorithm behaves if $r$ cannot be found.
As it is now, it will crash! :eek:

So, do we have to consider a variable [m] found [/m] like that? (Thinking)

Code:
depth=0;
Algorithm(pointer R,r){
  int k,found=0;
  if (R->key==r){
     found=1;
     return depth;
  }
  else if (R->key<r){
      depth++;
      k=Algorithm(R->RC,r)
      depth--;
  }
  else {
      depth++;
      k=Algorithm(R->LC,r)
      depth--;
   }
   return k;
}
 
  • #14
evinda said:
Do we have to find the height inside the function [m] Algorithm [/m] ? :confused:

Up to you. (Wait)
Also, do we have to find again, after having found the depth, the position at which we found the node with key $r$ ? (Thinking)

I believe that can be part of the same algorithm.
No need to "find again". Just continue to recurse differently after we have found $r$. (Thinking)
So, do we have to consider a variable [m] found [/m] like that? (Thinking)

I don't think so.
The point is that you cannot refer to [m]R->key[/m] as long as you cannot be sure the R != NULL.
You need a check whether R == NULL or not! (Doh)
 
  • #15
I like Serena said:
I believe that can be part of the same algorithm.
No need to "find again". Just continue to recurse differently after we have found $r$. (Thinking)

Could you give me a hint how we could do this? (Worried)
I like Serena said:
I don't think so.
The point is that you cannot refer to [m]R->key[/m] as long as you cannot be sure the R != NULL.
You need a check whether R == NULL or not! (Doh)

A ok... (Thinking)
 
  • #16
evinda said:
Could you give me a hint how we could do this? (Worried)

Let's see...

I would set it up with something like:
Code:
Algorithm(root, r, *depth, *height)
  node = Find key(root, r, depth)
  if node != NULL
    *height = Determine height(node, 0)

Find key(R, r, *depth)
  Search the tree for r
  if found
    register its depth in *depth
    return its node
  else
    return NULL

Determine height(R, depth)
  if R == NULL
    return depth - 1
  leftHeight = Determine height(R->left, depth + 1)
  rightHeight = Determine height(R->right, depth + 1)
  return max(leftHeight, rightHeight)
(Wasntme)
 

Similar threads

Back
Top