Advertisement
Dmaxiya

Binary search tree in C++

Feb 21st, 2025
267
0
364 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <climits>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. class Node {
  7. private:
  8.     int data;
  9.     Node *left;
  10.     Node *right;
  11.  
  12.     friend class Tree;
  13. public:
  14.     Node(int value): data(value), left(NULL), right(NULL) {}
  15. };
  16.  
  17. class Tree {
  18. private:
  19.     Node *root;
  20.  
  21.     void preOrder(Node *root) {
  22.         if (root == NULL) {
  23.             return ;
  24.         }
  25.         cout << root->data << " ";
  26.         preOrder(root->left);
  27.         preOrder(root->right);
  28.     }
  29.  
  30.     void inOrder(Node *root) {
  31.         if (root == NULL) {
  32.             return ;
  33.         }
  34.         inOrder(root->left);
  35.         cout << root->data << " ";
  36.         inOrder(root->right);
  37.     }
  38.  
  39.     void postOrder(Node *root) {
  40.         if (root == NULL) {
  41.             return ;
  42.         }
  43.         postOrder(root->left);
  44.         postOrder(root->right);
  45.         cout << root->data << " ";
  46.     }
  47.  
  48.     void insert(Node *root, int value) {
  49.         if (value < root->data) {
  50.             if (root->left == NULL) {
  51.                 root->left = new Node(value);
  52.                 return ;
  53.             }
  54.             insert(root->left, value);
  55.             return ;
  56.         }
  57.  
  58.         if (root->right == NULL) {
  59.             root->right = new Node(value);
  60.             return ;
  61.         }
  62.         insert(root->right, value);
  63.     }
  64.  
  65.     int getHeight(Node *root) {
  66.         if (root == NULL) {
  67.             return 0;
  68.         }
  69.         return max(getHeight(root->left), getHeight(root->right)) + 1;
  70.     }
  71.  
  72.     int getMax(Node *root) {
  73.         if (root->right == NULL) {
  74.             return root->data;
  75.         }
  76.         return getMax(root->right);
  77.     }
  78.  
  79. public:
  80.     Tree(): root(NULL) {}
  81.  
  82.     void preOrder() {
  83.         preOrder(root);
  84.     }
  85.  
  86.     void inOrder() {
  87.         inOrder(root);
  88.     }
  89.  
  90.     void postOrder() {
  91.         postOrder(root);
  92.     }
  93.  
  94.     void insert(int value) {
  95.         if (root == NULL) {
  96.             root = new Node(value);
  97.             return ;
  98.         }
  99.         insert(root, value);
  100.     }
  101.  
  102.     int getHeight() {
  103.         return getHeight(root);
  104.     }
  105.  
  106.     int getMax() {
  107.         if (root == NULL) {
  108.             return INT_MIN;
  109.         }
  110.         return getMax(root);
  111.     }
  112.  
  113. };
  114.  
  115. int main() {
  116.     int arr[10] = {5, 3, 7, 2, 8, 10, 9, 1, 4, 6};
  117.     Tree tree;
  118.     for (int i = 0; i < 10; ++i) {
  119.         tree.insert(arr[i]);
  120.     }
  121.     tree.preOrder();
  122.     cout << endl;
  123.     tree.inOrder();
  124.     cout << endl;
  125.     tree.postOrder();
  126.     cout << endl;
  127.     cout << "Height = " << tree.getHeight() << endl;
  128.     cout << "MAX = " << tree.getMax() << endl;
  129.  
  130.     return 0;
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement