Advertisement
spacerose

двоичное дерево поиска

Oct 10th, 2020 (edited)
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <Windows.h>
  3. #include <conio.h>
  4. #include <cmath>
  5. #include <string>
  6. using namespace std;
  7. struct BinTree {
  8.     string data;
  9.     BinTree* left;
  10.     BinTree* right;
  11. };
  12.  
  13.     void CreateBinTree(BinTree*& Tree, string dt) {
  14.         if (Tree == NULL)
  15.         {
  16.             Tree = new BinTree;
  17.             Tree->data = dt;
  18.             Tree->left = Tree->right = NULL;
  19.  
  20.             return;
  21.         }
  22.         if (dt.length() >= (Tree->data).length()) CreateBinTree(Tree->right, dt);//Если аргумент больше чем текущий элемент, поместить его вправо
  23.         else CreateBinTree(Tree->left, dt);//Иначе поместить его влево
  24.     }
  25.     void Print(BinTree** Tree, int l)
  26.     {
  27.         int i;
  28.  
  29.         if (*Tree != NULL)
  30.         {
  31.             Print(&((**Tree).right), l + 1);
  32.             for (i = 1; i <= l; i++) cout << "   ";
  33.             cout << (**Tree).data << endl;
  34.             Print(&((**Tree).left), l + 1);
  35.         }
  36.     }
  37.  
  38.     void PreOrder(BinTree* Tree) {
  39.         if (Tree != NULL) {
  40.             cout << Tree->data << endl;
  41.             PreOrder(Tree->left);
  42.             PreOrder(Tree->right);
  43.  
  44.         }
  45.     }
  46.  
  47.     void InOrder(BinTree* Tree) {
  48.         if (Tree != NULL) {
  49.             InOrder(Tree->left);
  50.             cout << Tree->data << endl;
  51.             InOrder(Tree->right);
  52.         }
  53.     }
  54.  
  55.     void AvgValue(BinTree* Tree, float &count, float &sum)
  56.     {
  57.  
  58.         if (Tree != NULL) {
  59.  
  60.             count++;
  61.             sum += (Tree->data).length();
  62.            
  63.             AvgValue(Tree->left,count,sum);
  64.             AvgValue(Tree->right,count,sum);
  65.         }
  66.     }
  67.  
  68.     int Search(BinTree* Tree, string key, int count) {
  69.        
  70.         if (Tree == NULL) return -1;
  71.  
  72.         if ((Tree->data) == key) return count;
  73.         count++;
  74.         if (key.length() < (Tree->data).length()) return Search(Tree->left, key, count);
  75.         else
  76.             return Search(Tree->right, key, count);
  77.     }
  78.  
  79.  
  80.     void Menu() {
  81.  
  82.         BinTree* Tree = NULL;
  83.         string val, key;
  84.         int command;
  85.         float count = 0;
  86.         float sum = 0;
  87.         cout << "Выберете команду:\n1 - Добавление (вставка) элемента\n2 - Вывод дерева\n3 - Прямой обход дерева\n";
  88.         cout << "4 - Симметричный обход дерева\n5 - Вычисление среднего арифметического количества знаков всех узлов дерева\n";
  89.         cout<<"6 - Нахождение длины пути от корня до заданного значения\n7 - Завершение работы программы\n";
  90.         while (true) {
  91.             cout << "\n\n";
  92.             cin >> command;
  93.             switch (command)
  94.             {
  95.             case 1:
  96.                 cout << "Введите значение элемента: ";
  97.                 cin >> val;
  98.                 CreateBinTree(Tree, val);
  99.                 cout << "Элемент добавлен";
  100.                 break;
  101.             case 2:
  102.                 Print(&Tree, 0);
  103.                 break;
  104.             case 3:
  105.                 cout << "Прямой обход дерева:\n";
  106.                 PreOrder(Tree);
  107.                 break;
  108.             case 4:
  109.                 cout << "Cимметричный обход дерева:\n";
  110.                 InOrder(Tree);
  111.                 break;
  112.             case 5:
  113.                 cout << "Среднее арифметическое кол-ва знаков всех узлов дерева = ";
  114.                 AvgValue(Tree, count, sum);
  115.                 cout << sum / count << endl;
  116.                 break;
  117.             case 6:
  118.                 cout << "Введите значение элемента для поиска длины его пути от корня: ";
  119.                 cin >> key;
  120.                 if (Search(Tree, key, 0) == -1) cout << "Элемент не найден";
  121.                 else
  122.                     cout << "Длина пути от корня до заданного значения = " << Search(Tree, key, 0);
  123.                 break;
  124.             case 7:
  125.                 return;
  126.             default:
  127.                 cout << "Такой команды нет!\n";
  128.                 break;
  129.             }
  130.         }
  131.            
  132.         }
  133.  
  134.  
  135. int main() {
  136.     SetConsoleCP(1251);
  137.     SetConsoleOutputCP(1251);
  138.     setlocale(LC_ALL, "");
  139.     Menu();
  140.     system("pause");
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement