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 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top