Advertisement
operaatoors

Untitled

Feb 17th, 2016
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.68 KB | None | 0 0
  1. #include < iostream >
  2. using namespace std;
  3.  
  4. struct tree {
  5.     tree * right;
  6.     tree * middle;
  7.     tree * left;
  8.     int data1;
  9.     int data2;
  10.     tree * parent;
  11. };
  12.  
  13. void Output(tree * root) {
  14.     if (root -> right != NULL) {
  15.         Output(root -> right);
  16.     }
  17.     cout << " " << root -> data1 << " ";
  18.  
  19.     if (root -> middle != NULL) {
  20.         Output(root -> middle);
  21.     }
  22.  
  23.     if (root -> data2 != -1) {
  24.         cout << " " << root -> data2 << " ";
  25.     }
  26.  
  27.     if (root -> left != NULL) {
  28.         Output(root -> left);
  29.     }
  30. }
  31.  
  32. void Input_mid(tree * & point, int num);
  33. void Input_right(tree * & point, int num);
  34.  
  35. void Order(tree * root, tree * point) {
  36.     if (point == root -> right) {
  37.         if (root -> data2 == -1) {
  38.             root -> data2 = root -> data1;
  39.             root -> data1 = point -> data1;
  40.             root -> left = root -> middle;
  41.             root -> middle = point -> middle;
  42.             point -> middle = NULL;
  43.             root -> middle -> parent = root;
  44.             root -> right = point -> right;
  45.             point -> right = NULL;
  46.             root -> right -> parent = root;
  47.             delete(point);
  48.             return;
  49.         }
  50.  
  51.         if (root -> data2 != -1) {
  52.             tree * point;
  53.             tree * point2;
  54.             tree * point3;
  55.             tree * point4;
  56.  
  57.             point2 = root -> left;
  58.             point = root -> middle;
  59.             point3 = point -> right;
  60.             point4 = point -> middle;
  61.  
  62.             Input_mid(root, root -> data2);
  63.             root -> middle -> right = point;
  64.             root -> middle -> middle = point2;
  65.             root -> data2 = -1;
  66.  
  67.             Input_right(root, point -> data1);
  68.             root -> right -> right = point3;
  69.             root -> right -> middle = point4;
  70.             root -> left = NULL;
  71.             delete(point);
  72.  
  73.         }
  74.     }
  75.  
  76.     if (point == root -> middle) {
  77.         if (root -> data2 == -1) {
  78.             root -> data2 = point -> data1;
  79.             root -> left = root -> middle -> middle;
  80.             root -> middle -> middle = NULL;
  81.             root -> middle = point -> right;
  82.             point -> right = NULL;
  83.             root -> middle -> parent = root;
  84.             root -> left -> parent = root;
  85.             delete(point);
  86.             return;
  87.         }
  88.  
  89.         if (root -> data2 != -1) {
  90.             tree * point;
  91.             tree * point2;
  92.             tree * point3;
  93.             tree * point4;
  94.  
  95.             point = root -> right;
  96.             point2 = root -> left;
  97.             point3 = point -> right;
  98.             point4 = point -> middle;
  99.  
  100.             Input_right(root, root -> data1);
  101.             Input_mid(root, root -> data2);
  102.             root -> data2 = -1;
  103.             root -> data1 = point -> data1;
  104.             root -> right -> right = point;
  105.             root -> right -> middle = point3;
  106.             root -> middle -> right = point4;
  107.             root -> middle -> middle = point2;
  108.             root -> left = NULL;
  109.         }
  110.     }
  111.  
  112.     if (point == root -> left) {
  113.         tree * point;
  114.         tree * point2;
  115.         tree * point3;
  116.         tree * point4;
  117.  
  118.         point = root -> right;
  119.         point2 = root -> middle;
  120.  
  121.         Input_right(root, root -> data1);
  122.         root -> data1 = root -> data2;
  123.         root -> right -> right = point;
  124.         root -> right -> middle = point2;
  125.         root -> middle = NULL;
  126.         root -> middle = root -> left;
  127.         root -> left = NULL;
  128.         root -> data2 = -1;
  129.     }
  130. }
  131.  
  132. void Input_init(tree * & point, int num) {
  133.     point -> data1 = num;
  134.     point -> data2 = -1;
  135.     point -> right = NULL;
  136.     point -> middle = NULL;
  137.     point -> left = NULL;
  138.     point -> parent = NULL;
  139. }
  140.  
  141. void Input_right(tree * & point, int num) {
  142.     point -> right = new tree;
  143.     point -> right -> data1 = num;
  144.     point -> right -> data2 = -1;
  145.     point -> right -> right = NULL;
  146.     point -> right -> middle = NULL;
  147.     point -> right -> left = NULL;
  148.     point -> right -> parent = point;
  149. }
  150. void Input_mid(tree * & point, int num) {
  151.     point -> middle = new tree;
  152.     point -> middle -> data1 = num;
  153.     point -> middle -> data2 = -1;
  154.     point -> middle -> right = NULL;
  155.     point -> middle -> middle = NULL;
  156.     point -> middle -> left = NULL;
  157.     point -> middle -> parent = point;
  158. }
  159.  
  160. void Input(tree * & point, int number, tree * & prim) {
  161.     if ((number <= point -> data1) && (point -> data2 == -1) && (point -> right == NULL)) {
  162.         point -> data2 = point -> data1;
  163.         point -> data1 = number;
  164.         return;
  165.     }
  166.  
  167.     if ((number > point -> data1) && (point -> data2 == -1) && (point -> right == NULL)) {
  168.         point -> data2 = number;
  169.         return;
  170.     }
  171.  
  172.     if ((point -> right != NULL) && (point -> data1 >= number)) {
  173.         Input(point -> right, number, prim);
  174.         return;
  175.     }
  176.  
  177.     if ((point -> middle != NULL) && (point -> data2 >= number) && (point -> data1 < number)) {
  178.         Input(point -> middle, number, prim);
  179.         return;
  180.     }
  181.  
  182.     if ((point -> middle != NULL) && (point -> data2 == -1) && (point -> data1 < number)) {
  183.         Input(point -> middle, number, prim);
  184.         return;
  185.     }
  186.  
  187.     if ((point -> left != NULL) && (point -> data2 < number)) {
  188.         Input(point -> left, number, prim);
  189.         return;
  190.     }
  191.  
  192.     if ((number <= point -> data1) && (point -> data2 != -1) && (point -> right == NULL)) {
  193.         Input_right(point, number);
  194.         Input_mid(point, point -> data2);
  195.         point -> data2 = -1;
  196.  
  197.         if (point -> parent != NULL) {
  198.             Order(point -> parent, point);
  199.         }
  200.         return;
  201.     }
  202.  
  203.     if ((number > point -> data1) && (number <= point -> data2)) {
  204.         Input_right(point, point -> data1);
  205.         Input_mid(point, point -> data2);
  206.         point -> data2 = -1;
  207.         point -> data1 = number;
  208.  
  209.         if (point -> parent != NULL) {
  210.             Order(point -> parent, point);
  211.         }
  212.         return;
  213.     }
  214.  
  215.     if ((number > point -> data2) && (point -> data2 != -1)) {
  216.         Input_right(point, point -> data1);
  217.         Input_mid(point, number);
  218.         point -> data1 = point -> data2;
  219.         point -> data2 = -1;
  220.  
  221.         if (point -> parent != NULL) {
  222.             Order(point -> parent, point);
  223.         }
  224.         return;
  225.     }
  226. }
  227.  
  228. int main() {
  229.     tree root, * P, * P1;
  230.     P1 = P = & root;
  231.     int num;
  232.     cout << "Input number - ";
  233.     cin >> num;
  234.     Input_init(P, num);
  235.  
  236.     while (num != 0) {
  237.         cout << "Input number - ";
  238.         cin >> num;
  239.  
  240.         if (num < 0) {
  241.             num = num * -1;
  242.         }
  243.         if (num != 0) {
  244.             Input(P, num, P1);
  245.         }
  246.     }
  247.  
  248.     Output(P);
  249.     return 0;
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement