Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.94 KB | None | 0 0
  1. #define NOMINMAX
  2. #include <iostream>
  3. #include <limits>
  4. #include <string>
  5. #include <conio.h>
  6. #include <Windows.h>
  7. using namespace std;
  8. struct Tree {
  9.     int key;
  10.     string inf;
  11.     Tree *left;
  12.     Tree *right;
  13. };
  14. bool isKeyUnique(Tree *tree, int key);
  15. int menu();
  16. void addElement(Tree *&tree, string a, int b);
  17. void deleteTree(Tree *&tree);
  18. void exitProgram();
  19. void inputNumber(int &a);
  20. void searchMinAndMaxElements(Tree *tree, int &min, int &max);
  21. void searchNearestElements(Tree *tree, int average, int &dif, Tree *&temp);
  22. void showTree(Tree *tree, int level, bool isEmpty);
  23. int main() {
  24.     Tree *root = NULL, *temp;
  25.     string a;
  26.     int b, min, max, average, dif;
  27.     bool isEmpty = true;
  28.     while (true) {
  29.         switch (menu()) {
  30.         case 1:
  31.             cout << "Enter the string" << endl;
  32.             cin >> a;
  33.             cout << "Enter the key" << endl;
  34.             inputNumber(b);
  35.             addElement(root,a,b);
  36.             cout << "Element added" << endl;
  37.             break;
  38.         case 2:
  39.             cout << "\t\tTree" << endl;
  40.             showTree(root, 0, isEmpty);
  41.             if (isEmpty) {
  42.                 cout << "Tree is empty" << endl;
  43.             }
  44.             break;
  45.         case 3:
  46.             deleteTree(root);
  47.             cout << "Tree deleted" << endl;
  48.             break;
  49.         case 4:
  50.             min = root->key;
  51.             max = root->key;
  52.             temp = root;
  53.             searchMinAndMaxElements(root, min, max);
  54.             cout << "min - " << min << endl;
  55.             cout << "max - " << max << endl;
  56.             average = (min + max) / 2;
  57.             cout << "av = "<<average << endl;
  58.             dif = root->key - average;
  59.             searchNearestElements(root, average, dif, temp);
  60.             cout << "Nearest to middle = " << temp->key << endl;
  61.             break;
  62.         case 5:
  63.             exitProgram();
  64.             break;
  65.         default:
  66.             cout << "Choose 1-5!" << endl;
  67.             break;
  68.         }
  69.     }
  70.     return 0;
  71. }
  72. int menu() {
  73.     int choise;
  74.     cout << "1 - add" << endl;
  75.     cout << "2 - show" << endl;
  76.     cout << "3 - delete tree" << endl;
  77.     cout << "4 - Search the nearest to average element" << endl;
  78.     cout << "5 - exit" << endl;
  79.     inputNumber(choise);
  80.     return choise;
  81. }
  82. void showTree(Tree *tree, int level, bool isEmpty) {
  83.     string str;
  84.     if (tree != NULL) {
  85.         isEmpty = false;
  86.         showTree(tree->right, level+1, isEmpty);
  87.         for (int i = 0; i < level; i++) {
  88.             str += "    ";
  89.         }
  90.         cout << str << tree->inf << " " << tree->key << endl;
  91.         showTree(tree->left, level+1, isEmpty);
  92.     }
  93. }
  94. void addElement(Tree *&tree, string a, int b) {
  95.     if (!isKeyUnique(tree, b)) {
  96.         return;
  97.     }
  98.     if (tree == NULL) {
  99.         tree = new Tree;
  100.         tree->inf = a;
  101.         tree->key = b;
  102.         tree->left = NULL;
  103.         tree->right = NULL;
  104.     }
  105.     else if (b<tree->key){
  106.         addElement(tree->left, a, b);
  107.     }
  108.     else {
  109.         addElement(tree->right, a, b);
  110.     }  
  111. }
  112. void deleteTree(Tree *&tree) {
  113.     if (tree != NULL) {
  114.         deleteTree(tree->left);
  115.         deleteTree(tree->right);
  116.         delete tree;
  117.         tree = NULL;
  118.     }
  119. }
  120. bool isKeyUnique(Tree *tree, int key) {
  121.     bool flag = true;
  122.     while (tree && flag) {
  123.         if (key == tree->key) {
  124.             flag = false;
  125.             cout << "Such key already exists" << endl;
  126.         }
  127.         else {
  128.             if (key < tree->key) {
  129.                 tree = tree->left;
  130.             }
  131.             else {
  132.                 tree = tree->right;
  133.             }
  134.         }
  135.     }
  136.     return flag;
  137. }
  138. void inputNumber(int &a) {
  139.     while (true) {
  140.         cin >> a;
  141.         if (cin.get() == '\n') {
  142.             break;
  143.         }
  144.         cout << "Wrong input. Please, try again!" << endl;
  145.         cin.clear();
  146.         cin.ignore(numeric_limits<streamsize>::max(), '\n');
  147.     }
  148. }
  149. void exitProgram() {
  150.     cout << "Press Enter if you want to exit" << endl;
  151.     if (_getch() == 13) {
  152.         exit(0);
  153.     }
  154. }
  155. void searchMinAndMaxElements(Tree *tree, int &min, int &max) {
  156.     if (tree != NULL) {
  157.         searchMinAndMaxElements(tree->right, min, max);
  158.         if (tree->key > max) {
  159.             max = tree->key;
  160.         }
  161.         else if (tree->key < min) {
  162.             min = tree->key;
  163.         }
  164.         searchMinAndMaxElements(tree->left, min, max);
  165.     }
  166. }
  167. void searchNearestElements(Tree *tree, int mid, int &dif, Tree *&temp) {
  168.     if (tree != NULL) {
  169.         searchNearestElements(tree->right, mid, dif, temp);
  170.         if (abs(dif) > abs(tree->key - mid)) {
  171.             dif = abs(tree->key - mid);
  172.             temp = tree;
  173.         }
  174.         searchNearestElements(tree->left, mid, dif, temp);
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement