Is my AVL-tree height finding method correct?

In summary, the conversation discusses a test where the task was to find the height of a node with a given key in an AVL-tree. The person asking the question also provides a code they have tried to solve the problem and asks for feedback. The response includes a different implementation of a height function and a suggested LookUp function. Overall, the conversation focuses on finding the height of a node in an AVL-tree.
  • #1
evinda
Gold Member
MHB
3,836
0
Hello! (Wave)

I had a test today , it was given an AVL-tree and I had to find the height of the node with key $k$.

That's what I have tried:

Code:
Height(T,K){ 
  if (T==NULL) return; 
  pointer R; 
  R=LookUp(T,K); 
  int k=height(R); 
  return k; 
} height(pointer R){ 
  static j=0,l=0; 
  if (R==NULL) return 0; 
  if (R->lc!=NULL) l=1+height(R->lc); 
  if (R->rc!=NULL) j=l-height(R->lc)+height(R->rc); 
  if (R->rc==NULL and R->lc==NULL) return 1; 
  return max(l,j); 
}
Could you tell me if it is right? (Thinking)
 
Technology news on Phys.org
  • #2
Evinda,
Apparently, you are saying the height of an empty binary tree is 0. This is not the normal definition that the height of an empty binary tree is -1. So here's a simple height function:
Code:
int height(pointer R) {
  if (R==NULL) {
    return(-1);
  }
  return(1+max(height(R->LC),height(R->RC)));
}
So your height function is "correct" for your definition of height, but it's the worst implementation I can imagine.

Next, where is function LookUp? Presumably, the question wants you to write this function.

Code:
// assumes T is a value parameter
pointer LookUp(pointer T,int k) {
  while (T != NULL) {
     if (T->key == k) {
       return(T);
     }
     if (k < T->key) {
        T=T->LC;
     }
     else {
        T=T->RC;
     }
  }
  return(NULL);
}
If I were grading the question and was generous, I'd give you less than half credit.
 
Last edited:

FAQ: Is my AVL-tree height finding method correct?

1. What is the purpose of finding the height of a specific node?

The height of a specific node in a tree data structure is used to determine the level of that node in the tree. This can be useful in understanding the structure and organization of the tree, as well as in certain algorithms that require knowledge of the node's position.

2. How is the height of a specific node calculated?

The height of a node is equal to the number of edges between that node and the root node of the tree. This can be calculated recursively by finding the height of the left and right subtrees of the node and adding 1 to the larger value.

3. Can the height of a specific node be greater than the height of the entire tree?

Yes, it is possible for a node to have a height greater than the height of the entire tree. This can happen if the node is part of a subtree that is taller than the rest of the tree.

4. What is the time complexity of finding the height of a specific node?

The time complexity of finding the height of a specific node is O(log n), where n is the number of nodes in the tree. This assumes that the tree is balanced, otherwise the time complexity can be O(n) in the worst case.

5. Can the height of a specific node change?

Yes, the height of a specific node can change if the structure of the tree is modified. For example, if a new node is added to the subtree of the node, its height will increase by 1. Similarly, if a node is deleted from the subtree, its height will decrease by 1.

Similar threads

Replies
1
Views
1K
Replies
1
Views
1K
Replies
20
Views
4K
Replies
10
Views
2K
Replies
3
Views
1K
Back
Top