Anda di halaman 1dari 10

AVL TREE

EX.NO. : DATE : AIM To write a C++ program to perform implementation of AVL tree and its operations. ALGORITHM STEP 1: Define a class avlnode and declare the data members. STEP 2: Define another class avltree and declare the member functions. STEP 3: Declare the class avltree as a friend class of avlnode. STEP 4: There are two operations performed namely insert and display. STEP 5: Display the operations in the main function and get choice of operation from the user. STEP 6: In the insert operation, first check whether the tree is null or not. STEP 7: If the tree is null, create a new node and insert the value of the node. STEP 8: Else, check whether the element to be inserted is lesser than or greater than the value of the node pointed. STEP 9: If the element is lesser, insert into the left of the node pointed. Else insert into the right of the node pointed. STEP 10: After insertion, check whether the height of the tree is balanced. If not, perform rotate operations. There are four rotate operations namely rotate with left, rotate with right, double rotate with left and double rotate with right. STEP 11: In the display operation, display the elements in the AVL tree. STEP 12: In the exit operation, terminate the execution of the program.

PROGRAM //**** AVL Tree **** #include<iostream.h> #include<stdlib.h> #include<conio.h> #include<math.h> class node { int data; int balance; public: node *left,*right; void create(); node* insert(int x,node* avl); void findmin(node *); void findmax(node *); void ascend(node *); int height(node *current); void bal(node *current); node* rotatewithleftchild(node* avl); node* rotatewithrightchild(node* avl); node* doublewithleftchild(node* avl); node* doublewithrightchild(node* avl); void search(int p,node *); }*list,*root,*temp,*l,*ord,*parent; int h,or=0; void node::create() { list=NULL; list->balance=NULL; list->left=NULL; list->right=NULL; l=list; root=list; } int node::height(node* current) { int hleft=0,hright=0; if(current==NULL) return(0);

hleft=height(current->left)+1; hright=height(current->right)+1; if(hleft>hright) h=hleft; else h=hright; return(h); } node* node::insert(int num,node* avl) { if(avl==NULL) { avl=new(node); avl->data=num; avl->balance=0; avl->left=NULL; avl->right=NULL; return(avl); } else if(num<avl->data) { avl->left=insert(num,avl->left); if(height(avl->left)-height(avl->right)==2) if(num<avl->left->data) { cout<<"\nL ROTATION\n"; avl=rotatewithleftchild(avl); return(avl); } else { cout<<"\nRL ROTATION\n"; avl=doublewithleftchild(avl); } } else if(avl->data<num) { avl->right=insert(num,avl->right); if(height(avl->right)-height(avl->left)==2) if(avl->right->data<num) { cout<<"\nR ROTATION\n"; avl=rotatewithrightchild(avl);

} else { cout<<"\nLR ROTATION\n"; avl=doublewithrightchild(avl); } } else cout<<"\n\n\tThe entered element is already exist.........."; return(avl); } node* node::rotatewithleftchild(node* k2) { node* k1; k1=k2->left; k2->left=k1->right; k1->right=k2; return k1; } node* node::rotatewithrightchild(node* k2) { node*k1; k1=k2->right; k2->right=k1->left; k1->left=k2; return k1; } node* node::doublewithleftchild(node* k1) { k1->left=rotatewithrightchild(k1->left); k1=rotatewithleftchild(k1); return k1; } node* node::doublewithrightchild(node *k1) { k1->right=rotatewithleftchild(k1->right); k1=rotatewithrightchild(k1); return k1; }

void node::bal(node* current) { if(current->left!=NULL) bal(current->left); current->balance=height(current->left)-height(current->right); if(current->right!=NULL) bal(current->right); } void node::ascend(node *r) { if(r==NULL) return; else { ascend(r->left); if(r->data) { cout<<r->data; if(r->left==NULL) cout<<"\t0"; else cout<<"\t"<<r->left->data; if(r->right==NULL) cout<<"\t0\n"; else cout<<"\t"<<r->right->data<<"\n"; } ascend(r->right); } } void node::findmin(node *traverse) { if(traverse->left!=NULL) findmin(traverse->left); else cout<<"\n\n\tThe minimum element is "<<traverse->data; } void node::findmax(node *traverse) { if(traverse->right!=NULL) findmax(traverse->right); else cout<<"\n\n\tThe maximum element is "<<traverse->data;

} void node::search(int x,node *traverse) { if(traverse==NULL) { cout<<"\n\n\tThe element "<<x<<" is not in the tree...."; return; } else if(traverse->data==x) { cout<<"\n\n\tThe element "<<x<<" is in the tree....."; return; } else if(traverse->data>x) search(x,traverse->left); else search(x,traverse->right); } int main() { int n,e=1; node list1; list1.create(); clrscr(); while(e) { cout<<"\n ************"; cout<<"\n * AVL TREE *"; cout<<"\n ************\n"; cout<<" 1.INSERT\n 2.SEARCH\n 3.FIND MINIMUM\n"; cout<<" 4.FIND MAXIMUM\n 5.DISPLAY\n 6.EXIT"; cout<<"\n Enter Your Choice : "; cin>>n; int b,c; switch(n) { case 1: cout<<"\n Enter the element to be inserted....: "; cin>>b; root=list1.insert(b,root); list1.bal(root); break;

case 2: if(root==NULL) cout<<"\n Empty tree.......:"; else { cout<<"\n Enter the element to be searched...: "; cin>>b; list1.search(b,root); } break; case 3: if(root==NULL) cout<<"\n Empty tree.......:"; else list1.findmin(root); break; case 4: if(root==NULL) cout<<"\n Empty tree.......: "; else list1.findmax(root); break; case 5: clrscr(); cout<<"ROOT\t LEFT\t RIGHT\n"; list1.ascend(root); getch(); break; case 6: e=0; break; } } return 0; }

OUTPUT AVL TREE ************ 1.INSERT 2.SEARCH 3.FIND MINIMUM 4.FIND MAXIMUM 5.DISPLAY 6.EXIT Enter Your Choice : 1 Enter the element to be inserted....: 2 ************ * AVL TREE * ************ 1.INSERT 2.SEARCH 3.FIND MINIMUM 4.FIND MAXIMUM 5.DISPLAY 6.EXIT Enter Your Choice : 1 Enter the element to be inserted....: 3 ************ * AVL TREE * ************ 1.INSERT 2.SEARCH 3.FIND MINIMUM 4.FIND MAXIMUM 5.DISPLAY 6.EXIT Enter Your Choice : 1 Enter the element to be inserted....: 4

* AVL TREE * ************ 1.INSERT 2.SEARCH 3.FIND MINIMUM 4.FIND MAXIMUM 5.DISPLAY 6.EXIT Enter Your Choice : 2 Enter the element to be searched...: 3 The element 3 is in the tree..... ************ * AVL TREE * ************ 1.INSERT 2.SEARCH 3.FIND MINIMUM 4.FIND MAXIMUM 5.DISPLAY 6.EXIT Enter Your Choice : 3 The minimum element is 2 ************ * AVL TREE * ************ 1.INSERT 2.SEARCH 3.FIND MINIMUM 4.FIND MAXIMUM 5.DISPLAY 6.EXIT Enter Your Choice : 4 The maximum element is 4

************ * AVL TREE * ************ 1.INSERT 2.SEARCH 3.FIND MINIMUM 4.FIND MAXIMUM 5.DISPLAY 6.EXIT Enter Your Choice :5 ROOT 2 0 3 2 4 0 LEFT RIGHT 0 4 0

************ * AVL TREE * ************ 1.INSERT 2.SEARCH 3.FIND MINIMUM 4.FIND MAXIMUM 5.DISPLAY 6.EXIT Enter Your Choice : 6

RESULT Hence the program is executed successfully and the output is verified.

Anda mungkin juga menyukai