Sie sind auf Seite 1von 9

#include <iostream.

h> template<class T> class avltree; template<class T> class avlnode { T element; avlnode *left; avlnode *right; int height; public: friend class avltree<T>; }; template<class T> class avltree { public: avlnode <T>*root; public : avltree() { root=NULL; } void insert(T,avlnode<T>*&p); void del(T, avlnode<T>* &p); T deletemin(avlnode<T>*&p); void find(T,avlnode<T>* &p); void preorder(avlnode<T>*&p); void inorder(avlnode<T>*&p); void postorder(avlnode<T>*&p); int bsheight(avlnode<T>*&p); avlnode<T>* srl(avlnode<T>*&p1); avlnode<T>*drl(avlnode<T>*&p1); avlnode<T>* srr(avlnode<T>*&p1); avlnode<T>* drr(avlnode<T>*&p1); int max(int,int); void menu(); void displaymenu(); }; template<class T> void avltree<T>::insert(T x,avlnode<T>*&p) { if (p == NULL) { p = new avlnode<T>; p->element = x; p->left=NULL; p->right = NULL; p->height=0; if (p==NULL) { cout<<"Out of Space\n"<<endl; } } else { if (x<p->element) { insert(x,p->left); if ((bsheight(p->left) - bsheight(p->right))==2)

{ if (x < p->left->element) { p=srl(p); } else { p = drl(p); } } } else if (x>p->element) { insert(x,p->right); if ((bsheight(p->right) - bsheight(p->left))==2) { if (x > p->right->element) { p=srr(p); } else { p = drr(p); } } } else { cout<<"Element Exists\n"<<endl; } } int m,n,d; m=bsheight(p->left); n=bsheight(p->right); d=max(m,n); p->height = d + 1; } template<class T> void avltree<T>::find(T x,avlnode<T>* &p) { if (p==NULL) { cout<<"Sorry! element not found\n"<<endl; } else { if (x < p->element) { find(x,p->left); } else if (x>p->element) { find(x,p->right); } else { cout<<"Element found!\n"<<endl; } } }

template<class T> void avltree<T>::del(T x,avlnode<T>* &p) { avlnode<T>* d; if (p==NULL) { cout<<"Sorry! element not found\n"<<endl; } else if ( x < p->element) { del(x,p->left); } else if (x > p->element) { del(x,p->right); } else if ((p->left == NULL) && (p->right == NULL)) { d=p; free(d); p=NULL; cout<<"Element deleted successfully\n"<<endl; } else if (p->left == NULL) { d=p; free(d); p=p->right; cout<<"Element deleted successfully\n"<<endl; } else if (p->right == NULL) { d=p; p=p->left; free(d); cout<<"Element deleted successfully\n"<<endl; } else { p->element = deletemin(p->right); } } template<class T> T avltree<T>::deletemin(avlnode<T>*&p) { T c; cout<<"inside deltemin\n"<<endl; if (p->left == NULL) { c=p->element; p=p->right; return c; } else { c=deletemin(p->left); return c; } } template<class T>

void avltree<T>::preorder(avlnode<T>* &p) { if (p!=NULL) { cout<<p->element<<"\t"; preorder(p->left); preorder(p->right); } } template<class T> void avltree<T>::inorder(avlnode<T>* &p) { if (p!=NULL) { inorder(p->left); cout<<p->element<<"\t"; inorder(p->right); } } template<class T> void avltree<T>::postorder(avlnode<T>* &p) { if (p!=NULL) { postorder(p->left); postorder(p->right); cout<<p->element<<"\t"; } } template<class T> int avltree<T>::max(int value1, int value2) { return ((value1 > value2) ? value1 : value2); } template<class T> int avltree<T>::bsheight(avlnode<T>*&p) { int t; if (p == NULL) { return -1; } else { t = p->height; return t; } } template<class T> avlnode<T>* avltree<T>:: srl(avlnode<T>*&p1) { cout<<"\nLL Rotation"<<endl; avlnode<T>*p2; p2 = p1->left; p1->left = p2->right; p2->right = p1; p1->height = max(bsheight(p1->left),bsheight(p1->right)) + 1; p2->height = max(bsheight(p2->left),p1->height) + 1; return p2; }

template<class T> avlnode<T>* avltree<T>:: srr(avlnode<T>*&p1) { cout<<"\nRR Rotation"<<endl; avlnode<T>* p2; p2 = p1->right; p1->right = p2->left; p2->left = p1; p1->height = max(bsheight(p1->left),bsheight(p1->right)) + 1; p2->height = max(p1->height,bsheight(p2->right)) + 1; return p2; } template<class T> avlnode<T>* avltree<T>:: drl(avlnode<T>*&p1) { cout<<"\nLR Rotation"<<endl; p1->left=srr(p1->left); return srl(p1); } template<class T> avlnode<T>* avltree<T>:: drr(avlnode<T>*&p1) { cout<<"\nRL Rotation"<<endl; p1->right = srl(p1->right); return srr(p1); } template<class T> void avltree<T>::menu() { cout<<"\n\t\t\tImplementation of AVL tree"; cout<<"\n\t\t\t**********************"; cout<<"\n 1. Insertion"; cout<<"\n 2. Deletion"; cout<<"\n 3. Search"; cout<<"\n 4. Display"; cout<<"\n 5. Exit"; } template<class T> void avltree<T>::displaymenu() { cout<<"\n\t\t\tDisplay"; cout<<"\n\t\t\t`~~~~~"; cout<<"\n\n1. Inorder"; cout<<"\n2. Preorder"; cout<<"\n3. Postorder"; cout<<"\n4. Exit"; } int main() { avltree<int>i; avltree<float>f; avltree<char>c; int ch,i1,i2,i3,ch1,ch2; float f1,f2,f3; char c1='\0',c2='\0',c3='\0'; do { cout<<"\nData Type"; cout<<"\n-----------"; cout<<"\n1. Integer";

cout<<"\n2. Float"; cout<<"\n3. Character"; cout<<"\n4. Exit"; cout<<"\nEnter the data type you want:"; cin>>ch1; switch(ch1) { case 1: do { i.menu(); cout<<"\nEnter your choice : "; cin>>ch; switch(ch) { case 1: cout<<"\nEnter the element to be inserted : "; cin>>i1; i.insert(i1,i.root); break; case 2: cout<<"\nEnter the element to be deleted : " ; cin>>i2; i.del(i2,i.root); break; case 3: cout<<"\nEnter the element to be search : "; cin>>i3; i.find(i3,i.root); break; case 4: do { i.displaymenu(); cout<<"\nEnter your choice:"; cin>>ch2; switch(ch2) { case 1: cout<<"\nThe contents of avl tree is"<<endl; i.inorder(i.root); break; case 2: cout<<"\nThe contents of avl tree is"<<endl; i.preorder(i.root); break; case 3: cout<<"\nThe contents of avl tree is"<<endl; i.postorder(i.root); break; case 4: break; } }while(ch2<4); break;

case 5:break; } }while(ch<5); break; case 2: do { f.menu(); cout<<"\nEnter your choice : "; cin>>ch; switch(ch) { case 1: cout<<"\nEnter the element to be inserted : "; cin>>f1; f.insert(f1,f.root); break; case 2: cout<<"\nEnter the element to be deleted : " ; cin>>f2; f.del(f2,f.root); break; case 3: cout<<"\nEnter the element to be search : "; cin>>f3; f.find(f3,f.root); break; case 4: do { f.displaymenu(); cout<<"\nEnter your choice:"; cin>>ch2; switch(ch2) { case 1: cout<<"\nThe contents of avl tree is"<<endl; f.inorder(f.root); break; case 2: cout<<"\nThe contents of avl tree is"<<endl; f.preorder(f.root); break; case 3: cout<<"\nThe contents of avl tree is"<<endl; f.postorder(f.root); break; case 4: break; } }while(ch2<4); break; case 5:break; } }while(ch<5);

break; case 3: do { c.menu(); cout<<"\nEnter your choice : "; cin>>ch; switch(ch) { case 1: cout<<"\nEnter the element to be inserted : "; cin>>c1; c.insert(c1,c.root); break; case 2: cout<<"\nEnter the element to be deleted : " ; cin>>c2; c.del(c2,c.root); break; case 3: cout<<"\nEnter the element to be search : "; cin>>c3; c.find(c3,c.root); break; case 4: do { c.displaymenu(); cout<<"\nEnter your choice:"; cin>>ch2; switch(ch2) { case 1: cout<<"\nThe contents of avl tree is"<<endl; c.inorder(c.root); break; case 2: cout<<"\nThe contents of avl tree is"<<endl; c.preorder(c.root); break; case 3: cout<<"\nThe contents of avl tree is"<<endl; c.postorder(c.root); break; case 4: break; } }while(ch2<4); break; case 5:break; } }while(ch<5); break; case 4:break; }

}while(ch1<4); return 0; }

Das könnte Ihnen auch gefallen