Feedjit

Articles for you

Tuesday, December 31, 2013

Preorder, Inorder, PostOrder C++ Plus Plus Program in Object Oriented Program Binary Search Tree BST in C++ Preorder, Inorder, PostOrder

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<iostream>
using namespace std;
struct btnode
{
 btnode *left, *right;  int info;
 btnode() { left =0; right= 0;info=0; }

 btnode(int i,btnode *l=0,btnode*r =0)
 {
  info =i;left=l;right=r; } };
////////////////////////////////////////

class binarytree 
{  btnode *root; 
public:
    btnode * &rroot() { return root; }
 binarytree() {  root = NULL; }

 void preorder( btnode *b );
    void inorder( btnode *b );
    void postorder( btnode *b ); 
    void insert(int item);
 void insert(int item,btnode *&bt); };
////////////////////////////////////////

void binarytree :: preorder(btnode *bt)
{  // cout<<"Now Showing From Pre-order"<<endl;
 if(! bt ==0)
 {
  cout<<" "<<bt->info;
  preorder(bt->left);
  
  preorder(bt->right); }
//cout<<endl; 
}
////////////////////////////////////////
void binarytree::inorder(btnode *bt)
{  
 if(! bt ==0) 

{ inorder(bt->left);
  cout<<" "<<bt->info;
  inorder(bt->right); } 
}
///////////////////////////////////////
void binarytree:: postorder(btnode *bt)
{ 
 if(! bt==0)
 { postorder(bt->left);
      postorder(bt->right);
      cout<<" "<<bt->info; }  
}
//////////////////////////////////////
void binarytree::insert(int item)
{  if(root==0)
{ root=new btnode(item); }
else{ btnode*cur = root, *pre; 
           while(cur!=0)
     { pre = cur;
     if(item<cur->info)
      cur=cur->left;
 else if( item> cur->info)

  cur=cur->right;  }
if(pre->info>item)

 pre->left=new btnode(item);
else
pre-> right= new btnode(item); } };
//////////////////////////////////////////
 void binarytree::insert(int item,btnode* &bt)
   {

    if(bt==0)

     bt = new btnode(item);


    else if( item<bt->info)
      insert(item,bt->left);

 else
 
  insert( item,bt->right); }
////////////////////////////////////////////

void main()
{
 binarytree bst;

bst.insert(37,bst.rroot());
bst.insert(8);
bst.insert(9);
bst.insert(5);
bst.insert(55);
bst.insert(39);
bst.insert(58);
bst.insert(72);
bst.insert(65);
bst.insert(18);
bst.insert(15);

cout<<"Now Showing From Pre-order"<<endl;
bst.preorder(bst.rroot());
cout<<endl;
cout<<"Now Showing From In-order"<<endl;
bst.inorder(bst.rroot());
cout<<endl;
cout<<"Now Showing From Post-order"<<endl;
bst.postorder(bst.rroot());
cout<<"\t\t\t\t\t\t\t"<<endl;
} 

Read More

Articles for you