Traverse Tree According To User Value :Get Value From User ☻ :~C++ Code Tree

Traverse Tree According To User Value :Get Value From User ☻

#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 inorder(Node *root){
if(root == NULL) return;
else{
inorder(root->left);
cout <<root->data <<"  ";
inorder(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]); }
while(getch()!=30){

cout<<"\n\n\n\tSelect A Mode: \n\n1 for Inorder Treverse\n2 for PostOrder Treverse\n3 for PreOrder Treverse\n";
cin>>c;
switch(c){
case 1:{
cout<<"\n\nInorder: ";
b.inorder(b.root);
break;
}
case 2:
{
cout<<"\n\nPost Order: ";
b.postorder(b.root);
break;
}
case 3:
{
cout<<"\n\nPre Order: ";
b.preorder(b.root);
break;
}
default:{
}
}
cout<<"\n\n----------+--------------+---------------------+-------------\n\n";
cout<<"\n\nPress Space Bar To Exit || Enter To Continue \n\n";
}
getch();

}



No comments

Powered by Blogger.