Advertisement
Hebiame

Untitled

Jan 19th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6.  
  7.  
  8. struct Node {
  9.     int bf;
  10.     Node * up;
  11.     Node * left;
  12.     Node * right;
  13.     string value;
  14.     string lines = "";
  15. };
  16.  
  17. class AVLTree {
  18. private:
  19.     Node * root;
  20. public:
  21.     AVLTree();
  22.     ~AVLTree();
  23.     void add(string, int);
  24.     void r_rotate(Node *);
  25.     void l_rotate(Node *);
  26.     void rl_rotate(Node *);
  27.     void lr_rotate(Node *);
  28.     void show_inorder(Node *);
  29.     Node * find(string);
  30. };
  31.  
  32. AVLTree::AVLTree() {
  33.     this->root = NULL;
  34. }
  35.  
  36. AVLTree::~AVLTree() {
  37.     //...
  38. }
  39. void AVLTree::add(string value, int line) {
  40.     if (root == NULL) {
  41.         root = new Node;
  42.         root->value = value;
  43.         root->lines += to_string(line);
  44.         root->up = NULL;
  45.         root->left = NULL;
  46.         root->right = NULL;
  47.         root->bf = 0;
  48.     }
  49.     else {
  50.         Node * tmp = find(value);
  51.         if (tmp != NULL) {
  52.             tmp->lines = tmp->lines + " " + to_string(line);
  53.         }
  54.         else {
  55.             Node * new_node = new Node;
  56.             new_node->left = NULL;
  57.             new_node->right = NULL;
  58.             new_node->value = value;
  59.             new_node->lines += to_string(line);
  60.             new_node->bf = 0;
  61.             Node * tmp = root;
  62.             bool running = false;
  63.             while (running) {
  64.                 if (value < tmp->value) {
  65.                     if (tmp->left == NULL) {
  66.                         tmp->left = new_node;
  67.                         running = false;
  68.                     }
  69.                     else {
  70.                         tmp = tmp->left;
  71.                     }
  72.                 }
  73.                 else {
  74.                     if (tmp->right == NULL) {
  75.                         tmp->right = new_node;
  76.                         running = false;
  77.                     }
  78.                     else {
  79.                         tmp = tmp->right;
  80.                     }
  81.                 }
  82.             }
  83.             new_node->up = tmp;
  84.             if (tmp->bf == 0) {
  85.                 if (tmp->left == new_node) {
  86.                     tmp->bf = 1;
  87.                 }
  88.                 else {
  89.                     tmp->bf = -1;
  90.                 }
  91.             }
  92.             else {
  93.                 tmp->bf = 0;
  94.             }
  95.             Node * tmp_parent = new Node;
  96.             tmp_parent = tmp->up;
  97.             while (tmp_parent != NULL) {
  98.                 if (tmp_parent->bf != 0) {
  99.                     if (tmp_parent->bf == -1) {
  100.                         if (tmp_parent->left == tmp) {
  101.                             tmp_parent->bf = 0;
  102.                         }
  103.                         else {
  104.                             if (tmp->bf == 1) {
  105.                                 rl_rotate(tmp_parent);
  106.                             }
  107.                             else {
  108.                                 r_rotate(tmp_parent);
  109.                             }
  110.                         }
  111.                     }
  112.                     else {
  113.                         if (tmp_parent->right == tmp) {
  114.                             tmp_parent->bf = 0;
  115.                         }
  116.                         else {
  117.                             if (tmp->bf == -1) {
  118.                                 lr_rotate(tmp_parent);
  119.                             }
  120.                             else {
  121.                                 l_rotate(tmp_parent);
  122.                             }
  123.                         }
  124.                     }
  125.                 }
  126.                 else {
  127.                     if (tmp_parent->left == tmp) {
  128.                         tmp_parent->bf = 1;
  129.                     }
  130.                     else {
  131.                         tmp_parent->bf = -1;
  132.                     }
  133.                     tmp = tmp_parent;
  134.                 }
  135.             }
  136.         }
  137.     }
  138. }
  139. void AVLTree::r_rotate(Node * tmp_parent) {
  140.     //...
  141. }
  142.  
  143. void AVLTree::l_rotate(Node * tmp_parent) {
  144.     //...
  145. }
  146.  
  147. void AVLTree::rl_rotate(Node * tmp_parent) {
  148.     //...
  149. }
  150.  
  151. void AVLTree::lr_rotate(Node * tmp_parent) {
  152.     //...
  153. }
  154.  
  155. Node * AVLTree::find(string value) {
  156.     //...
  157.     return NULL;
  158. }
  159. void AVLTree::show_inorder(Node * root) {
  160.     if (root != NULL) {
  161.         show_inorder(root->left);
  162.         cout << root->value << " => " << root->lines << endl;
  163.         show_inorder(root->right);
  164.     }
  165. }
  166.  
  167.  
  168. int main() {
  169.     int n, i = 1;
  170.     string * array;
  171.     cin >> n;
  172.     array = new string[n];
  173.     while (n <= i) {
  174.         getline(cin, array[i]);
  175.     }
  176.     delete[] array;
  177.     system("pause");
  178.     return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement