Advertisement
Guest User

DZ2

a guest
Jan 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <tchar.h>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. template<class T>
  9. struct ANode
  10. {
  11.     T data;
  12.     ANode<T> *down, *right;
  13.     ANode(T dd, ANode<T> *d = nullptr, ANode<T> *r = nullptr) :
  14.         data(dd), down(d), right(r) {}
  15. };
  16.  
  17. template<class T>
  18. void print(ANode<T> *r, int offset = 0)
  19. {
  20.     if (r == nullptr) return;
  21.     for (int i = 0; i<offset; i++)
  22.         cout << " ";
  23.     cout << r->data << endl;
  24.     print(r->down, offset + 3);
  25.     print(r->right, offset);
  26. }
  27.  
  28. template<class T>
  29. int count2(ANode<T> *r)
  30. {
  31.     int c = 0;
  32.     if (r == nullptr) return c;
  33.     r = r->down;
  34.     while (r != nullptr)
  35.     {
  36.         c++;
  37.         r = r->right;
  38.     }
  39.     return c;
  40. }
  41. template<class T>
  42. ANode<int> *find(ANode<T> *r, int d1, int d2){
  43.     int c = 0;
  44.     ANode<T> *f = NULL;
  45.     r = r->down;
  46.     while (r != nullptr)
  47.     {
  48.         if (r->data == d1)
  49.         {
  50.             f = r;
  51.             r = r->down;
  52.             while (r != nullptr)
  53.             {
  54.                 if (r->data == d2) return f;
  55.                 r = r->right;
  56.             }
  57.  
  58.         }
  59.         r = r->right;
  60.     }
  61. }
  62.  
  63. template<class T>
  64. ANode<int> Ad(ANode<T> *r, int d1){
  65.     ANode<int> *tree1 = &ANode<int>(d1, nullptr, r->down);
  66.     ANode<int> *tree3 = &(ANode<int>(r->data, tree1, nullptr));
  67.     print(tree3);
  68.     return *tree3;
  69. }
  70.  
  71. template<class T>
  72. ANode<int> Dell(ANode<T> *r){
  73.     ANode<T> *f = NULL;
  74.     ANode<T> *c = NULL;
  75.     ANode<int> *tree1 = &(ANode<int>(r->data, r->down->right, nullptr));
  76.     f = r->down;
  77.     f->right = NULL;
  78.    
  79.     r = tree1->down;
  80.     while (r->right)
  81.     {
  82.         r = r->right;
  83.         c = r;
  84.     }
  85.     c->right = f;
  86.  
  87.     print(tree1);
  88.     return *tree1;
  89. }
  90. int main()
  91. {
  92.     int d1, d2;
  93.     ANode<int> *tree2 = new ANode<int>(1,
  94.         new ANode<int>(2,
  95.         new ANode<int>(5,
  96.         nullptr,
  97.         new ANode<int>(6,
  98.         nullptr,
  99.         new ANode<int>(7))),
  100.         new ANode<int>(3,
  101.         new ANode<int>(8),
  102.         new ANode<int>(4,
  103.         new ANode<int>(9,
  104.         nullptr,
  105.         new ANode<int>(10))))));
  106.     print(tree2);
  107.  
  108.     cout << "Number: " << count2(tree2) << endl; //посчитать число сыновей
  109.     cin >> d1 >> d2;
  110.     ANode<int> *f = find(tree2, d1, d2);
  111.     cout << "Derevo: " << endl;
  112.  
  113.     print(f->down);
  114.     cout << "Derevo2: " << endl;
  115.     Ad(tree2, d1);
  116.     cout << "Derevo3: " << endl;
  117.     Dell(tree2);
  118.     //cout << "Derevo 2 : " << endl;
  119.     //print(tree2);
  120.     //Задание:
  121.     //1) Написать функцию, возвращающую указатель на первого сына корня с данными d1, у которого есть сын с
  122.     //данными d2(d1 и d2 — параметры функции).
  123.     //2) Написать процедуру, вставляющую в дерево первого сына корня с данными, переданными в качестве параметра
  124.     //3) Написать процедуру, удаляющую первого сына корня (его сыновья, если такие были, становятся сыновьями
  125.     //корня перед бывшим вторым сыном корня).
  126.  
  127.  
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement