Advertisement
snowywhitee

Untitled

Dec 7th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. using namespace std;
  6.  
  7. struct node {
  8.     int key;
  9.     node *l;
  10.     node *r;
  11.     node *prev;
  12. };
  13.  
  14. node* find(node *peak, int key) {
  15.     if (peak == NULL || peak->key == key)
  16.     {
  17.         return peak;
  18.     }
  19.     if (key < peak->key)
  20.     {
  21.         return find(peak->l, key);
  22.     } else {
  23.         return find(peak->r, key);
  24.     }
  25. }
  26.  
  27. void insert(node* &root, int key) {
  28.     if (find(root, key) == NULL)
  29.     {
  30.         node *q;
  31.         q = new node();
  32.         q->key = key;
  33.         q->l = NULL;
  34.         q->r = NULL;
  35.         q->prev = NULL;
  36.         node *cur = root;
  37.      
  38.         while (cur != NULL)
  39.         {
  40.             if (key == cur->key)
  41.             {
  42.                 return;
  43.             }
  44.             if (key > cur->key)
  45.             {
  46.                 if (cur->r != NULL)
  47.                 {
  48.                     cur = cur->r;
  49.                 } else {
  50.                     q->prev = cur;
  51.                     cur->r = q;
  52.                     return;
  53.                 }
  54.             } else if (key < cur->key)
  55.             {
  56.                 if (cur->l != NULL) {
  57.                     cur = cur->l;
  58.                 } else {
  59.                     q->prev = cur;
  60.                     cur->l = q;
  61.                     return;
  62.                 }
  63.             }
  64.         }
  65.      
  66.         root = q;
  67.     }
  68.  
  69. }
  70.  
  71. node* next(node *root, int x)
  72. {
  73.     node *cur = root;
  74.     node *result = NULL;
  75.     while(cur != NULL)
  76.     {
  77.         if (cur->key > x)
  78.         {
  79.             result = cur;
  80.             cur = cur->l;
  81.         } else {
  82.             cur = cur->r;
  83.         }
  84.     }
  85.     return result;
  86. }
  87.  
  88. void del(node* &root, int key)
  89. {
  90.     node* peak = find(root, key);
  91.     if (peak == NULL)
  92.     {
  93.         return;
  94.     }
  95.  
  96.     if (peak == root) {
  97.         if (peak->l == NULL && peak->r == NULL)
  98.         {
  99.             root = NULL;
  100.             delete peak;
  101.             return;
  102.         }
  103.  
  104.         if (peak->l == NULL || peak->r == NULL)
  105.         {
  106.             if (peak->l == NULL)
  107.             {
  108.                 root = peak->r;
  109.             } else {
  110.                 root = peak->l;
  111.             }
  112.             delete peak;
  113.             return;
  114.         }
  115.     }
  116.  
  117.     if (peak->l == NULL && peak->r == NULL) {
  118.         if (peak->prev->l == peak)
  119.         {
  120.             peak->prev->l = NULL;
  121.         }
  122.         if (peak->prev->r == peak)
  123.         {
  124.             peak->prev->r = NULL;
  125.         }
  126.         delete peak;
  127.         return;
  128.     }
  129.  
  130.     if (peak->l == NULL || peak->r ==  NULL) {
  131.         if (peak->l == NULL) {
  132.             if (peak->prev->l == peak)
  133.             {
  134.                 peak->prev->l = peak->r;
  135.             } else {
  136.                 peak->prev->r = peak->r;
  137.             }
  138.             peak->r->prev = peak->prev;
  139.         } else {
  140.             if (peak->prev->l == peak)
  141.             {
  142.                 peak->prev->l = peak->l;
  143.             } else {
  144.                 peak->prev->r = peak->l;
  145.             }
  146.             peak->l->prev = peak->prev;
  147.         }
  148.         delete peak;
  149.         return;
  150.     }
  151.  
  152.     node* cur = next(root, key);
  153.     peak->key = cur->key;
  154.     if (cur->prev->l == cur)
  155.     {
  156.         cur->prev->l = cur->r;
  157.         if (cur->r != NULL)
  158.         {
  159.             cur->r->prev = cur->prev;
  160.         }
  161.     } else {
  162.         cur->prev->r = cur->r;
  163.         if (cur->r != NULL)
  164.         {
  165.             cur->r->prev = cur->prev;
  166.         }
  167.     }
  168.     delete cur;
  169. }
  170.  
  171. node* prev(node *root, int x)
  172. {
  173.     node *cur = root;
  174.     node *result = NULL;
  175.     while(cur != NULL)
  176.     {
  177.         if (cur->key >= x)
  178.         {
  179.             cur = cur->l;
  180.         } else {
  181.             result = cur;
  182.             cur = cur->r;
  183.         }
  184.     }
  185.     return result;
  186. }
  187.  
  188.  
  189. int main()
  190. {
  191.     node *root = NULL;
  192.     ifstream fin("bstsimple.in");
  193.     ofstream fout("bstsimple.out");
  194.     string command;
  195.     int x;
  196.     while( fin >> command )
  197.     {
  198.         fin >> x;
  199.         cout << command << " " << x << ", res: ";
  200.         if (command == "insert")
  201.         {
  202.             insert(root, x);
  203.         } else if (command == "delete")
  204.         {
  205.             del(root, x);
  206.         } else if (command == "exists")
  207.         {
  208.             if (find(root, x) != NULL)
  209.             {
  210.                 fout << "true\n";
  211.             } else {
  212.                 fout << "false\n";
  213.             }
  214.         } else if (command == "prev")
  215.         {
  216.             node *q = prev(root, x);
  217.             if (q != NULL)
  218.             {
  219.                 fout << q->key << endl;
  220.             } else {
  221.                 fout << "none\n";
  222.             }
  223.         } else if (command == "next")
  224.         {
  225.             node *q = next(root, x);
  226.             if (q != NULL)
  227.             {
  228.                 fout << q->key << endl;
  229.             } else {
  230.                 fout << "none\n";
  231.             }
  232.         }
  233.     }
  234.  
  235.     return 0;
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement