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:
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top