SkrillexOMG

BST Order

Apr 16th, 2022
1,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct BstNode
  5. {
  6.     int data;
  7.  
  8.     BstNode* left;
  9.  
  10.     BstNode* right;
  11. };
  12.  
  13. BstNode* root;
  14.  
  15. BstNode* GetNewNode(int data)
  16. {
  17.     BstNode* NewNode = new BstNode();
  18.  
  19.     NewNode->data = data;
  20.  
  21.     NewNode->left = NULL;
  22.  
  23.     NewNode->right = NULL;
  24.  
  25.     return NewNode;
  26. }
  27.  
  28. void PreOrder(BstNode* root)
  29.  
  30. {
  31.     if (root == NULL)
  32.  
  33.     {
  34.         return;
  35.     }
  36.  
  37.     cout << root->data << " ";
  38.  
  39.     PreOrder(root->left);
  40.  
  41.     PreOrder(root->right);
  42. }
  43.  
  44. void InOrder(BstNode* root) {
  45.     if (root == NULL)
  46.  
  47.     {
  48.         return;
  49.     }
  50.     InOrder(root->left);
  51.  
  52.     cout << root->data << " ";
  53.  
  54.     InOrder(root->right);
  55. }
  56.  
  57. void PostOrder(BstNode* root) {
  58.     if (root == NULL)
  59.  
  60.     {
  61.         return;
  62.     }
  63.     PostOrder(root->left);
  64.     PostOrder(root->right);
  65.     cout << root->data << " ";
  66. }
  67.  
  68. BstNode* Insert(BstNode* root, int data)
  69.  
  70. {
  71.     if (root == NULL)
  72.  
  73.     {
  74.         root = GetNewNode(data);
  75.     }
  76.  
  77.     else if (data <= root->data)
  78.  
  79.     {
  80.         root->left = Insert(root->left, data);
  81.     }
  82.  
  83.     else
  84.  
  85.     {
  86.         root->right = Insert(root->right, data);
  87.     }
  88.  
  89.     return root;
  90. }
  91.  
  92. bool Search(BstNode* root, int data)
  93.  
  94. {
  95.     if (root == NULL)
  96.  
  97.     {
  98.         cout << "Error: tree is empty" << endl;
  99.  
  100.         return false;
  101.     }
  102.  
  103.     else if (root->data == data)
  104.  
  105.     {
  106.         return true;
  107.     }
  108.  
  109.     else if (data <= root->data)
  110.  
  111.     {
  112.         return Search(root->left, data);
  113.     }
  114.  
  115.     else
  116.  
  117.     {
  118.         return Search(root->right, data);
  119.     }
  120. }
  121.  
  122. int main()
  123.  
  124. {
  125.     root = NULL;
  126.  
  127.     root = Insert(root, 5);
  128.  
  129.     root = Insert(root, 6);
  130.  
  131.     root = Insert(root, 4);
  132.  
  133.     root = Insert(root, 7);
  134.  
  135.     root = Insert(root, 3);
  136.  
  137.     root = Insert(root, 2);
  138.  
  139.     /*cout << "please enter your search item: ";
  140.  
  141.     int s;
  142.  
  143.     cin >> s;
  144.  
  145.     cout << endl;*/
  146.  
  147.     /*if (Search(root, s) == true)
  148.     {
  149.         cout << "found" << endl;
  150.     }
  151.  
  152.     else
  153.     {
  154.         cout << "Not found" << endl;
  155.     }*/
  156.     cout << "Pre:" << endl;
  157.     PreOrder(root);
  158.     cout << endl;
  159.     cout << "Post:" << endl;
  160.     PostOrder(root);
  161.     cout << endl;
  162.     cout << "In:" << endl;
  163.     InOrder(root);
  164. }
Advertisement
Add Comment
Please, Sign In to add comment