Advertisement
Vladislav_Bezruk

Untitled

Oct 16th, 2021
1,039
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. #include<iostream>
  2. #include <time.h>
  3. using namespace std;
  4.  
  5. struct Tree {
  6.     string name;
  7.     string number;
  8.     Tree* right = NULL;
  9.     Tree* left = NULL;
  10. };
  11.  
  12. void Add(string aName, string aNumber, Tree*& root) {// * - a iaei? noai?eii iiea??ee ia ei??iu aa?aaa, & - uia i?ao?aaoe c i?ea?iaeii ? ci?i?aaoe eiai
  13.     if (root == NULL) {
  14.         root = new Tree;
  15.         root->name = aName;
  16.         root->number = aNumber;
  17.         return;
  18.     }
  19.     if (aName > root->name) {
  20.         return Add(aName, aNumber, root->right);
  21.     }
  22.     return Add(aName, aNumber, root->left);
  23. }
  24.  
  25. string Search(string aName, Tree* root) {// iiea??ee ia ei??iu
  26.     if (root == NULL) {
  27.         return "This name isn`t in list";
  28.     }
  29.     if (aName == root->name) {
  30.         return root->number;
  31.     }
  32.  
  33.     if (aName > root->name) {
  34.         return Search(aName, root->right);
  35.     }
  36.     return Search(aName, root->left);
  37. }
  38.  
  39. Tree* Delete(string aName, Tree*& root) {
  40.     if (root == NULL) {
  41.         cout << "This name isn`t in list" << endl;
  42.         return root;
  43.     }
  44.     if (root->name == aName) {
  45.         Tree* tmp;
  46.  
  47.         if (root->right == NULL) {
  48.             tmp = root->left;
  49.         }
  50.         else {
  51.             Tree* ptr = root->right;
  52.  
  53.             if (ptr->left == NULL) {
  54.                 ptr->left = root->right;
  55.                 tmp = ptr;
  56.             }
  57.             else {
  58.                 Tree* pMin = ptr->left;
  59.  
  60.                 while (pMin->left) {
  61.                     ptr = pMin;
  62.                     pMin = ptr->left;
  63.                 }
  64.                 ptr->left = pMin->right;
  65.                 pMin->left = root->left;
  66.                 pMin->right = root->right;
  67.                 tmp = pMin;
  68.             }
  69.  
  70.         }
  71.  
  72.         delete root;
  73.         return tmp;
  74.     }
  75. }
  76. int main() {
  77.     Tree* a = NULL;
  78.     Add("Nik", "123456", a);
  79.     cout << Search("Nik", a) << endl;
  80.     Add("Onf", "234567", a);
  81.     Add("Dih", "4637468274", a);
  82.     cout << Search("Onf", a) << endl;
  83.     cout << Search("Dih", a) << endl;
  84.  
  85.     Delete("Nik", a);
  86.  
  87.     Delete("Onf", a);
  88.  
  89.     cout << Search("Onf", a) << endl;
  90.     cout << Search("Nik", a) << endl;
  91.  
  92.     cout << Search("Dih", a) << endl;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement