Vladislav_Bezruk

tree

Oct 19th, 2021 (edited)
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3.  
  4. using namespace std;
  5.  
  6. struct Tree {
  7.     string name;
  8.     string number;
  9.     Tree* right = NULL;
  10.     Tree* left = NULL;
  11. };
  12.  
  13. Tree* 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
  14.     if (root == NULL) {
  15.         root = new Tree;
  16.         root->name = aName;
  17.         root->number = aNumber;
  18.         return root;
  19.     }
  20.     if (aName > root->name) {
  21.         root->right = Add(aName, aNumber, root->right);
  22.     }
  23.     else {
  24.         root->left = Add(aName, aNumber, root->left);
  25.     }
  26.     return root;
  27. }
  28.  
  29. string Search(string aName, Tree* root) {// iiea??ee ia ei??iu
  30.     if (root == NULL) {
  31.         return "This name isn`t in list";
  32.     }
  33.     if (aName == root->name) {
  34.         return root->number;
  35.     }
  36.     if (aName > root->name) {
  37.         return Search(aName, root->right);
  38.     }
  39.     return Search(aName, root->left);
  40. }
  41.  
  42. Tree* SearchMin(Tree* root) {
  43.     if (root->left) {
  44.         return SearchMin(root->left);
  45.     }
  46.     return root;
  47. }
  48.  
  49. Tree* Delete(string aName, Tree* root) {
  50.     if (root == NULL) {
  51.         cout<<"This name isn`t in list"<<endl;
  52.         return root;
  53.     }
  54.     if (root->name == aName) {
  55.         if (root->left && root->right) {
  56.             Tree* min = SearchMin(root->right);
  57.             root->name = min->name;
  58.             root->number = min->number;
  59.             root->right = Delete(min->name, root->right);
  60.             return root;
  61.         }
  62.         if (root->left) {
  63.             root->name = root->left->name;
  64.             root->number = root->left->number;
  65.             root->left = Delete(root->left->name, root->left);
  66.             return root;
  67.         }
  68.         if (root->right) {
  69.             root->name = root->right->name;
  70.             root->number = root->right->number;
  71.             root->right = Delete(root->right->name, root->right);
  72.             return root;
  73.         }
  74.         root = NULL;
  75.         return root;
  76.     }
  77.     if (root->name > aName) {
  78.         root->right = Delete(aName, root->right);
  79.     }
  80.     else {
  81.         root->left = Delete(aName, root->left);
  82.     }
  83.     return root;
  84. }
  85.  
  86. int main() {
  87.     Tree* a = NULL;
  88.  
  89.     a = Add("Nik", "123456", a);
  90.     a = Add("Onf", "234567", a);
  91.     a = Add("Dih", "4637468274", a);
  92.  
  93.     cout << Search("Nik", a) << endl;
  94.     cout << Search("Onf", a) << endl;
  95.     cout << Search("Dih", a) << endl;
  96.  
  97.     a = Delete("Nik", a);
  98.  
  99.     if (!a) cout << "null" << endl;
  100.  
  101.     cout << Search("Nik", a) << endl;
  102.     cout << Search("Onf", a) << endl;
  103.     cout << Search("Dih", a) << endl;
  104.  
  105.     a = Delete("Onf", a);
  106.  
  107.     cout << Search("Nik", a) << endl;
  108.     cout << Search("Onf", a) << endl;
  109.     cout << Search("Dih", a) << endl;
  110.  
  111.     a = Delete("Dih", a);
  112.  
  113.     cout << Search("Nik", a) << endl;
  114.     cout << Search("Onf", a) << endl;
  115.     cout << Search("Dih", a) << endl;
  116.  
  117.     return 0;
  118. }
Add Comment
Please, Sign In to add comment