Vladislav_Bezruk

binary search tree

Oct 2nd, 2021 (edited)
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <conio.h>
  5.  
  6. using namespace std;
  7.  
  8. struct Node {
  9.     string key, value;
  10.     Node *right = NULL, *left = NULL;
  11.     Node(string a, string b) : key(a), value(b) {}
  12. };
  13.  
  14. Node** treeSearch(Node** tree, string key) {
  15.     if (*tree == NULL || (*tree) -> key == key) return tree;
  16.     if (key > (*tree) -> key) return treeSearch(&((*tree) -> right), key);
  17.     return treeSearch(&((*tree) -> left), key);
  18. }
  19.  
  20. void treeInsert(Node** tree, string key, string value) {
  21.     if (*tree == NULL) { *tree = new Node(key, value); return; }
  22.     if ((*tree) -> key > key) { treeInsert(&((*tree) -> left), key, value); return; }
  23.     treeInsert(&((*tree) -> right), key, value);
  24. }
  25.  
  26. Node** treeMin(Node** tree) {
  27.     if ((*tree) -> left == NULL) return tree;
  28.     return treeMin(&((*tree) -> left));
  29. }
  30.  
  31. void treeDelete(Node** tree) {
  32.     if ((*tree) -> left && (*tree) -> right) {
  33.         Node** p = treeMin(&((*tree) -> right));
  34.         (*tree) -> key = (*p) -> key;
  35.         (*tree) -> value = (*p) -> value;
  36.         treeDelete(p);
  37.         return;
  38.     }
  39.     Node* p;
  40.     if ((*tree) -> left || (*tree) -> right) {
  41.         if ((*tree) -> left) {
  42.             p = (*tree) -> left;
  43.             **tree = *((*tree) -> left);
  44.         } else {
  45.             p = (*tree) -> right;
  46.             **tree = *((*tree) -> right);
  47.         }
  48.         delete p;
  49.         return;
  50.     }
  51.     delete *tree;
  52.     *tree = NULL;
  53. }
  54.  
  55. void treePrint(Node* tree) {
  56.     if (tree == NULL) return;
  57.     treePrint(tree -> left);
  58.     cout << tree -> key << ": " << tree -> value << endl;
  59.     treePrint(tree -> right);
  60. }
  61.  
  62. int main() {
  63.     string command, surn, phone;
  64.     Node* tree = NULL;
  65.    
  66.     while (1) {
  67.         system("CLS");
  68.         cout << "===Telephone directory based on binary search tree===" << endl << endl;
  69.        
  70.         cout << "add 'surname' 'phone' - add new contact" << endl;
  71.         cout << "remove 'surname' - remove contact" << endl;
  72.         cout << "search 'surname' - search contact" << endl;
  73.         cout << "print - print all contacts" << endl;
  74.         cout << "exit - close program" << endl;
  75.        
  76.         cout << endl << "<- ";
  77.        
  78.         cin.clear();
  79.         cin >> command;
  80.        
  81.         if (command == "exit") break;
  82.         if (command == "print") {
  83.             cout << endl << "All contacts:" << endl;
  84.             treePrint(tree);
  85.             if (tree == NULL) cout << "empty" << endl;
  86.         }
  87.         if (command == "search") {
  88.             cin.clear();
  89.             cin >> surn;
  90.             Node* p = *treeSearch(&tree, surn);
  91.             if (p) cout << endl << p -> key << ": " << p -> value << endl;
  92.             else cout << endl << "there is not" << endl;       
  93.         }
  94.         if (command == "remove") {
  95.             cin.clear();
  96.             cin >> surn;
  97.             Node** p = treeSearch(&tree, surn);
  98.            
  99.             if (*p) {
  100.                 treeDelete(p); 
  101.                 cout << endl << "Removed" << endl;
  102.             } else cout << endl << "there is not" << endl;         
  103.         }
  104.         if (command == "add") {
  105.             cin.clear();
  106.             cin >> surn >> phone;
  107.             if (*treeSearch(&tree, surn) == NULL) {
  108.                 treeInsert(&tree, surn, phone);
  109.                 cout << endl << "Aded" << endl;
  110.             } else cout << endl << "already aded" << endl;
  111.         }  
  112.         getch();
  113.     }
  114.     return 0;
  115. }
Add Comment
Please, Sign In to add comment