Advertisement
Guest User

Tree

a guest
Jan 23rd, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <map>
  4. using namespace std;
  5.  
  6. struct Node {
  7.     int data;
  8.     Node* left = nullptr;
  9.     Node* right = nullptr;
  10.  
  11.     Node(int value) {
  12.         data = value;
  13.     }
  14.  
  15.     ~Node() {
  16.         delete left;
  17.         delete right;
  18.     }
  19. };
  20.  
  21. class BST {
  22. private:
  23.     Node* root;
  24.  
  25.     bool _exists(int value, Node* current) const {
  26.         if (current) {
  27.             if (current->data == value) {
  28.                 return true;
  29.             } else if (value > current->data) {
  30.                 return _exists(value, current->right);
  31.             } else {
  32.                 return _exists(value, current->left);
  33.             }
  34.         } else {
  35.             return false;
  36.         }
  37.     }
  38.  
  39.     Node* _insert(int value, Node* current) {
  40.         if (!current) {
  41.             return new Node(value);
  42.         }
  43.  
  44.         if (value < current->data) {
  45.             current->left = _insert(value, current->left);
  46.         } else if (value > current->data) {
  47.             current->right = _insert(value, current->right);
  48.         }
  49.  
  50.         return current;
  51.     }
  52.  
  53.     void _print(Node* current) const {
  54.         if (current) {
  55.             _print(current->left);
  56.             cout << current->data << " ";
  57.             _print(current->right);
  58.         }
  59.     }
  60.  
  61.     Node* _remove(int value, Node* current) {
  62.         if (!current) {
  63.             return nullptr;
  64.         }
  65.  
  66.         if (value < current->data) {
  67.             current->left = _remove(value, current->left);
  68.         } else if (value > current->data) {
  69.             current->right = _remove(value, current->right);
  70.         } else {
  71.             if (!current->left && !current->right) {
  72.                 free(current);
  73.                 return nullptr;
  74.             } else if (!current->left) {
  75.                 Node* tempRight = current->right;
  76.                 free(current);
  77.                 return tempRight;
  78.             } else if (!current->right) {
  79.                 Node* tempLeft = current->left;
  80.                 free(current);
  81.                 return tempLeft;
  82.             } else {
  83.                 Node* swapWith = current->right;
  84.                
  85.                 while (swapWith->left) {
  86.                     swapWith = swapWith->left;
  87.                 }
  88.  
  89.                 current->data = swapWith->data;
  90.                 current->right = _remove(swapWith->data, current->right);
  91.             }
  92.         }
  93.  
  94.         return current;
  95.     }
  96.  
  97.     void _printTop(Node* root, int distance, int level, std::map<int, std::pair<int, int>>& m) {
  98.        
  99.         if (root == nullptr) {
  100.             return;
  101.         }
  102.            
  103.         if (m.find(distance) == m.end() ||
  104.             level < m[distance].second) {
  105.            
  106.             m[distance] = { root->data, level };
  107.         }
  108.        
  109.         _printTop(root->left, distance - 1, level + 1, m);
  110.         _printTop(root->right, distance + 1, level + 1, m);
  111.  
  112.     }
  113.  
  114. public:
  115.     BST() {
  116.         root = nullptr;
  117.     }
  118.  
  119.     ~BST() {
  120.         delete root;
  121.     }
  122.  
  123.     bool exists(int value) const {
  124.         return _exists(value, root);
  125.     }
  126.  
  127.     void insert(int value) {
  128.         root = _insert(value, root);
  129.     }
  130.  
  131.     void remove(int value) {
  132.         root = _remove(value, root);
  133.     }
  134.  
  135.     void print() const {
  136.         _print(root);
  137.     }
  138.  
  139.     void levelorder() const {
  140.         queue<Node*> que;
  141.         que.push(root);
  142.  
  143.         while (!que.empty()) {
  144.             Node* current = que.front();
  145.             que.pop();
  146.            
  147.             if (current) {
  148.                 cout << current->data << " ";
  149.  
  150.                 if (current->left) {
  151.                     que.push(current->left);
  152.                 }
  153.                 if (current->right) {
  154.                     que.push(current->right);
  155.                 }
  156.             }
  157.  
  158.         }
  159.     }
  160.  
  161.     void printTop() {
  162.         //key - the horizontal distance to the node
  163.         //value - std::pair<node->data, level>
  164.         std::map<int, std::pair<int, int>> m;
  165.        
  166.         _printTop(root, 0, 0, m);
  167.        
  168.         for (auto it : m) {
  169.             cout << it.second.first << " ";
  170.         }
  171.     }
  172. };
  173.  
  174. int main() {
  175.     BST s;
  176.  
  177.     s.insert(3);
  178.     s.print();
  179.  
  180.     return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement