Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # include <iostream>
- # include <cstdlib>
- 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 doubleOrder(node *);
- void display(node *, int);
- BST()
- {
- root = NULL;
- }
- };
- /*
- * Main Contains Menu
- */
- int main()
- {
- int choice, num;
- BST bst;
- node *temp;
- while (1)
- {
- cout << "-----------------" << endl;
- cout << "Operations on BST" << endl;
- cout << "-----------------" << endl;
- cout << "1.Insert Element " << endl;
- cout << "2.Double-Order Traversal" << endl;
- cout << "3.Display" << endl;
- cout << "4.Quit" << endl;
- cout << "Enter your choice : ";
- cin >> 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 << "Double-Order Traversal of BST:" << endl;
- bst.doubleOrder(root);
- cout << endl;
- break;
- case 3:
- cout << "Display BST:" << endl;
- bst.display(root, 1);
- cout << endl;
- break;
- case 4:
- exit(1);
- default:
- cout << "Wrong choice" << endl;
- }
- }
- }
- /*
- * Inserting Element into the Tree
- */
- void BST::insert(node *tree, node *newnode)
- {
- if (root == NULL)
- {
- root = new node;
- root->info = newnode->info;
- root->left = NULL;
- root->right = NULL;
- cout << "Root Node is Added" << endl;
- return;
- }
- if (tree->info == newnode->info)
- {
- cout << "Element already in the tree" << endl;
- return;
- }
- if (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" << endl;
- return;
- }
- }
- else
- {
- if (tree->right != NULL)
- {
- insert(tree->right, newnode);
- }
- else
- {
- tree->right = newnode;
- (tree->right)->left = NULL;
- (tree->right)->right = NULL;
- cout << "Node Added To Right" << endl;
- return;
- }
- }
- }
- /*
- * In Order Traversal
- */
- void BST::doubleOrder(node *ptr)
- {
- if (root == NULL)
- {
- cout << "Tree is empty" << endl;
- return;
- }
- if (ptr != NULL)
- {
- cout << ptr->info << " ";
- doubleOrder(ptr->left);
- cout << ptr->info << " ";
- doubleOrder(ptr->right);
- }
- }
- /*
- * Display Tree Structure
- */
- void BST::display(node *ptr, int level)
- {
- int i;
- if (ptr != NULL)
- {
- display(ptr->right, level + 1);
- cout << endl;
- if (ptr == root)
- cout << "Root->: ";
- else
- {
- for (i = 0; i < level; i++)
- cout << " ";
- }
- cout << ptr->info;
- display(ptr->left, level + 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement