#include<iostream.h> #include<conio.h> #include<process.h> struct tree_node { tree_node *left; tree_node *right; int data; } ; class bst { tree_node *root; public: bst() { root=NULL; } int isempty() { return(root==NULL); } void insert(int item); void inordertrav(); void inorder(tree_node *); void postordertrav(); void postorder(tree_node *); void preordertrav(); void preorder(tree_node *); }; void bst::insert(int item) { tree_node *p=new tree_node; tree_node *parent; p->data=item; p->left=NULL; p->right=NULL; parent=NULL; if(isempty()) root=p; else { tree_node *ptr; ptr=root; while(ptr!=NULL) { parent=ptr; if(item>ptr->data) ptr=ptr->right; else ptr=ptr->left; } if(item<parent->data) parent->left=p; else parent->right=p; } } void bst::inordertrav() { inorder(root); } void bst::inorder(tree_node *ptr) { if(ptr!=NULL) { inorder(ptr->left); cout<<" "<<ptr->data<<" "; inorder(ptr->right); } } void bst::postordertrav() { postorder(root); } void bst::postorder(tree_node *ptr) { if(ptr!=NULL) { postorder(ptr->left); postorder(ptr->right); cout<<" "<<ptr->data<<" "; } } void bst::preordertrav() { preorder(root); } void bst::preorder(tree_node *ptr) { if(ptr!=NULL) { cout<<" "<<ptr->data<<" "; preorder(ptr->left); preorder(ptr->right); } } void main() { bst b; b.insert(52); b.insert(25); b.insert(50); b.insert(15); b.insert(40); b.insert(45); b.insert(20); cout<<"inorder"<<endl; b.inordertrav(); cout<<endl<<"postorder"<<endl; b.postordertrav(); cout<<endl<<"preorder"<<endl; b.preordertrav(); getch(); } |
Description :
This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.
#Home #Sitemap #Resources #Terms of Use
Copyright©2012 electrofriends.com All Rights Reserved
Contact:[email protected]
cout<<"nice";
koncham cinnatha kudunga da
simple program pls
i want simple program on tree traversal
it’s very big so plz small prg
super!!!!!!easy to understand:)
the program is easy to understand
but,
please give the display for input and output
great & easily understandable code………………………………………..bt can it be like we enter the numbers(items) at runtime ???????????
This program is simple to understand but the value is inserted at root,Left child and right child is taking from user……..