Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6. int cond = 4;
  7.  
  8. struct node
  9. {
  10.     int val;
  11.     node* left;
  12.     node* right;
  13.     node* up;
  14. }*root_1=NULL,*root_2=NULL;
  15.  
  16. node *min(node *root)
  17. {
  18.     while (root->left)
  19.         root = root->left;
  20.     return root;
  21. }
  22.  
  23. node *max(node *root)
  24. {
  25.     if (!root)
  26.         cout << "Drzewo jest puste!";
  27.     while (root->right)
  28.         root = root->right;
  29.     return root;
  30. }
  31.  
  32.  
  33.  
  34. node *look(node *root, int x)
  35. {
  36.     if (!root)
  37.         cout << "Drzewo jest puste!";
  38.     else
  39.     {
  40.         while ((root) && (root->val != x))
  41.         {
  42.             if (x < root->val)
  43.                 root = root->left;
  44.             else
  45.                 root = root->right;
  46.         }
  47.         if (!root)
  48.             cout << "Brak takiego elementu!";
  49.     }
  50.     return root;
  51. }
  52.  
  53. node *next(node *w)
  54. {
  55.     if (w->right)
  56.         return min(w->right);
  57.     node *p = w->up;
  58.     while ((p) && (w == p->right))
  59.     {
  60.         w = p;
  61.         p = p->up;
  62.     }
  63.     return p;
  64. }
  65.  
  66. node *prev(node *w)
  67. {
  68.     if (!w)
  69.         cout << "Drzewo jest puste!";
  70.     if (w->left)
  71.         return max(w->left);
  72.     node *p = w->up;
  73.     while ((p) && (w == p->left))
  74.     {
  75.         w = p;
  76.         p = p->up;
  77.     }
  78.     return p;
  79. }
  80.  
  81. void add(node *&root, int x)
  82. {
  83.     node *nowy = new node;
  84.     nowy->right = nowy->left = NULL;
  85.     nowy->val = x;
  86.     node* p = root;
  87.     if (!p)
  88.         root = nowy;
  89.     else
  90.         while (true)
  91.             if (x < p->val)
  92.                 if (!p->left)
  93.                 {
  94.                     p->left = nowy;
  95.                     break;
  96.                 }
  97.                 else
  98.                     p = p->left;
  99.             else
  100.             {
  101.                 if (!p->right)
  102.                 {
  103.                     p->right = nowy;
  104.                     break;
  105.                 }
  106.                 else
  107.                     p = p->right;
  108.             }
  109.     nowy->up = p;
  110. }
  111.  
  112. int del(node *&root, node* x)
  113. {
  114.     node *p, *w;
  115.     int k = x->val;
  116.     if ((!x->left) || (!x->right))
  117.         p = x;
  118.         else
  119.             p = next(x);
  120.         if (p->left)
  121.             w = p->left;
  122.         else
  123.             w = p->right;
  124.         if (w)
  125.             w->up = p->up;
  126.  
  127.         if (!p->up) root = w;
  128.         else if (p == p->up->left) p->up->left = w;
  129.         else p->up->right = w;
  130.         if (p != x) x->val = p->val;
  131.  
  132.         delete p;
  133.         return k;
  134. }
  135.  
  136.  
  137. void modify(node*& root_1, node*& root_2, node *x_2, int cond)
  138. {
  139.     if (x_2->val == cond)
  140.     {
  141.         add(root_1, del(root_2, next(x_2)));
  142.     }
  143. }
  144.  
  145. void inorder1(node*& root_1, node*& root_2, node* x_2, int cond)
  146. {
  147.     if (!x_2)return;
  148.     if (x_2->left)
  149.         inorder1(root_1,root_2,x_2->left, cond);
  150.     modify(root_1, root_2, x_2, cond);
  151.     if (x_2->right)
  152.         inorder1(root_1, root_2, x_2->right, cond);
  153. }
  154.  
  155. void inorder(node* root)
  156. {
  157.     if (!root)return;
  158.     if (root->left)
  159.         inorder(root->left);
  160.     cout << root->val << " ";
  161.     if (root->right)
  162.         inorder(root->right);
  163. }
  164.  
  165.  
  166. void main()
  167. {
  168.     add(root_1, 3);
  169.     add(root_1, 7);
  170.     add(root_1, 5);
  171.     add(root_2, 2);
  172.     add(root_2, 6);
  173.     add(root_2, 4);
  174.     add(root_2, 1);
  175.     add(root_2, 8);
  176.  
  177.     inorder(root_1);
  178.     cout << endl;
  179.     inorder(root_2);
  180.  
  181.     cout << endl;
  182.  
  183.     node*x = root_2;
  184.     inorder1(root_1,root_2,x,cond);
  185.     inorder(root_1);
  186.     cout << endl;
  187.     inorder(root_2);
  188.  
  189.     system("PAUSE");
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement