void RRrotation(struct Avlnode *t) { struct Avlnode *Bptr,*pptr; pptr=t; Bptr=pptr->rchild; pptr->rchild=Bptr->lchild; pptr->h=findheight(pptr); Bptr->h=findheight(Bptr); pptr=Bptr; }
void LLrotation(struct Avlnode *t) { struct Avlnode *Aptr,*pptr; pptr=t; Aptr=pptr->lchild; pptr->lchild=Aptr->rchild; pptr->h=findheight(pptr); Aptr->h=findheight(Aptr); pptr=Aptr; }
int findheight(struct node* root) { int left_max=0,right_max=0; if(!root) return 0; if(root->left) left_max=1+findheight(root->left); if(root->right) right_max=1+findheight(root->right); return (left_max>right_max?left_max:right_max); }
int findheight(struct node* root) { int hleft, hright; if(root==NULL) return -1; hleft=findheight(root->left); hright =findheight(root->right); if(hleft>hright) { return hleft+1; } else return hright+1; }
int findheight(struct tree *root) { struct tree *temp = root; if(temp == NULL) return 0; else { int ldepth = findheight(temp->left); int rdepth = findheight(temp->right); if(ldepth > rdepth) return (ldepth+1); else return (rdepth+1); } }
int main() { struct tree *root = newnode(1); root->left = newnode(2); root->right = newnode(3); root->left->left = newnode(4); root->left->right = newnode(5); printf("the height of tree is %d", findheight(root)); return 0; }
// Driver program to test above functions int main() { struct node* root = NULL; struct Queue* queue = createQueue(SIZE); int i; for(i = 1; i <= 12; ++i) insert(&root, i, queue); printf("The tree elements are: "); levelOrder(root); printf("\nThe height of the tree is %d",findheight(root)); return 0; }
int findheight( struct Avlnode *root) { int height,x,y; struct Avlnode *ptr,*lptr,*rptr; ptr=root; if(ptr==NULL) { height=0; return height; } else { lptr=ptr->lchild; rptr=ptr->rchild; x=findheight(lptr); y=findheight(rptr); if(x<=y) height=y+1; else height=x+1; return height; } }
struct Avlnode *AddNode(struct Avlnode **root,struct Avlnode *Nptr) { struct Avlnode *ptr,*lptr,*rptr; int h1,h2,bf; ptr=*root; if(ptr==NULL) { ptr=Nptr; Nptr->lchild=NULL; Nptr->rchild=NULL; ptr->h=1; return ptr; } else { if(Nptr->data<ptr->data) { AddNode(&ptr->lchild,Nptr); lptr=ptr->lchild; rptr=ptr->rchild; if(rptr==NULL) { h2=0; } else { h2=rptr->h; h1=lptr->h; bf=h1-h2; if(bf==2) { if(Nptr->data<lptr->data) { LLrotation(ptr); } else { LRrotation(ptr); } ptr->h=findheight(ptr); } } } else { if(Nptr->data>ptr->data) { AddNode(&ptr->rchild,Nptr); rptr=ptr->rchild; lptr=ptr->lchild; if(lptr==NULL) { h1=0; } else { h2=rptr->h; h1=lptr->h; bf=h1-h2; if(bf==-2) { if(Nptr->data>rptr->data) { RRrotation(ptr); } else { RLrotation(ptr); } ptr->h=findheight(ptr); } } } } } return ptr; }