/* * C++ Program To Implement BST */ # include # include using namespace std; /* * Node Declaration */ struct node { int info; struct node *left; struct node *right; }*root; /* * Class Declaration */ class BST { public: void insert(node *, node *); void preorder(node *); void inorder(node *); void postorder(node *); void display(node *, int); BST() { root = NULL; } }; /* * Main Contains Menu */ int main() { int choice, num; BST bst; node *temp; while (1) { cout<<"-----------------"<>choice; switch(choice) { case 1: temp = new node; cout<<"Enter the number to be inserted : "; cin>>temp->info; bst.insert(root, temp); break; case 2: cout<<"Inorder Traversal of BST:"<info = newnode->info; root->left = NULL; root->right = NULL; cout<<"Root Node is Added"<info == newnode->info) { cout<<"Element already in the tree"<info > newnode->info) { if (tree->left != NULL) { insert(tree->left, newnode); } else { tree->left = newnode; (tree->left)->left = NULL; (tree->left)->right = NULL; cout<<"Node Added To Left"<right != NULL) { insert(tree->right, newnode); } else { tree->right = newnode; (tree->right)->left = NULL; (tree->right)->right = NULL; cout<<"Node Added To Right"<info<<" "; preorder(ptr->left); preorder(ptr->right); } } /* * In Order Traversal */ void BST::inorder(node *ptr) { if (root == NULL) { cout<<"Tree is empty"<left); cout<info<<" "; inorder(ptr->right); } } /* * Postorder Traversal */ void BST::postorder(node *ptr) { if (root == NULL) { cout<<"Tree is empty"<left); postorder(ptr->right); cout<info<<" "; } } /* * Display Tree Structure */ void BST::display(node *ptr, int level) { int i; if (ptr != NULL) { display(ptr->right, level+1); cout<: "; else { for (i = 0;i < level;i++) cout<<" "; } cout<info; display(ptr->left, level+1); } }