Advertisement
snowywhitee

Untitled

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