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

Home » C++ Home » Data Structures Home » BINARY SEARCH TREE

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

Search Projects & Source Codes:

Title BINARY SEARCH TREE
Author Aniket Kadu
Author Email aniket_kadu [at] rediffmail.com
Description Binary search tree with all the three recursive and non
recursive traversals

Category C++ » Data Structures
Hits 28515
Code Select and Copy the Code
/*Binary search tree with all the Recursive and non Recursive traversals*/ /* By:- Aniket P. Kadu :- S.e computer :- M.E.S.C.O.E */ #include<iostream.h> #include<conio.h> #include<stdlib.h> class binarynode { public: int data; binarynode *left; binarynode *right; }; class binsrctree { private: binarynode *root; void inorder_rec(binarynode *); void inorder_non_rec(binarynode *); void preorder_rec(binarynode *); void preorder_non_rec(binarynode *); void postorder_rec(binarynode *); void postorder_non_rec(binarynode *); public: binsrctree() { root=NULL; } void insert(int ); void print_inorder_rec(); void print_inorder_non_rec(); void print_preorder_rec(); void print_preorder_non_rec(); void print_postorder_rec(); void print_postorder_non_rec(); }; class stack { int top; binarynode *stackel[20]; public: stack() { top=-1; } void push(binarynode *); binarynode* pop(); int empty() { if(top==-1) return(1); return(0); } }; void stack::push(binarynode *node) { stackel[++top]=node; } binarynode *stack::pop() { return(stackel[top--]); } class stack_int { int top; int stack_int[20]; public: stack_int() { top=-1; } void push(int flag); int pop(); int empty_int() { if(top==-1) return(1); return(0); } }; void stack_int::push(int flag) { stack_int[++top]=flag; } int stack_int::pop() { return(stack_int[top--]); } /*---------------------------------------------------------------------*/ /* fUNCTION TO INSERT A NODE IN THE TREE */ /*---------------------------------------------------------------------*/ void binsrctree::insert(int val) { binarynode *temp,*prev,*curr; temp=new binarynode; temp->data=val; temp->left=temp->right=NULL; if(root==NULL) { root=temp; } else { curr=root; while(curr!=NULL) { prev=curr; if(temp->data<curr->data) curr=curr->left; else curr=curr->right; } if(temp->data<prev->data) prev->left=temp; else prev->right=temp; } } /* ------------------------------------------------*/ /*INORDER RECURSIVE TRAVERSAL*/ /*-------------------------------------------------*/ void binsrctree::inorder_rec(binarynode *root) { if(root!=NULL) { inorder_rec(root->left); cout<<root->data<<" "; inorder_rec(root->right); } } /*--------------------------------------------------*/ /*INORDER NON RECURSIVE TRAVERSAL*/ /*--------------------------------------------------*/ void binsrctree::inorder_non_rec(binarynode *root) { stack stk; binarynode *temp; if(root!=NULL) { temp=root; do { while(temp!=NULL) { stk.push(temp); temp=temp->left; }/*end while*/ if(!stk.empty()) { temp=stk.pop(); cout<<temp->data<<" "; temp=temp->right; }/*end if*/ else break; }while(1); /*end do while*/ }/* end if */ else cout<<" Empty tree"; } /*end function */ /*--------------------------------------------------*/ /*PREORDER RECURSIVE TRAVERSAL */ /*---------------------------------------------------*/ void binsrctree::preorder_rec(binarynode *root) { if(root!=NULL) { cout<<root->data<<" "; preorder_rec(root->left); preorder_rec(root->right); } } /*--------------------------------------------------*/ /*PREORDER NON RECURSIVE TRAVERSAL */ /*---------------------------------------------------*/ void binsrctree::preorder_non_rec(binarynode *root) { stack stk; binarynode *temp=root; stk.push(temp); while(!stk.empty()) { temp=stk.pop(); if(temp!=NULL) { cout<<temp->data<<" "; stk.push(temp->right); stk.push(temp->left); } } } /*--------------------------------------------------*/ /*POSTRDER RECURSIVE TRAVERSAL */ /*---------------------------------------------------*/ void binsrctree::postorder_rec(binarynode *root) { if(root!=NULL) { postorder_rec(root->left); postorder_rec(root->right); cout<<root->data<<" "; } } /*--------------------------------------------------*/ /*POSTORDER NON RECURSIVE TRAVERSAL */ /*---------------------------------------------------*/ void binsrctree::postorder_non_rec(binarynode *root) { stack stk; stack_int stk1; int flag; binarynode *temp=root; do { if(temp!=NULL) { stk.push(temp); stk1.push(1); temp=temp->left; } else { if(stk.empty()) break; temp=stk.pop(); flag=stk1.pop(); if(flag==2) { cout<<temp->data; temp=NULL; } /*end if */ else { stk.push(temp); stk1.push(2); temp=temp->right; } /* end else */ } /* end if */ }while(1);/*end do while*/ }/*end function*/ /*--------------------------------------------------*/ /*FUNCTION TO PRINT INORDER RECURSIVE TRAVERSAL */ /*---------------------------------------------------*/ void binsrctree::print_inorder_rec() { cout<<" "; inorder_rec(root); cout<<" "; } /*--------------------------------------------------*/ /*FUNCTION TO PRINT INORDER NON RECURSIVE TRAVERSAL */ /*---------------------------------------------------*/ void binsrctree::print_inorder_non_rec() { cout<<" "; inorder_non_rec(root); cout<<" "; } /*--------------------------------------------------*/ /*FUNCTION TO PRINT PREORDER RECURSIVE TRAVERSAL */ /*---------------------------------------------------*/ void binsrctree::print_preorder_rec() { cout<<" "; preorder_rec(root); cout<<" "; } /*--------------------------------------------------*/ /*FUNCTION TO PRINT PREORDER NON RECURSIVE TRAVERSAL */ /*---------------------------------------------------*/ void binsrctree::print_preorder_non_rec() { cout<<" "; preorder_non_rec(root); cout<<" "; } /*--------------------------------------------------*/ /*FUNCTION TO PRINT POSTORDER RECURSIVE TRAVERSAL */ /*---------------------------------------------------*/ void binsrctree::print_postorder_rec() { cout<<" "; postorder_rec(root); cout<<" "; } /*--------------------------------------------------*/ /*FUNCTION TO PRINT POSTORDER NON RECURSIVE TRAVERSAL */ /*---------------------------------------------------*/ void binsrctree::print_postorder_non_rec() { cout<<" "; postorder_non_rec(root); cout<<" "; } /*--------------------------------------------------*/ /* MAIN FUNCTION */ /*---------------------------------------------------*/ void main() { binsrctree BST; int ch,element; clrscr(); do { cout<<" 1.Insert a node in binary tree"; cout<<" 2.Recursive Inorder traversal"; cout<<" 3.Non Recursive Inorder traversal"; cout<<" 4.Recursive preorder traversal"; cout<<" 5.Non recursive preorder traversal"; cout<<" 6.Recursive postorder traversal"; cout<<" 7.Non recursive postorder traversal"; cout<<" 8.Exit"; cout<<" Enter your choice"; cin>>ch; switch(ch) { case 1: cout<<" Enter the element you wnat to insert"; cin>>element; BST.insert(element); break; case 2: cout<<" Recursive Inorder traversal "; BST.print_inorder_rec(); break; case 3: cout<<" NonRecursive Inorder traversal "; BST.print_inorder_non_rec(); break; case 4: cout<<" Recursive preorder traversal "; BST.print_preorder_rec(); break; case 5: cout<<" Non recursive preorder traversal "; BST.print_preorder_non_rec(); break; case 6: cout<<" Recursive postorder traversal"; BST.print_postorder_rec(); break; case 7: cout<<" Non recursive postorder traversal "; BST.print_postorder_non_rec(); break; case 8: exit(1); } }while(ch<8); }

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
Copyright ©2003-2017 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=1158


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