Advertisement
Bibodui

Binary tree

Dec 16th, 2020 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <Windows.h>
  3.  
  4. using namespace std;
  5.  
  6. struct Node
  7. {
  8.     int Key;
  9.     int Count;
  10.     Node* Left;
  11.     Node* Right;
  12. };
  13.  
  14. Node* tree;
  15.  
  16. void Initial(Node*&);
  17. void Search(Node*&, int);
  18. void Traversal_Straight(Node*);
  19. void Traversal_Back(Node*);
  20. void Traversal_Symmetric(Node*);
  21. int Height(Node*);
  22. void Clean(Node*);
  23. void Output(Node*, int l);
  24. void Delete_1(Node*&, Node*&);
  25. void Delete(Node*&, int);
  26. Node* Search_Elem(Node*, int);
  27.  
  28. int main()
  29. {
  30.     int q;
  31.  
  32.     do
  33.     {
  34.         SetConsoleOutputCP(1251);
  35.         SetConsoleCP(1251);
  36.  
  37.         cout << "Выберите действие:" << endl;
  38.         cout << "1. Инициализация дерева" << endl;
  39.         cout << "2. Прямой обход дерева" << endl;
  40.         cout << "3. Обратный обход дерева" << endl;
  41.         cout << "4. Симметричный обход дерева" << endl;
  42.         cout << "5. Определить высоту дерева" << endl;
  43.         cout << "6. Очистить дерево" << endl;
  44.         cout << "7. Вывод дерева" << endl;
  45.         cout << "8. Добавить вершину" << endl;
  46.         cout << "9. Удалить вершину" << endl;
  47.         cout << "10. Найти адресс вершины с нужным значеним" << endl;
  48.         cout << "0. Выход" << endl;
  49.         cin >> q;
  50.         if (q == 1)
  51.             Initial(tree);
  52.         else if (q == 2)
  53.         {
  54.             Traversal_Straight(tree);
  55.             cout << endl;
  56.         }
  57.         else if (q == 3)
  58.         {
  59.             Traversal_Back(tree);
  60.             cout << endl;
  61.         }
  62.         else if (q == 4)
  63.         {
  64.             Traversal_Symmetric(tree);
  65.             cout << endl;
  66.         }
  67.         else if (q == 5)
  68.             cout << Height(tree);
  69.         else if (q == 6)
  70.             Clean(tree);
  71.         else if (q == 7)
  72.             Output(tree, 0);
  73.         else if (q == 8)
  74.         {
  75.             int elem;
  76.             cin >> elem;
  77.             Search(tree, elem);
  78.         }
  79.         else if (q == 9)
  80.         {
  81.             int elem;
  82.             cin >> elem;
  83.             Delete(tree, elem);
  84.         }
  85.         else if (q == 10)
  86.         {
  87.             int elem;
  88.             cin >> elem;
  89.             cout << "Адресс элемента " << elem << " равен: " << Search_Elem(tree, elem) << endl;
  90.         }
  91.  
  92.     } while (q != 0);
  93. }
  94.  
  95. void Initial(Node*& tree)
  96. {
  97.     int elem;
  98.     tree = nullptr;
  99.     cout << "Введите ключи дерева:" << endl;
  100.     cin >> elem;
  101.     while (elem != 0)
  102.     {
  103.         Search(tree, elem);
  104.         cin >> elem;
  105.     }
  106. }
  107.  
  108. void Search(Node*& tree, int elem)
  109. {
  110.     if (tree == nullptr)
  111.     {
  112.         tree = new Node;
  113.         tree->Key = elem;
  114.         tree->Count = 1;
  115.         tree->Left = tree->Right = nullptr;
  116.     }
  117.     else
  118.     {
  119.         if (elem < tree->Key)
  120.         {
  121.             Search(*&tree->Left, elem);
  122.         }
  123.         else if (elem > tree->Key)
  124.         {
  125.             Search(*&tree->Right, elem);
  126.         }
  127.         else tree->Count++;
  128.     }
  129. }
  130.  
  131. void Traversal_Straight(Node* tree)
  132. {
  133.     if (tree != nullptr)
  134.     {
  135.         cout << tree->Key << ' ';
  136.         Traversal_Straight(tree->Left);
  137.         Traversal_Straight(tree->Right);
  138.     }
  139. }
  140.  
  141. void Traversal_Back(Node* tree)
  142. {
  143.     if (tree != nullptr)
  144.     {
  145.         Traversal_Back(tree->Left);
  146.         Traversal_Back(tree->Right);
  147.         cout << tree->Key << ' ';
  148.     }
  149. }
  150.  
  151. void Traversal_Symmetric(Node* tree)
  152. {
  153.     if (tree != nullptr)
  154.     {
  155.         Traversal_Symmetric(tree->Left);
  156.         cout << tree->Key << ' ';
  157.         Traversal_Symmetric(tree->Right);
  158.     }
  159. }
  160.  
  161. int Height(Node* tree)
  162. {
  163.     int h1, h2;
  164.     if (tree == nullptr)
  165.         return -1;
  166.     else
  167.     {
  168.         h1 = Height(tree->Left);
  169.         h2 = Height(tree->Right);
  170.         if (h1 > h2)
  171.             return (h1 + 1);
  172.         else return (h2 + 1);
  173.     }
  174. }
  175.  
  176. void Clean(Node* tree)
  177. {
  178.     if (tree != nullptr)
  179.     {
  180.         Clean(tree->Left);
  181.         Clean(tree->Right);
  182.         delete tree;
  183.     }
  184. }
  185.  
  186. void Output(Node* tree, int l)
  187. {
  188.     if (tree != nullptr)
  189.     {
  190.         Output(tree->Right, l + 1);
  191.         for (int i = 1; i <= l; i++)
  192.             cout << "     ";
  193.         cout << tree->Key << endl;
  194.         Output(tree->Left, l + 1);
  195.     }
  196. }
  197.  
  198. void Delete(Node*& tree, int elem)
  199. {
  200.     Node* tmp;
  201.  
  202.     if (tree == nullptr)
  203.         cout << "Такой вершины не найдено" << endl;
  204.     else if (elem < tree->Key)
  205.         Delete(tree->Left, elem);
  206.     else if (elem > tree->Key)
  207.         Delete(tree->Right, elem);
  208.     else
  209.     {
  210.         tmp = tree;
  211.         if (tmp->Right == nullptr)
  212.         {
  213.             tree = tmp->Left;
  214.             delete tmp;
  215.         }
  216.         else if (tmp->Left == nullptr)
  217.         {
  218.             tree = tmp->Right;
  219.             delete tmp;
  220.         }
  221.         else Delete_1(tmp->Left, tmp);
  222.     }
  223. }
  224.  
  225. void Delete_1(Node*& tmp1, Node*& tmp2)
  226. {
  227.     Node* tmp3;
  228.     if (tmp1->Right == nullptr)
  229.     {
  230.         tmp2->Key = tmp1->Key;
  231.         tmp2->Count = tmp1->Count;
  232.         tmp2 = tmp1;
  233.         tmp3 = tmp1;
  234.         tmp1 = tmp1->Left;
  235.         delete tmp3;
  236.     }
  237.     else Delete_1(tmp1->Right, tmp2);
  238. }
  239.  
  240. Node* Search_Elem(Node* tree, int elem)
  241. {
  242.     if (tree == nullptr)
  243.         return tree;
  244.     else if (elem == tree->Key)
  245.         return tree;
  246.     else if (elem < tree->Key)
  247.         return Search_Elem(tree->Left, elem);
  248.     else return Search_Elem(tree->Right, elem);
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement