Sie sind auf Seite 1von 7

//Q27. WAP to create a Binary Search tree and to perform various operations on the tree. #include<iostream.h> #include<conio.

h> #define max 100 class queue { public: int front,rear; int arr[max]; queue() { front=rear=-1; } void add(int info); int del(); }; void queue::add(int info) { if((rear==max-1) && (front==0) || rear+1==front) { cout<<"\nList Full"; return; }i f(rear==max-1) rear=0; else rear++; arr[rear]=info; if(front==-1) front=0; }i nt queue::del() { int f; if(front==-1) { return 0; } else { f=arr[front]; if(front==rear) front=rear=-1; else front++; return f; } } class binary:public queue { int data,i; binary *root,*p,*q,*q1,*p1; binary *left,*right; int count,count1; public: binary() { root=NULL;

count=count1=0; } void insert(); void traverse(); void breath(binary *); void inorder(binary *); void preorder(binary *); void postorder(binary *); void mirror(); binary* search(int,binary **); void remove(); void leaf(); }; void binary::insert() { cout<<"\nEnter value & Enter 0 to quit:"; cin>>i; while(i!=0) { if(root==NULL) { root=new binary; root->left=NULL; root->right=NULL; root->data=i; p=root; } else { p=root; q=new binary; q->left=NULL; q->right=NULL; while(p!=NULL) { if(i<p->data) { p1=p; p=p->left; } else if(i>=(p->data)) { p1=p; p=p->right; } }i f(i<p1->data) p1->left=q; else p1->right=q; p=q; p->data=i; p->left=NULL; p->right=NULL; } cout<<"\nEnter next value:"; cin>>i; } } void binary::traverse()

{ cout<<"\nInorder Traversal:"; inorder(root); cout<<"\nPreorder Traversal:"; preorder(root); cout<<"\nPostorder Traversal:"; postorder(root); cout<<"\nBreadth Traversal:"; breath(root); } void binary::inorder(binary *p) { if(p!=NULL) { inorder(p->left); cout<<p->data<<" "; inorder(p->right); } else return; } void binary::postorder(binary *p) { if(p!=NULL) { postorder(p->left); postorder(p->right); cout<<p->data<<" "; } else return; } void binary::breath(binary *p) { queue Queue; int i; if(p!=NULL) { Queue.add(p->data); while(i!=0) { i=Queue.del(); if(i!=0) cout<<i<<" "; p1=NULL; //searching the number p=search(i,&p1); if(p->left!=NULL) Queue.add(p->left->data); if(p->right!=NULL) Queue.add(p->right->data); } } } void binary::preorder(binary *p) { if(p!=NULL) { cout<<p->data<<" "; if((p->left!=NULL)||(p->right!=NULL))

count++; if((p->left==NULL)&&(p->right==NULL)) count1++; preorder(p->left); preorder(p->right); } else return; } void binary::mirror() { queue Queue; int i; p=root; if(p!=NULL) { Queue.add(p->data); while(i!=0) { i=Queue.del(); if(i!=0) cout<<i; p1=NULL; //searching the numbers p=search(i,&p1); if(p->right!=NULL) Queue.add(p->right->data); if(p->left!=NULL) Queue.add(p->left->data); } } } binary* binary::search(int i,binary **pre) { q=root; int found=0; while(q!=NULL) { if(q->data==i) { found=1; break; } *pre=q; if(q->data>i) q=q->left; else q=q->right; }i f(found==0) q=NULL; return q; } void binary::remove() { if(root==NULL) { cout<<"\nTree is empty"; return; }

q=root; cout<<"\nEnter the node want to delete:"; cin>>i; p1=NULL; //searching the number q=search(i,&p1); if(q==NULL) { cout<<"\nElement not found"; return; } getch(); clrscr(); //node to be deleted has no child if((q->right==NULL)&&(q->left==NULL)) { if(p1->right==q) p1->right=NULL; else p1->left=NULL; delete q; } //node to be deleted has no left child if((q->right!=NULL)&&(q->left==NULL)) { if(p1->right==q) p1->right=q->right; else p1->left=q->right; delete q; } //node to be deleted has no right child if((q->right==NULL)&&(q->left!=NULL)) { if(p1->right==q) p1->right=q->left; else p1->left=q->left; delete q; }i nt ch; if((q->right!=NULL)&&(q->left!=NULL)) { cout<<"\n1.Copying"; cout<<"\n2.Merging"; cin>>ch; if(ch==1) { cout<<"\nDeletion by copying\n"; p=q->left; q1=q; while(p->right!=NULL) { q1=p; p=p->right; } q->data=p->data; if(q1==q) q1->left=p->left; else

q1->right=p->left; delete p; } else { cout<<"\n\nDeletion by merging"; p=q->left; while(p->right!=NULL) p=p->right; p->right=q->right; p=q; if(p1->left==q) p1->left=q->left; if(p1->right==q) p1->right=q->left; if(q==root) root=q->left; q=q->left; delete p; } } traverse(); } void binary::leaf() { cout<<"\nTotal number of leaf nodes:"; cout<<count1; cout<<"\nTotal number of non leaf nodes:" ; cout<<count; cout<<"\nTotal number of nodes:"; cout<<(count+count1); } void main() { clrscr(); binary b1; int ch; char ch1; cout<<"\n1.Insert"; cout<<"\n2.Traverse"; cout<<"\n3.Calculate leaf"; cout<<"\n4.Mirror Image"; cout<<"\n5.Delete"; do { cout<<"Enter your choice:"; cin>>ch; switch(ch) { case 1: { b1.insert(); break; } case 2: { b1.traverse(); break; } case 3: {

b1.leaf(); break; } case 4: { cout<<"\nMirror image using Breadth first\n"; b1.mirror(); break; } case 5: { b1.remove(); break; } } cout<<"\nWant to continue?"; cin>>ch1; } while((ch1=='y')||(ch1=='Y')); getch(); }

Das könnte Ihnen auch gefallen