Question: Write a C++ Function to Display the tree inputs in inorder, Preorder and Postorder please write a function to display my tree values accoring to

"Write a C++ Function to Display the tree inputs in inorder, Preorder and Postorder"

please write a function to display my tree values accoring to this program

I have already coded the other parts, please write a function to display the tree in three different ways( Inorder, Preorder and postorder)

#include

using namespace std;

struct tree { tree *l, *r; int data; } *root = NULL , *p=NULL; void BST ( ) { int Value; if (root == NULL) { root = new tree; cout<<"enter value of root node "; cin>>root->data; root->r=NULL; root->l=NULL; } else { p = root; cout<<"enter value of node "; cin>>Value; while(true) { if (Value < p->data) { if (p->l == NULL) { p->l = new tree; p = p->l; p->data = Value; p->l = NULL; p->r = NULL; break; } // if left null else if (p->l != NULL) { p = p->l; } // if left != null } // if value less than p

else if (Value > p->data) { if (p->r == NULL) { p->r = new tree; p = p->r; p->data = Value; p->l = NULL; p->r = NULL; break; }// if r=NULL else if (p->r != NULL) { p = p->r; }// if != NULL } // if grater than }// while }// else }

int main() { int Stop=1; char d; while(Stop==1) { BST(); cout<<" Do you Want to insert more node 'Y' or 'N' -> "; cin>>d; if(d=='Y' || d=='Y') { Stop=1; } else if( d=='N' || d=='n') Stop=0; }

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!