skb50bd

BST

Jul 6th, 2016
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.58 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node {
  5.     int Data;
  6.     struct Node *Left;
  7.     struct Node *Right;
  8. };
  9.  
  10. struct Node *insert(struct Node *Root, int X) {
  11.     if (Root == NULL) {
  12.         Root = new Node; //(struct Node *) malloc(sizeof(struct Node))
  13.         Root -> Data = X;
  14.         Root -> Left = NULL;
  15.         Root -> Right = NULL;
  16.     }
  17.     else {
  18.         if (Root -> Data > X) Root -> Left = insert(Root -> Left, X);
  19.         else if (Root -> Data < X) Root -> Right = insert(Root -> Right, X);
  20.     }
  21.     return Root;
  22. }
  23.  
  24. void preOrder(struct Node *Root) {
  25.     if (Root != NULL) {
  26.         cout << Root -> Data << " ";
  27.         preOrder(Root -> Left);
  28.         preOrder(Root -> Right);
  29.     }
  30. }
  31.  
  32. void inOrder(struct Node *Root) {
  33.     if (Root != NULL) {
  34.         inOrder(Root -> Left);
  35.         cout << Root -> Data << " ";
  36.         inOrder(Root -> Right);
  37.     }
  38. }
  39.  
  40. void postOrder(struct Node *Root) {
  41.     if (Root != NULL) {
  42.         postOrder(Root -> Left);
  43.         postOrder(Root -> Right);
  44.         cout << Root -> Data << " ";
  45.     }
  46. }
  47.  
  48. bool Search(struct Node *Root, int X) {
  49.     if (Root != NULL) {
  50.         if (Root -> Data == X) return true;
  51.         else {
  52.             if (Root -> Data > X) return Search(Root -> Left, X);
  53.             else return Search(Root -> Right, X);
  54.         }
  55.     }
  56.     else return false;
  57. }
  58.  
  59. struct Node *DeleteNodeLeft(struct Node *Root) {
  60.     if (Root -> Right != NULL) {
  61.         int temp = Root -> Data;
  62.         Root -> Data = Root -> Right -> Data;
  63.         Root -> Right -> Data = temp;
  64.         Root -> Right = DeleteNodeLeft(Root -> Right);
  65.     }
  66.     else if (Root -> Left != NULL) {
  67.         int temp = Root -> Data;
  68.         Root -> Data = Root -> Left -> Data;
  69.         Root -> Left -> Data = temp;
  70.         Root -> Left = DeleteNodeLeft(Root -> Left);
  71.     }
  72.     else {
  73.         delete Root;
  74.         return NULL;
  75.     }
  76. }
  77.  
  78. struct Node *DeleteNodeRight(struct Node *Root) {
  79.     if (Root -> Left != NULL) {
  80.         int temp = Root -> Data;
  81.         Root -> Data = Root -> Left -> Data;
  82.         Root -> Left -> Data = temp;
  83.         Root -> Left = DeleteNodeRight(Root -> Left);
  84.     }
  85.     else if (Root -> Right != NULL) {
  86.         int temp = Root -> Data;
  87.         Root -> Data = Root -> Right -> Data;
  88.         Root -> Right -> Data = temp;
  89.         Root -> Right = DeleteNodeRight(Root -> Right);
  90.     }
  91.     else {
  92.         delete Root;
  93.         return NULL;
  94.     }
  95. }
  96.  
  97.  
  98. struct Node *Delete(struct Node *Root, int X) {
  99.     for (struct Node *Parent = NULL, *Current = Root; Current; Parent = Current, Current = Current -> Data < X ? Current -> Right: Current -> Left) {
  100.         if (Current -> Data == X) {
  101.             if (Current == Root) {
  102.                 if (Root -> Left != NULL)
  103.                     Root = DeleteNodeLeft(Root);
  104.                 else if (Root -> Right != NULL)
  105.                     Root = DeleteNodeRight(Root);
  106.                 else {
  107.                     Root = NULL;
  108.                     delete Current;
  109.                 }
  110.             }
  111.             else {
  112.                 if (Parent -> Left == Current)
  113.                     Parent -> Left = DeleteNodeLeft(Current);
  114.                 else
  115.                     Parent -> Right = DeleteNodeRight(Current);
  116.             }
  117.         }
  118.     }
  119.     return Root;
  120. }
  121.  
  122.  
  123.  
  124. int main() {
  125.     int num, choice;
  126.     struct Node *Root = NULL;
  127.     while (true) {
  128.         cout << "Tree Operations" << endl;
  129.         cout << "---------------" << endl;
  130.         cout << "1. Insert" << endl;
  131.         cout << "2. PreOrder Travarse" << endl;
  132.         cout << "3. InOrder Travarse" << endl;
  133.         cout << "4. PostOrder Travarse" << endl;
  134.         cout << "5. Delete" << endl;
  135.         cout << "6. Search" << endl;
  136.         cout << "0. Exit" << endl;
  137.         cout << "Enter Your Choice: ";
  138.         cin >> choice;
  139.  
  140.         switch (choice) {
  141.             case 1:
  142.                 cout << "Enter the Number to Insert: ";
  143.                 cin >> num;
  144.                 Root = insert(Root, num);
  145.                 break;
  146.  
  147.             case 2:
  148.                 if (Root == NULL)
  149.                     cout << "Tree is Empty";
  150.                 else
  151.                     preOrder(Root);
  152.                 cout << endl;
  153.                 break;
  154.  
  155.             case 3:
  156.                 if (Root == NULL)
  157.                     cout << "Tree is Empty";
  158.                 else
  159.                     inOrder(Root);
  160.                 cout << endl;
  161.                 break;
  162.  
  163.             case 4:
  164.                 if (Root == NULL)
  165.                     cout << "Tree is Empty";
  166.                 else
  167.                     postOrder(Root);
  168.                 cout << endl;
  169.                 break;
  170.  
  171.             case 5:
  172.                 if (Root == NULL)
  173.                     cout << "Tree is Empty";
  174.                 else {
  175.                     cout << "Enter Number to Delete: ";
  176.                     cin >> num;
  177.                     if (Search(Root, num) == false)
  178.                         cout << num << " Not Found" << endl;
  179.                     else
  180.                         Root = Delete(Root, num);
  181.                 }
  182.                 break;
  183.  
  184.             case 6:
  185.                 cout << "Enter Number to Search: ";
  186.                 cin >> num;
  187.                 if (Search(Root, num) == true)
  188.                     cout << num << " Found" << endl;
  189.                 else
  190.                     cout << num << " Not Found" << endl;
  191.                 break;
  192.  
  193.             case 0:
  194.                 return 0;
  195.  
  196.             default:
  197.                 cout << "Invalid Choice" << endl;
  198.         }
  199.         cout << endl << endl;
  200.     }
  201.     return 0;
  202. }
Add Comment
Please, Sign In to add comment