Avl.h
#ifndef _avltree_h#define_avltree_hstructNode;typedefintElementtype;typedefstructNode *ptrtonode;typedef ptrtonode avltree;typedef ptrtonode Position; Avltree makeempty (Avltree T);intHeight (Position P); Position Find (ElementType x,avltree T); Position Findmax (Avltree T); Position findmin (Avltree T); Avltree Insert (ElementType x,avltree T); Avltree Delete (ElementType x,avltree T);#endifstructnode{ElementType element; Ptrtonode left; Ptrtonode right; intHeight;};
Avl.c
#include <stdlib.h>#include<stdio.h>#include"AvlTree.h"#include".. /.. /lib/common.h"avltree makeempty (Avltree T) {if(t!=NULL) {Makeempty (T-Left ); Makeempty (T-Right ); Free(T); } returnNULL;}intHeight (Position P) {if(p==NULL) { return-1; }Else{ returnP->Height; }}position Find (ElementType x,avltree T) {if(t==NULL) { returnNULL; }Else if(x<t->Element) { returnFind (x,t->Left ); }Else if(x>t->Element) { returnFind (x,t->Right ); } returnT;} Position Findmax (Avltree T) {if(t!=NULL) { while(t->right!=NULL) {T=t->Right ; } } returnT;} Position findmin (Avltree T) {if(t!=NULL) { while(t->left!=NULL) {T=t->Left ; } } returnT;} Avltree singlerotatewithleft (Avltree T) {Position P; P=t->Left ; T->left=p->Right ; P->right=u; T->height=max (height (t->left), height (t->right)) +1; P->height=max (height (p->left), height (p->right)) +1; returnP;} Avltree singlerotatewithright (Avltree T) {Position P; P=t->Right ; T->right=p->Left ; P->left=T; T->height=max (height (t->left), height (t->right)) +1; P->height=max (height (p->left), height (p->right)) +1; returnP;} Avltree doublerotatewithleft (Avltree t) {T->left=singlerotatewithright (t->Left ); returnSinglerotatewithleft (T);} Avltree doublerotatewithright (Avltree t) {T->right=singlerotatewithleft (t->Right ); returnsinglerotatewithright (T);} Avltree Insert (ElementType x,avltree T) {if(t==NULL) {T=malloc(sizeof(structNode)); if(t==NULL) {printf ("Out of space"); Exit (1); }Else{T->left=t->right=NULL; T->height=0; }Else if(x<t->Element) {T->left=insert (x,t->Left ); if(Height (T->left)-height (t->right)) = =2){ if(x<t->left->Element) {T=Singlerotatewithleft (T); }Else{T=Doublerotatewithleft (T); } } }Else if(x>t->Element) {T->right=insert (x,t->Right ); if(Height (T->right)-height (t->left)) = =2){ if(x<t->right->Element) {T=doublerotatewithright (T); }Else{T=singlerotatewithright (T); } }} T->height=max (height (t->left), height (t->right)) +1; returnT; }}
Algorithm: implementation of AVL tree