Is my AVL-tree height finding method correct?

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Height Specific
Click For Summary
SUMMARY

The discussion centers on the correctness of an AVL-tree height finding method. The original implementation defines the height of an empty tree as 0, which deviates from the conventional definition of -1. A revised height function is provided that correctly calculates the height using a recursive approach. Additionally, the absence of the LookUp function in the original code is noted, which is essential for locating the node with key $k$.

PREREQUISITES
  • Understanding of AVL-tree properties and structure
  • Familiarity with recursive programming techniques
  • Knowledge of C/C++ pointer manipulation
  • Basic concepts of binary tree height definitions
NEXT STEPS
  • Study AVL-tree height definitions and their implications
  • Implement and test the LookUp function for AVL-trees
  • Learn about recursive algorithms in C/C++ for tree traversal
  • Explore performance optimizations for AVL-tree operations
USEFUL FOR

Students studying data structures, software developers implementing AVL-trees, and educators teaching algorithms related to binary trees.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
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
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:

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
Replies
2
Views
2K
  • · Replies 20 ·
Replies
20
Views
5K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K