You are on page 1of 8

/* Binary Tree With Mirror,Height,Level and Leaf */ #include<iostream.h> #include<conio.h> #include<stdlib.

h> class node { public: int data; node *lptr; node *rptr; };

class Btree { node* queue[30]; int f,r; public: node *root; int d1,d2; Btree(); void create(); void insert(node *,node *); void menu_display(void); void mirror(node *); void leafer(node *); int height(node *); void level(node *); void add(node *); node* remove(void); void preorder(node *); void inorder(node *); void postorder(node *); }; Btree::Btree() { root=NULL; d1=0; d2=0; f=-1;

r=-1; } //MAIN FUNCTION void main() { Btree b; clrscr(); b.create(); cout<<"\n"; b.menu_display(); cout<<"\n"; getche(); } //FUNCTION DEFINITIONS void Btree::create() { int value; node *temp; do { cout<<"\n Enter The Integer Value "; cin>>value; temp=new node; temp->data=value; temp->lptr=NULL; temp->rptr=NULL; if(root==NULL) { root=temp; } else { insert(root,temp); } cout<<"\n Do You Want To Insert More ?(y/n) ";

}while(getche()=='y'); } void Btree::insert(node *root,node *temp) { char ch,ans; node *temp2; temp2=root; if(root!=NULL) cout<<"\nCurrent Root Is "<<temp2->data; cout<<"\nInsert To Left(l) Or Right(r) ?"; ch=getche(); if(ch=='l'||ch=='L') { if(temp2->lptr==NULL) temp2->lptr=temp; else insert(temp2->lptr,temp); } else { if(ch=='r'||ch=='R') { if(temp2->rptr==NULL) temp2->rptr=temp; else insert(temp2->rptr,temp); } else { cout<<"\nInvalid Choice "; } } } void Btree::menu_display() { int ch,ht;

do { cout<<"\nDisplay Menu "; cout<<"\n1. Preorder(Recursive)"; cout<<"\n2. Inorder(Recursive)"; cout<<"\n3. Postorder(Recursive)"; cout<<"\n4. Mirror"; cout<<"\n5. Leaf Node Display"; cout<<"\n6. Height"; cout<<"\n7. Level Display"; cout<<"\nEnter Choice "; cin>>ch; switch(ch) { case 1: preorder(root); break; case 2: inorder(root); break; case 3: postorder(root); break; case 4: mirror(root); cout<<"Display Mirror Image In Which Order ?"; menu_display(); break; case 5: leafer(root); break; case 6: ht=height(root); cout<<ht; break; case 7: level(root); break; default: cout<<"\nInvalid Choice ";

menu_display(); } cout<<"\nDisplay Other Order ?(y/n) "; }while(getche()=='y'); } //FUNCTIONS FOR DISPLAYING BINARY TREE void Btree::preorder(node *temp) { if(temp!=NULL) { cout<<"\t "<<temp->data<<" "; preorder(temp->lptr); preorder(temp->rptr); } } void Btree::inorder(node *temp) { if(temp!=NULL) { inorder(temp->lptr); cout<<"\t "<<temp->data<<" "; inorder(temp->rptr); } } void Btree::postorder(node *temp) { if(temp!=NULL) { postorder(temp->lptr); postorder(temp->rptr); cout<<"\t "<<temp->data<<" "; } }

//Functions For Mirror,Height,Leaf,Level void Btree::mirror(node *temp) { node *temp1; //at each level mirror the left and right subtree //after you reach the leaves interchange the links //and recursively thus links get mirrored if(temp!=NULL) { mirror(temp->lptr); mirror(temp->rptr); cout<<temp->data<<"\n"; temp1=temp->lptr; temp->lptr=temp->rptr; temp->rptr=temp1; } } void Btree::leafer(node *root) { node *temp; temp=root; if(temp!=NULL) { if(temp->lptr!=NULL) leafer(temp->lptr); if(temp->rptr!=NULL) leafer(temp->rptr); //leaf node will have both links null if((temp->lptr==NULL)&&(temp->rptr==NULL)) { cout<<" "<<temp->data; } }

} int Btree::height(node *temp) { node *temp2; temp2=temp; if(temp2!=NULL) { //call for height function for each left and right subtree d1=height(temp2->lptr); d2=height(temp2->rptr); //at every level whichever subtree has greater height // that value is returned if(d1>d2) return (d1+1); else return (d2+1); } else return 0; } void Btree::level(node *temp) { add(temp); while(f!=r) { temp=remove(); if(temp!=NULL) { cout<<" "<<temp->data; if(temp->lptr!=NULL || temp->rptr!=NULL) { add(temp->lptr); add(temp->rptr); } } } } void Btree::add(node *temp)

{ queue[++r]=temp; } node* Btree::remove() { return queue[++f]; }

You might also like