Friday, May 1, 2015

Traverse Left Subtree with Post Order and Right Sub tree with PreOrder ~ C++ Codes Tree

Traverse root left sub tree with post order ☻
and then print root ☻
and then root right sub tree with pre order ☻

#include<iostream.h>
#include<conio.h>
class Node{
private:
int data;
Node* right,*q,*p;
Node* left;
public:
Node(){
root=NULL;
right=left=NULL;
}Node *root;


void insert(int data){
Node* newnode = new Node();
newnode->data = data;
if (root == NULL){
root=newnode;
return;
}
Node *p,*q;
p=q=root;
while (q!=NULL){
p=q;
if (newnode->data> q->data)
q = q->right;
else q = q->left;
}
if (newnode->data> p->data)
p->right = newnode;
else p->left = newnode;
}




void mixorder(Node *root){
cout<<"\n\nPost Order: ";
postorder(root->left);
cout <<"\n\nRoot: "<<root->data <<"  ";
cout<<"\n\nPre Order: ";
preorder(root->right);
}


void preorder(Node *root){
if (root == NULL) return;
else {
cout << root->data <<"  ";
preorder(root->left);
preorder(root->right);
}
}

void postorder(Node *root){
if (root == NULL) return;
else {
postorder(root->left);
postorder(root->right);
cout << root->data <<"  ";
}
}



};

void main()
{
int c,A[]={9,4,3,2,5,6,8,1,33,12,44};
Node b;
clrscr();
for(int i=0;i<=10;i++){
b.insert(A[i]); }
cout<<"\n\n Traverse Left Subtree with Post Order and Right Sub tree with PreOrder\n\n";
b.mixorder(b.root);
cout<<"\n\n----------+--------------+---------------------+-------------\n\n";

getch();

}


Share this

0 Comment to "Traverse Left Subtree with Post Order and Right Sub tree with PreOrder ~ C++ Codes Tree"