TawratNibir

BST everything

Jul 21st, 2025
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. class Node
  5. {
  6. public:
  7.     int data;
  8.     Node *left;
  9.     Node *right;
  10.     Node *parent;
  11.     Node(int val, Node* par)
  12.     {
  13.         data = val;
  14.         left = right = NULL;
  15.         parent = par;
  16.     }
  17. };
  18. Node* insert(Node *root, int value, Node *parent)
  19. {
  20.     if (root == NULL)
  21.         return new Node(value, parent);
  22.  
  23.     if (value < root->data)
  24.     {
  25.         root->left = insert(root->left, value, root);
  26.     }
  27.     else
  28.     {
  29.         root->right = insert(root->right, value, root);
  30.     }
  31.     return root;
  32. }
  33.  
  34. Node* buildBST(vector<int> arr)
  35. {
  36.     Node *root = NULL;
  37.  
  38.     for (int x : arr)
  39.     {
  40.         root = insert(root, x, nullptr);
  41.     }
  42.     return root;
  43. }
  44.  
  45. void print(Node *root)
  46. {
  47.     if (root == NULL)
  48.         return;
  49.  
  50.     print(root->left);
  51.     cout << root->data << " ";
  52.     print(root->right);
  53. }
  54. void printStk(Node *root2)
  55. {
  56.     stack<Node*> s;
  57.     Node *root = root2;
  58.     while (root != nullptr || !s.empty())
  59.     {
  60.         while (root != nullptr)
  61.         {
  62.             s.push(root);
  63.             root = root->left;
  64.         }
  65.  
  66.         root = s.top();
  67.         s.pop();
  68.  
  69.         cout << root->data << " ";
  70.  
  71.         root = root->right;
  72.     }
  73. }
  74. void printPost(Node *root)
  75. {
  76.  
  77.     if (root == nullptr)
  78.         return;
  79.  
  80.     printPost(root->left);
  81.     printPost(root->right);
  82.     cout << root->data << " ";
  83. }
  84. void printPre(Node *root)
  85. {
  86.  
  87.     if (root == nullptr)
  88.         return;
  89.  
  90.     cout << root->data << " ";
  91.     printPre(root->left);
  92.     printPre(root->right);
  93. }
  94. Node* search(Node*& root, int val)
  95. {
  96.     if (root == nullptr)
  97.         return nullptr;
  98.  
  99.     if (val < root->data)
  100.         return search(root->left, val);
  101.     else if(val > root->data)
  102.         return search(root->right, val);
  103.     else if(val == root->data)
  104.         return root;
  105. }
  106. Node* searchItr(Node* root, int val) {
  107.     while (root != nullptr)
  108.     {
  109.         if(val == root->data) {
  110.             return root;
  111.         }
  112.         else if(val < root->data) root = root->left;
  113.         else if(val > root->data) root = root->right;
  114.     }
  115.     return root;
  116. }
  117. Node* findMini(Node*& root) {
  118.     if(root->left == nullptr) return root;
  119.     return findMini(root->left);
  120. }
  121. Node* findMaxi(Node* root) {
  122.     if(root->right == nullptr) return root;
  123.     return findMaxi(root->right);
  124. }
  125. void printPreStack(Node* root) {
  126.     stack<Node*>s;
  127.     s.push(nullptr);
  128.     Node* ptr = root;
  129.     while(ptr != nullptr) {
  130.         cout << ptr->data << " ";
  131.         if(ptr->right != nullptr) s.push(ptr->right);
  132.         if(ptr->left != nullptr) ptr = ptr->left;
  133.         else {
  134.             // cout << s.top()->data << " ";
  135.             ptr = s.top();
  136.             s.pop();
  137.         }
  138.     }
  139. }
  140. Node* successor(Node*& nodeTree) {
  141.     if(nodeTree->right != nullptr) {
  142.         return findMini(nodeTree->right);
  143.     }
  144.     else{
  145.         Node* y = nodeTree->parent;
  146.         while (y!=nullptr && nodeTree == y->right)
  147.         {
  148.             nodeTree = y;
  149.             y = y->parent;
  150.         }
  151.         return y;
  152.     }
  153. }
  154. Node* predecessor(Node* nodeTree) {
  155.     if(nodeTree->left != nullptr) {
  156.         return findMaxi(nodeTree->left);
  157.     }
  158.     else{
  159.         Node* y = nodeTree->parent;
  160.         while (y!=nullptr && nodeTree == y->left)
  161.         {
  162.             nodeTree = y;
  163.             y = y->parent;
  164.         }
  165.         return y;
  166.     }
  167. }
  168. void del(Node*& root, int key) {
  169.     Node* treeNode = search(root, key);
  170.     if(treeNode == nullptr) {
  171.         return;
  172.     }
  173.     if(treeNode->left == nullptr && treeNode->right == nullptr) {
  174.         if(treeNode == treeNode->parent->left) treeNode->parent->left = nullptr;
  175.         else treeNode->parent->right = nullptr;
  176.         delete treeNode;
  177.         return;
  178.     }
  179.     else if(treeNode->left != nullptr && treeNode->right == nullptr) {
  180.         if(treeNode->parent->left == treeNode) treeNode->parent->left = treeNode->left;
  181.         else treeNode->parent->right = treeNode->left;
  182.         // treeNode->left = nullptr;
  183.         return;
  184.     }
  185.     else if(treeNode->left == nullptr && treeNode->right != nullptr) {
  186.         if(treeNode->parent->left == treeNode) treeNode->parent->left = treeNode->right;
  187.         else treeNode->parent->right = treeNode->right;
  188.         // treeNode->right = nullptr;
  189.         return;
  190.     }
  191.     else{
  192.         Node* succ = successor(treeNode);
  193.         // treeNode->data = succ->data;
  194.         swap(treeNode->data,succ->data);
  195.         del(succ, succ->data);
  196.     }
  197. }
  198. int main()
  199. {
  200.     vector<int> arr = {5, 4, 6, 2, 3, 7, 8};
  201.     Node *root = buildBST(arr);
  202.     // cout << "Printed normally: ";
  203.     // print(root);
  204.     // cout << endl;
  205.     // cout << "Printed in stack: ";
  206.     // printStk(root);
  207.     // cout << endl;
  208.     // cout << "Printed post order: ";
  209.     // printPost(root);
  210.     // cout << endl;
  211.     // cout << "Printed pre order: ";
  212.     // printPre(root);
  213.     // cout << endl;
  214.     // cout << "Printed pre order with stack: ";
  215.     // printPreStack(root);
  216.     // cout << endl;
  217.     // Node* succ = successor(search(root, 2));
  218.     // cout << "Successor of 2 is: " << succ->data;
  219.     // cout << endl;
  220.     // Node* pre = predecessor(search(root, 5));
  221.     // cout << "Predecessor of 5 is: " << pre->data;
  222.     // cout << endl;
  223.     // Node* n1 = search(root, 8);
  224.     // Node* n2 = search(root, 3);
  225.     print(root);
  226.     cout << "\n";
  227.     del(root, 2);
  228.     print(root);
  229.     cout << "\n";
  230.     // cout << endl;
  231.     // del(root, 3);
  232.     // print(root);
  233.     // cout << endl;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment