Advertisement
skb50bd

BinarySearchTree

Jun 27th, 2016
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. struct Node {
  6.     int Data;
  7.     struct Node *Left;
  8.     struct Node *Right;
  9. };
  10.  
  11. struct Node *insert(struct Node *Root, int x) {
  12.     if (Root == NULL) {
  13.         Root = new Node; //(struct Node *) malloc(sizeof(struct Node))
  14.         Root -> Data = x;
  15.         Root -> Left = NULL;
  16.         Root -> Right = NULL;
  17.     }
  18.     else {
  19.         if (Root -> Data > x)
  20.             Root -> Left = insert(Root -> Left, x);
  21.         else if (Root -> Data < x)
  22.             Root -> Right = insert(Root -> Right, x);
  23.     }
  24.     return Root;
  25. }
  26.  
  27. void preOrder(struct Node *Root) {
  28.     if (Root != NULL) {
  29.         cout << Root -> Data << " ";
  30.         preOrder(Root -> Left);
  31.         preOrder(Root -> Right);
  32.     }
  33. }
  34.  
  35. void inOrder(struct Node *Root) {
  36.     if (Root != NULL) {
  37.         inOrder(Root -> Left);
  38.         cout << Root -> Data << " ";
  39.         inOrder(Root -> Right);
  40.     }
  41. }
  42.  
  43. void postOrder(struct Node *Root) {
  44.     if (Root != NULL) {
  45.         postOrder(Root -> Left);
  46.         postOrder(Root -> Right);
  47.         cout << Root -> Data << " ";
  48.     }
  49. }
  50.  
  51. int main() {
  52.     int num, choice;
  53.     struct Node *Root = NULL;
  54.     while (true) {
  55.         cout << "Tree Operations" << endl;
  56.         cout << "---------------" << endl;
  57.         cout << "1. Insert" << endl;
  58.         cout << "2. PreOrder Travarse" << endl;
  59.         cout << "3. InOrder Travarse" << endl;
  60.         cout << "4. PostOrder Travarse" << endl;
  61.         cout << "0. Exit" << endl;
  62.         cout << "Enter Your Choice: ";
  63.         cin >> choice;
  64.  
  65.         switch (choice) {
  66.             case 1: {
  67.                 cout << "Enter the Number to Insert: ";
  68.                 cin >> num;
  69.                 Root = insert(Root, num);
  70.                 break;
  71.             }
  72.             case 2: {
  73.                 if (Root == NULL)
  74.                     cout << "Tree is Empty";
  75.                 else
  76.                     preOrder(Root);
  77.                 cout << endl;
  78.                 break;
  79.             }
  80.             case 3: {
  81.                 if (Root == NULL)
  82.                     cout << "Tree is Empty";
  83.                 else
  84.                     inOrder(Root);
  85.                 cout << endl;
  86.                 break;
  87.             }
  88.             case 4: {
  89.                 if (Root == NULL)
  90.                     cout << "Tree is Empty";
  91.                 else
  92.                     postOrder(Root);
  93.                 cout << endl;
  94.                 break;
  95.             }
  96.             case 0: {
  97.                 exit(0);
  98.             }
  99.             default: {
  100.                 cout << "Invalid Choice" << endl;
  101.             }
  102.         }
  103.         cout << endl << endl;
  104.     }
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement