Advertisement
Bibodui

Лаба 3 ЯМП

Mar 10th, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.23 KB | None | 0 0
  1. /*
  2. 19. Существует способ проверить, является ли один узел истинным
  3. предком другого узла, который основан на следующем правиле : узел
  4. m является истинным предком узла n, если узел m предшествует узлу
  5. n при обходе дерева в X порядке, но следует за узлом n при обходе в Y
  6. порядке.Где X и Y выбираются из множества обходов{ прямом,
  7. обратном и симметричном }. Определите все возможные пары X и Y,
  8. когда это правило справедливо.
  9. */
  10.  
  11. #include <iostream>
  12. #include <Windows.h>
  13.  
  14. using namespace std;
  15.  
  16. struct Node
  17. {
  18.     int Key;
  19.     int Count;
  20.     Node* Left;
  21.     Node* Right;
  22. };
  23.  
  24. Node* tree;
  25.  
  26. int Initial(Node*&);
  27. void Search(Node*&, int);
  28. void Traversal_Straight(Node*, int*, int&);
  29. void Traversal_Back(Node*, int*, int&);
  30. void Traversal_Symmetric(Node*, int*, int&);
  31. void Output(Node*, int l);
  32. bool compare(int, int, int*, int*, int);
  33.  
  34. int main()
  35. {
  36.     int q;
  37.  
  38.     do
  39.     {
  40.         SetConsoleOutputCP(1251);
  41.         SetConsoleCP(1251);
  42.  
  43.         int size = Initial(tree);//создаем дерево и узнаем количество узлов в нем
  44.         cout << "Вывод дерева:" << endl;
  45.         Output(tree, 0);
  46.         int* arr1 = new int[size];//создаем массив под значения элементов при прямом обходе
  47.         int* arr2 = new int[size];//при обратном
  48.         int* arr3 = new int[size];//при симметричном
  49.  
  50.         int i = 0;
  51.  
  52.         Traversal_Straight(tree, arr1, i);
  53.         cout << "Прямой порядок обхода:" << endl;
  54.         for (int j = 0; j < size; j++)
  55.             cout << arr1[j] << '\t';
  56.         cout << endl;
  57.         i = 0;
  58.         Traversal_Back(tree, arr2, i);
  59.         cout << "Обратный порядок обхода:" << endl;
  60.         for (int j = 0; j < size; j++)
  61.             cout << arr2[j] << '\t';
  62.         cout << endl;
  63.         i = 0;
  64.         Traversal_Symmetric(tree, arr3, i);
  65.         cout << "Симметричный порядок обхода:" << endl;
  66.         for (int j = 0; j < size; j++)
  67.             cout << arr3[j] << '\t';
  68.         cout << endl;
  69.  
  70.         int n, m;
  71.         cout << "Проверка: является ли узел истинным предком другого узла:" << endl;
  72.         cout << "Введите предка:" << endl;
  73.         cin >> m;
  74.         cout << "Введите потомка:" << endl;
  75.         cin >> n;
  76.  
  77.         cout << "Прямой и обратный обходы: ";
  78.         if (compare(m, n, arr1, arr2, size))
  79.             cout << "Справедливо" << endl;
  80.         else cout << "Неверно" << endl;
  81.         cout << "Обратный и прямой обходы: ";
  82.         if (compare(m, n, arr2, arr1, size))
  83.             cout << "Справедливо" << endl;
  84.         else cout << "Неверно" << endl;
  85.        
  86.         cout << "Прямой и симметричный обходы: ";
  87.         if (compare(m, n, arr1, arr3, size))
  88.             cout << "Справедливо" << endl;
  89.         else cout << "Неверно" << endl;
  90.         cout << "Симметричный и прямой обходы: ";
  91.         if (compare(m, n, arr3, arr1, size))
  92.             cout << "Справедливо" << endl;
  93.         else cout << "Неверно" << endl;
  94.  
  95.         cout << "Обратный и симметричный обходы: ";
  96.         if (compare(m, n, arr2, arr3, size))
  97.             cout << "Справедливо" << endl;
  98.         else cout << "Неверно" << endl;
  99.         cout << "Симметричный и обратный обходы: ";
  100.         if (compare(m, n, arr3, arr2, size))
  101.             cout << "Справедливо" << endl;
  102.         else cout << "Неверно" << endl;
  103.  
  104.         cout << "0. Выход" << endl;
  105.         cin >> q;
  106.     } while (q != 0);
  107. }
  108.  
  109. int Initial(Node*& tree)
  110. {
  111.     int elem;
  112.     int size = 0;
  113.     tree = nullptr;
  114.     cout << "Введите ключи дерева:" << endl;
  115.     cin >> elem;
  116.     while (elem != 0)
  117.     {
  118.         size++;
  119.         Search(tree, elem);
  120.         cin >> elem;
  121.     }
  122.  
  123.     return size;
  124. }
  125.  
  126. void Search(Node*& tree, int elem)
  127. {
  128.     if (tree == nullptr)
  129.     {
  130.         tree = new Node;
  131.         tree->Key = elem;
  132.         tree->Count = 1;
  133.         tree->Left = tree->Right = nullptr;
  134.     }
  135.     else
  136.     {
  137.         if (elem < tree->Key)
  138.         {
  139.             Search(*&tree->Left, elem);
  140.         }
  141.         else if (elem > tree->Key)
  142.         {
  143.             Search(*&tree->Right, elem);
  144.         }
  145.         else tree->Count++;
  146.     }
  147. }
  148.  
  149. void Traversal_Straight(Node* tree, int* arr1, int& i)
  150. {
  151.     if (tree != nullptr)
  152.     {
  153.         arr1[i] = tree->Key;
  154.         i++;
  155.         Traversal_Straight(tree->Left, arr1, i);
  156.         Traversal_Straight(tree->Right, arr1, i);
  157.     }
  158. }
  159.  
  160. void Traversal_Back(Node* tree, int* arr2, int& i)
  161. {
  162.     if (tree != nullptr)
  163.     {
  164.         Traversal_Back(tree->Left, arr2, i);
  165.         Traversal_Back(tree->Right, arr2, i);
  166.         arr2[i] = tree->Key;
  167.         i++;
  168.     }
  169. }
  170.  
  171. void Traversal_Symmetric(Node* tree, int* arr3, int& i)
  172. {
  173.     if (tree != nullptr)
  174.     {
  175.         Traversal_Symmetric(tree->Left, arr3, i);
  176.         arr3[i] = tree->Key;
  177.         i++;
  178.         Traversal_Symmetric(tree->Right, arr3, i);
  179.     }
  180. }
  181.  
  182. void Output(Node* tree, int l)
  183. {
  184.     if (tree != nullptr)
  185.     {
  186.         Output(tree->Right, l + 1);
  187.         for (int i = 1; i <= l; i++)
  188.             cout << "     ";
  189.         cout << tree->Key << endl;
  190.         Output(tree->Left, l + 1);
  191.     }
  192. }
  193.  
  194. bool compare(int m, int n, int* arr1, int* arr2, int size)
  195. {
  196.     bool m_parent = false;
  197.     bool n_child = false;
  198.     for (int i = 0; i < size - 1; i++)
  199.     {
  200.         if (m == arr1[i])
  201.             if (n == arr1[i + 1])
  202.                 m_parent = true;
  203.         if (n == arr2[i])
  204.             if (m == arr2[i + 1])
  205.                 n_child = true;
  206.     }
  207.  
  208.     return (m_parent && n_child);
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement