Advertisement
Marisichka

Untitled

Oct 3rd, 2021
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. class BinarySearchTree{
  7.     private:
  8.         struct tree_node{
  9.            tree_node* left;
  10.            tree_node* right;
  11.            int data;
  12.            string name;
  13.         };
  14.         tree_node* root;
  15.     public:
  16.         BinarySearchTree(){
  17.            root = NULL;
  18.         }
  19.         bool isEmpty() const { return root==NULL; }        
  20.         void print_preorder();
  21.         void preorder(tree_node*);  
  22.         void insert(int d, string  n);
  23.         void remove(string  n);
  24.         void find(string  key);      
  25. };
  26. void BinarySearchTree::insert(int d, string  n){
  27.     tree_node* t = new tree_node;
  28.     tree_node* parent;
  29.     t->data = d;
  30.     t->name = n;
  31.     t->left = NULL;
  32.     t->right = NULL;
  33.     parent = NULL;
  34.     if(isEmpty()) root = t;
  35.     else{//åñëè äåðåâî íå ïóñòîå -> ñîçäàåì ïîääåðåâî
  36.         tree_node* curr;
  37.         curr = root;
  38.         while(curr){
  39.             parent = curr;
  40.             if(t->name > curr->name) curr = curr->right;
  41.             else curr = curr->left;
  42.         }
  43.         if(t->name < parent->name)parent->left = t;
  44.         else parent->right = t;
  45.     }
  46. }
  47. void BinarySearchTree::remove(string  n){
  48.     bool found = false;
  49.     if(isEmpty()){
  50.         cout<<" The telephone book is empty! "<<endl;
  51.         return;
  52.     }
  53.     tree_node* curr;
  54.     tree_node* parent;
  55.     curr = root;
  56.     while(curr != NULL){
  57.          if(curr->name == n){
  58.             found = true;
  59.             break;
  60.          }
  61.          else{
  62.              parent = curr;
  63.              if(n>curr->name) curr = curr->right;
  64.              else curr = curr->left;
  65.          }
  66.     }
  67.     if(!found){
  68.         cout<<" The contact wasn`t found! "<<endl;
  69.         return;
  70.     }
  71.     if((curr->left == NULL && curr->right != NULL)|| (curr->left != NULL && curr->right == NULL)){
  72.        if(curr->left == NULL && curr->right != NULL){
  73.           if(parent->left == curr){
  74.              parent->left = curr->right;
  75.              delete curr;
  76.           }
  77.           else{
  78.              parent->right = curr->right;
  79.              delete curr;
  80.           }
  81.        }
  82.        else{
  83.           if(parent->left == curr){
  84.              parent->left = curr->left;
  85.              delete curr;
  86.           }
  87.           else{
  88.              parent->right = curr->left;
  89.              delete curr;
  90.           }
  91.        }
  92.      return;
  93.     }
  94. //óçåë áåç äåòåé
  95.    if( curr->left == NULL && curr->right == NULL){
  96.         if(parent->left == curr) parent->left = NULL;
  97.         else parent->right = NULL;
  98.         delete curr;
  99.         return;
  100.     }
  101. //óçåë ñ äâóìÿ äåòüìè
  102.     if (curr->left != NULL && curr->right != NULL){
  103.         tree_node* chkr;
  104.         chkr = curr->right;
  105.         if((chkr->left == NULL) && (chkr->right == NULL)){
  106.             curr = chkr;
  107.             delete chkr;
  108.             curr->right = NULL;
  109.         }
  110.         else{
  111.             if((curr->right)->left != NULL){
  112.                 tree_node* lcurr;
  113.                 tree_node* lcurrp;
  114.                 lcurrp = curr->right;
  115.                 lcurr = (curr->right)->left;
  116.                 while(lcurr->left != NULL){
  117.                    lcurrp = lcurr;
  118.                    lcurr = lcurr->left;
  119.                 }
  120.                 curr->name = lcurr->name;
  121.                 curr->data = lcurr->data;
  122.                 delete lcurr;
  123.                 lcurrp->left = NULL;
  124.             }
  125.             else{
  126.                tree_node* tmp;
  127.                tmp = curr->right;
  128.                curr->name = tmp->name;
  129.                curr->data = tmp->data;
  130.                curr->right = tmp->right;
  131.                delete tmp;
  132.            }
  133.         }
  134.          return;
  135.     }
  136. }
  137. void BinarySearchTree::print_preorder(){
  138.     preorder(root);
  139. }
  140.  
  141. void BinarySearchTree::preorder(tree_node* p){
  142.     if(p != NULL){
  143.         cout<<" "<<p->name<<" : "<<p->data<<endl;
  144.         if(p->left) preorder(p->left);
  145.         if(p->right) preorder(p->right);
  146.     }
  147.     else return;
  148. }
  149. void BinarySearchTree::find(string  key){
  150.     bool found = false;
  151.     if(isEmpty()){
  152.         cout<<" The telephone book is empty "<<endl;
  153.         return;
  154.     }
  155.     tree_node* curr;
  156.     tree_node* parent;
  157.     curr = root;
  158.     while(curr != NULL){
  159.          if(curr->name == key){
  160.             found = true;
  161.             cout << "Number " << key << " - " << curr->data << endl;
  162.             break;
  163.          }
  164.          else{
  165.              parent = curr;
  166.              if(key>curr->name) curr = curr->right;
  167.              else curr = curr->left;
  168.          }
  169.     }
  170.     if(!found){
  171.         cout<<" The contact wasn`t found! "<<endl;
  172.         return;
  173.     }
  174. }
  175.  
  176. int main()
  177. {
  178.     BinarySearchTree b;
  179.     int ch,tmp,n;
  180.     string  n1, n2;
  181.     while(1)
  182.     {
  183.        cout<< endl;
  184.        cout<<" 1. Add a new member "<<endl;
  185.        cout<<" 2. Tree output "<<endl;
  186.        cout<<" 3. Delete member "<<endl;
  187.        cout<<" 4. Search the number "<<endl;
  188.         cout<<" Your choice : ";
  189.        cin>>ch;
  190.        switch(ch)
  191.        {
  192.            case 1 : cout<<" Enter the name: ";
  193.                     cin>>n1;
  194.                     cout<<" Enter the number: ";
  195.                     cin>>tmp;
  196.                     b.insert(tmp,n1);
  197.                     break;
  198.          
  199.            case 2 : cout<<endl;
  200.                     cout<<" Tree output: "<<endl;                    
  201.                     b.print_preorder();
  202.                     break;
  203.          
  204.            case 3 : cout<<" Enter the member to delete: ";
  205.                     cin>>n2;
  206.                     b.remove(n2);
  207.                     break;
  208.            case 4 : cout<<" Enter the name for searching: ";
  209.                     cin>>n2;
  210.                     b.find(n2);
  211.                     break;
  212.        }
  213.     }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement