Toxotsist

Сиаод 3 неРабочее АВЛ-дерево на числах

Oct 14th, 2021 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct node {
  5.     int data;
  6.     node* left, * right;
  7.     unsigned char height;
  8.     node(char in) { data = in; left = right = NULL; height = 1; }
  9. };
  10.  
  11. void Print(node* Tree, int l) {
  12.     int i;
  13.     if (Tree != NULL) {
  14.         Print((*Tree).right, l + 1);
  15.  
  16.         for (i = 1; i <= l; i++) cout << "   "; {
  17.             cout << Tree->data << endl;
  18.         }
  19.         Print(((*Tree).left), l + 1);
  20.     }
  21. }
  22.  
  23. void print(node* t, int u) {
  24.     //cout << "PRINT!\n";
  25.     if (t == NULL) {
  26.         //`cout << "T = NULL!\n";
  27.         return;
  28.     }
  29.     else {
  30.         print(t->left, u++);
  31.         for (int i = 0; i < u; i++) cout << "|";
  32.         cout << t->data << endl;
  33.         u--;
  34.         };
  35.     print(t->right, u++);
  36. }
  37.  
  38. unsigned char height(node* t) {
  39.     if (t) {
  40.         return t->height;
  41.     }
  42.     else {
  43.         return 0;
  44.     }
  45. }
  46.  
  47. void fixheight(node* p) {
  48.     unsigned char hl = height(p->left);
  49.     unsigned char hr = height(p->right);
  50.     if (hl > hr) p->height=hl+1;
  51.     else p->height=hr+1;
  52. }
  53.  
  54. int bf(node* p) {
  55.     return height(p->right) - height(p->left);
  56. }
  57.  
  58. node* rotateright(node* p) {
  59.     node* q = p->left;
  60.     p->left = q->right;
  61.     q->right = p;
  62.     fixheight(p);
  63.     fixheight(q);
  64.     return q;
  65. }
  66.  
  67. node* rotateleft(node* q) {
  68.     node* p = q->right;
  69.     q->right = p->left;
  70.     p->left = q;
  71.     fixheight(q);
  72.     fixheight(p);
  73.     return p;
  74. }
  75.  
  76. node* balance(node* p) {
  77.     cout << "BALANCE!\n";
  78.     fixheight(p);
  79.     if (bf(p) == 2) {
  80.         if (bf(p->right) < 0) p->right = rotateright(p->right);
  81.         return rotateleft(p);
  82.     }
  83.     if (bf(p) == -2) {
  84.         if (bf(p->left) > 0) p->left = rotateleft(p->left);
  85.         return rotateright(p);
  86.     }
  87.     return p;
  88. }
  89.  
  90. node* push(unsigned char in, node** t) {
  91.     if ((*t) == NULL) {
  92.         cout << "PUSH! "; (*t) = new node(in); cout << (*t)->data << endl; return balance((*t));
  93.     }
  94.     if (in < (*t)->data) { (*t)->left = push(in, &(*t)->left); }
  95.     else (*t)->right = push(in, &(*t)->right);
  96.     return balance((*t));
  97. }
  98.  
  99. int main() {
  100.     node* tree = NULL;
  101.     push(13, &tree);
  102.     push(8, &tree);
  103.     push(17, &tree);
  104.     push(1, &tree);
  105.     push(11, &tree);
  106.     push(15, &tree);
  107.     push(25, &tree);
  108.     push(22, &tree);
  109.     push(27, &tree);
  110.     print(tree, 0);
  111.     Print(tree, 0);
  112.     return 0;
  113. }
Add Comment
Please, Sign In to add comment