MHB Is my AVL-tree height finding method correct?

  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Height Specific
AI Thread Summary
The discussion revolves around a test question involving AVL-trees, specifically finding the height of a node with a given key. The initial implementation of the height function is critiqued for defining the height of an empty tree as 0, which deviates from the more common definition of -1. An alternative height function is provided that correctly returns -1 for an empty tree. The critique emphasizes that while the original height function is technically correct based on its definition, it is poorly implemented. Additionally, the absence of a LookUp function in the original code is noted, with a suggested implementation provided to locate a node by its key. Overall, the response indicates that the original solution would receive a low score due to these issues.
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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top