Advertisement
GrandtherAzaMarks

Black&Red Tree

Apr 7th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. struct Node {
  7.     long long key;
  8.     Node * left;
  9.     Node * right;
  10.  
  11.     Node(long long _key) {
  12.         key = _key;
  13.         left = nullptr;
  14.         right = nullptr;
  15.     }
  16. };
  17.  
  18. void insert(Node * root, long long key) {
  19.     if (key < root->key) {
  20.         if (root->left == nullptr) {
  21.             root->left = new Node(key);
  22.         }
  23.         insert(root->left, key);
  24.         return;
  25.     }
  26.     if (key > root->key) {
  27.         if (root->right == nullptr) {
  28.             root->right = new Node(key);
  29.         }
  30.         insert(root->right, key);
  31.         return;
  32.     }
  33. }
  34.  
  35. int height(Node * root) {
  36.     if (root == nullptr) {
  37.         return 0;
  38.     }
  39.     return max(height(root->left), height(root->right)) + 1;
  40. }
  41.  
  42. int main() {
  43.     long long num;
  44.     cin >> num;
  45.     Node * root = new Node(num);
  46.     while (cin >> num) {
  47.         if (num == 0) {
  48.             break;
  49.         }
  50.         insert(root, num);
  51.     }
  52.     cout << height(root);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement