Mahmoud_Hawara

BST - without removing

Nov 13th, 2025 (edited)
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class BST {
  5. private:
  6.     struct Node {
  7.         int key;
  8.         Node* left;
  9.         Node* right;
  10.  
  11.         Node(int value) {
  12.             key = value;
  13.             left = right = nullptr;
  14.         }
  15.     };
  16.  
  17.     Node* root;
  18.     int sz = 0;
  19.  
  20.     Node* insert(Node* root, int value) {
  21.         if (root == nullptr) return new Node(value);
  22.         if (value < root->key)
  23.             root->left = insert(root->left, value);
  24.         else
  25.             root->right = insert(root->right, value);
  26.         return root;    
  27.     }
  28.  
  29.     bool search(Node* root, int value) {
  30.         if (root == nullptr) return false;
  31.         if (value < root->key)
  32.             return search(root->left, value);
  33.         if (value > root->key)
  34.             return search(root->right, value);
  35.         return true;
  36.     }
  37.  
  38.     Node* getMin(Node* root) {
  39.         while (root->left != nullptr) {
  40.             root = root->left;
  41.         }
  42.         return root;
  43.     }
  44.  
  45.     Node* getMax(Node* root) {
  46.         while (root->right != nullptr) {
  47.             root = root->right;
  48.         }
  49.         return root;
  50.     }
  51.  
  52.     void inOrder(Node* root) {
  53.         if (root == nullptr) return;
  54.         inOrder(root->left);
  55.         cout << root->key << ' ';
  56.         inOrder(root->right);
  57.     }
  58.  
  59.     void preOrder(Node* root) {
  60.         if (root == nullptr) return;
  61.         cout << root->key << ' ';
  62.         preOrder(root->left);
  63.         preOrder(root->right);
  64.     }
  65.  
  66.     void postOrder(Node* root) {
  67.         if (root == nullptr) return;
  68.         postOrder(root->left);
  69.         postOrder(root->right);
  70.         cout << root->key << ' ';
  71.     }
  72.  
  73. public:
  74.     BST() {
  75.         root = nullptr;
  76.     }
  77.  
  78.     int size() {    // O(1)
  79.         return sz;
  80.     }
  81.  
  82.     bool empty() {  // O(1)
  83.         return root == nullptr;
  84.     }
  85.  
  86.     void insert(int value) {    // O(n)
  87.         root = insert(root, value);
  88.         sz++;
  89.     }
  90.  
  91.     bool search(int value) {    // O(n)
  92.         return search(root, value);
  93.     }
  94.  
  95.     int getMin() {  // O(n)
  96.         if (empty()) throw runtime_error("The BST is Empty");
  97.         return getMin(root)->key;
  98.     }
  99.  
  100.     int getMax() {  // O(n)
  101.         if (empty()) throw runtime_error("The BST is Empty");
  102.         return getMax(root)->key;
  103.     }
  104.  
  105.     bool remove(int value) {    // O(n)
  106.         //  TO DO
  107.         return true;
  108.     }
  109.  
  110.     void inOrder() {    // O(n)
  111.         inOrder(root);
  112.         cout << '\n';
  113.     }
  114.  
  115.     void preOrder() {   // O(n)
  116.         preOrder(root);
  117.         cout << '\n';
  118.     }
  119.  
  120.     void postOrder() {  // O(n)
  121.         postOrder(root);
  122.         cout << '\n';
  123.     }
  124. };
  125.  
  126. int main()
  127. {
  128.     BST tree;
  129.  
  130.     // cout << tree.getMin() << '\n';
  131.  
  132.     tree.insert(5);
  133.     tree.insert(1);
  134.     tree.insert(3);
  135.     tree.insert(7);
  136.     tree.insert(8);
  137.  
  138.     cout << tree.getMin() << '\n';
  139.     cout << tree.getMax() << '\n';
  140.  
  141.     // tree.search(3);
  142.     // tree.search(6);
  143.  
  144.     // tree.inOrder();
  145.     // tree.preOrder();
  146.     // tree.postOrder();
  147.  
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment