Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <Windows.h>
- #include <conio.h>
- #include <cmath>
- #include <string>
- using namespace std;
- struct BinTree {
- string data;
- BinTree* left;
- BinTree* right;
- };
- void CreateBinTree(BinTree*& Tree, string dt) {
- if (Tree == NULL)
- {
- Tree = new BinTree;
- Tree->data = dt;
- Tree->left = Tree->right = NULL;
- return;
- }
- if (dt.length() >= (Tree->data).length()) CreateBinTree(Tree->right, dt);//Если аргумент больше чем текущий элемент, поместить его вправо
- else CreateBinTree(Tree->left, dt);//Иначе поместить его влево
- }
- void Print(BinTree** Tree, int l)
- {
- int i;
- if (*Tree != NULL)
- {
- Print(&((**Tree).right), l + 1);
- for (i = 1; i <= l; i++) cout << " ";
- cout << (**Tree).data << endl;
- Print(&((**Tree).left), l + 1);
- }
- }
- void PreOrder(BinTree* Tree) {
- if (Tree != NULL) {
- cout << Tree->data << endl;
- PreOrder(Tree->left);
- PreOrder(Tree->right);
- }
- }
- void InOrder(BinTree* Tree) {
- if (Tree != NULL) {
- InOrder(Tree->left);
- cout << Tree->data << endl;
- InOrder(Tree->right);
- }
- }
- void AvgValue(BinTree* Tree, float &count, float &sum)
- {
- if (Tree != NULL) {
- count++;
- sum += (Tree->data).length();
- AvgValue(Tree->left,count,sum);
- AvgValue(Tree->right,count,sum);
- }
- }
- int Search(BinTree* Tree, string key, int count) {
- if (Tree == NULL) return -1;
- if ((Tree->data) == key) return count;
- count++;
- if (key.length() < (Tree->data).length()) return Search(Tree->left, key, count);
- else
- return Search(Tree->right, key, count);
- }
- void Menu() {
- BinTree* Tree = NULL;
- string val, key;
- int command;
- float count = 0;
- float sum = 0;
- cout << "Выберете команду:\n1 - Добавление (вставка) элемента\n2 - Вывод дерева\n3 - Прямой обход дерева\n";
- cout << "4 - Симметричный обход дерева\n5 - Вычисление среднего арифметического количества знаков всех узлов дерева\n";
- cout<<"6 - Нахождение длины пути от корня до заданного значения\n7 - Завершение работы программы\n";
- while (true) {
- cout << "\n\n";
- cin >> command;
- switch (command)
- {
- case 1:
- cout << "Введите значение элемента: ";
- cin >> val;
- CreateBinTree(Tree, val);
- cout << "Элемент добавлен";
- break;
- case 2:
- Print(&Tree, 0);
- break;
- case 3:
- cout << "Прямой обход дерева:\n";
- PreOrder(Tree);
- break;
- case 4:
- cout << "Cимметричный обход дерева:\n";
- InOrder(Tree);
- break;
- case 5:
- cout << "Среднее арифметическое кол-ва знаков всех узлов дерева = ";
- AvgValue(Tree, count, sum);
- cout << sum / count << endl;
- break;
- case 6:
- cout << "Введите значение элемента для поиска длины его пути от корня: ";
- cin >> key;
- if (Search(Tree, key, 0) == -1) cout << "Элемент не найден";
- else
- cout << "Длина пути от корня до заданного значения = " << Search(Tree, key, 0);
- break;
- case 7:
- return;
- default:
- cout << "Такой команды нет!\n";
- break;
- }
- }
- }
- int main() {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- setlocale(LC_ALL, "");
- Menu();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement