Add to Favorites    Make Home Page 6919 Online  
 Language Categories  
 Our Services  

Home » C++ Home » Data Structures Home »

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title
Author ABHISHEK GUPTA
Author Email abhishek_kiki [at] yahoo.co.in
Description code to create binary tree,find mirror image of it,find
height,print original tree and mirrot image tree level wise,display
leaf
nodes
Category C++ » Data Structures
Hits 367699
Code Select and Copy the Code
#include<iostream.h> #include<conio.h> #include<stdlib.h> //header for standard library function exit(); typedef class bin_tree { public: int data,status; class bin_tree *lchild,*rchild; //pointers to class bin_tree bin_tree():data(0),lchild(NULL),rchild(NULL),status(0){}//constructor class bin_tree* createbt(class bin_tree*,int data); //create function void leafnodes(class bin_tree*); void display(class bin_tree*); class bin_tree* mirrorimage(class bin_tree*,class bin_tree*); void levelprint(class bin_tree*,int&); }nodebt;//typedef for simplicity typedef class queue { public: nodebt *data; class queue *next; queue():data(NULL),next(NULL){} nodebt* deletefront(queue **front,queue **rear); queue *insertrear(queue **front,queue **rear,nodebt *data); }que; void nodebt::levelprint(class bin_tree* r,int &height) { que q1; nodebt *check=NULL,*pt=r; que *front=NULL,*rear=NULL; q1.insertrear(&front,&rear,r); q1.insertrear(&front,&rear,check); while(front!=NULL)//q not empty { while(pt!=NULL) { pt=q1.deletefront(&front,&rear); if(pt==NULL) { cout<<endl; height++; q1.insertrear(&front,&rear,check); pt=q1.deletefront(&front,&rear); } if(front==NULL) return; cout<<pt->data<<" "; if(pt->lchild!=NULL) q1.insertrear(&front,&rear,pt->lchild); if(pt->rchild!=NULL) q1.insertrear(&front,&rear,pt->rchild); } } } nodebt* que::deletefront(que **front,que **rear) { nodebt *temp=NULL; if(*front==NULL) { cout<<" queue is empty"; } else { temp=(*front)->data; (*front)=(*front)->next; if((*front)==NULL) { *rear=NULL; } } return temp; } que *que::insertrear(que **front,que **rear,nodebt *data) { que *temp=NULL; temp=new queue; temp->data=data; temp->next=NULL; if(*front==NULL) { *front=temp; *rear=temp; } else { (*rear)->next=temp; *rear=(*rear)->next; } return *front; } nodebt* nodebt::mirrorimage(class bin_tree *r,class bin_tree* m) { nodebt *ret=NULL; if(r==NULL) return r; if(r->status==2) { nodebt *temp=new nodebt; temp->data=r->data; temp->status=2; m=temp; ret=temp; } if(r->status==0) { nodebt *temp=new nodebt; temp->data=r->data; temp->status=1; m->rchild=temp; m=m->rchild; } if(r->status==1) { nodebt *temp=new nodebt; temp->data=r->data; temp->status=0; m->lchild=temp; m=m->lchild; } mirrorimage(r->lchild,m); mirrorimage(r->rchild,m); return ret; } void nodebt::leafnodes(class bin_tree* r) { if(r==NULL) return; if(r->lchild==NULL && r->lchild==NULL) cout<<r->data<<" "; leafnodes(r->lchild); leafnodes(r->rchild); } void nodebt::display(class bin_tree* r) { if(r==NULL) return; cout<<" "<<r->data<<" "; display(r->lchild); display(r->rchild); } nodebt* nodebt::createbt(nodebt *r,int data) { int ch=0; if(r==NULL) { nodebt *temp=new nodebt; temp->data=data; temp->status=2; r=temp; } else { cout<<" 1.INSERT AT LEFT OF"<<" "<<r->data; cout<<" 2.INSERT AT RIGHT OF"<<" "<<r->data; cout<<" ENTER YOUR CHOICE="; cin>>ch; switch(ch) { case 1:if(r->lchild==NULL) { nodebt *temp=new nodebt; temp->data=data; temp->status=0; r->lchild=temp; } else createbt(r->lchild,data); break; case 2:if(r->rchild==NULL) { nodebt *temp=new nodebt; temp->data=data; temp->status=1; r->rchild=temp; } else createbt(r->rchild,data); break; } } return r; } int main() { int ch=0,data=0,height=0; nodebt *head=NULL,*m=NULL; nodebt t2; do { clrscr(); cout<<" MENU"; cout<<" 1.CREATE OR INSERT BINARY TREE"; cout<<" 2.PRINT LEAF NODES"; cout<<" 3.FIND MIRROR IMAGE OF THE TREE"; cout<<" 4.PRINT ORIGINAL AND MIRROR IMAGE LEVELWISE"; cout<<" 5.HEIGHT OF TREE"; cout<<" 6.EXIT"; cout<<" ENTER YOUR CHOICE="; cin>>ch; switch(ch) { case 1: nodebt t1; do { cout<<" ENTER THE DATA=";cin>>data; head=t1.createbt(head,data); cout<<" DO U WANT TO ADD MORE NODES(1.YES/2.NO)"; cout<<" ENTER YOUR CHOICE"; cin>>ch; }while(ch!=2); t1.display(head); break; case 2:t1.leafnodes(head); break; case 3:m=t2.mirrorimage(head,m); cout<<" MIRRORIMAGE TREE LEVELWISE "; height=0; t2.levelprint(m,height); break; case 4: cout<<" ORIGINAL TREE LEVELWISE "; t1.levelprint(head,height); m=t2.mirrorimage(head,m); cout<<" MIRRORIMAGE TREE LEVELWISE "; height=0; t2.levelprint(m,height); cout<<" HEIGHT OF BINARY TREE="<<height-1; break; case 5:cout<<" HEIGHT OF BINARY TREE="<<height-1; break; case 6:exit(1); } getch(); }while(ch!=6); return 1; }

Related Source Codes

Script Name Author
Moving ball screen saver karlmarx
The Classic Game of Snake & Ladder Lakshmi Narayana .A
Railway seat reservation question which comes in sapient VyomWorld
To calculate percentile Ravi Mathur
Send to folder ANIMESH SAHU
Analog clock and calendar Nazia & Rida
HIGH/LOW GAME MOLLY ARORA
Data structure (stack Implimentation) Swapnil B Adsure
Memory Game AnirudhSanyal
Easy Calc Anirudh Sanyal
GK Quiz Anirudh Sanyal
Hangman Game Manish Jain
Snakeman Manish Jain
Full month Calendar Nigi
Cursor shapes nigi

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=1178


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review