Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. struct tree {
  7. int k, l, r, p;
  8. };
  9. int i, x;
  10. tree v[20000005];
  11.  
  12.  
  13. void ins(int x, int j, int kol)
  14. {
  15. if (v[j].k > x && v[j].l == 0)
  16. {
  17. v[j].l = kol;
  18. v[kol].k = x;
  19. v[kol].p = j;
  20. exit;
  21. }
  22. if (v[j].k < x && v[j].r == 0)
  23. {
  24. v[j].r = kol;
  25. v[kol].k = x;
  26. v[kol].p = j;
  27. exit;
  28. }
  29. if (v[j].k > x)
  30. ins(x, v[j].l, kol);
  31. if (v[j].k < x)
  32. ins(x, v[j].r, kol);
  33. }
  34.  
  35. void del(int j, int x)
  36. {
  37. if (x > v[j].k)
  38. {
  39. if (v[j].r != 0)
  40. del(v[j].r, x);
  41. return;
  42. }
  43. if (x < v[j].k)
  44. {
  45. if (v[j].l != 0)
  46. del(v[j].l, x);
  47. return;
  48. }
  49. if (v[j].l == 0 && v[j].r == 0)
  50. {
  51. if (v[v[j].p].l == j)
  52. v[v[j].p].l = 0;
  53. else
  54. v[v[j].p].r = 0;
  55. return;
  56. }
  57. if (v[j].r == 0)
  58. {
  59. if (v[v[j].p].l == j)
  60. v[v[j].p].l = v[j].l;
  61. else
  62. v[v[j].p].r = v[j].l;
  63. return;
  64. }
  65. else
  66. {
  67. int h = v[j].r;
  68. while (v[h].l != 0)
  69. h = v[h].l;
  70. v[j].k = v[h].k;
  71. del(h, v[h].k);
  72. }
  73. }
  74.  
  75. bool ex(int x, int j)
  76. {
  77. if (v[j].k == x) return 1;
  78. if ((x > v[j].k and v[j].r == 0) || (x < v[j].k and v[j].l == 0)) return 0;
  79. if (x > v[j].k) return ex(x, v[j].r);
  80. if (x < v[j].k) return ex(x, v[j].l);
  81. }
  82.  
  83.  
  84. int main()
  85. {
  86. ios_base::sync_with_stdio(false);
  87. cin.tie(NULL);
  88. cout.tie(NULL);
  89. //freopen("bstsimple.in", "r", stdin);
  90. //freopen("bstsimple.out", "w", stdout);
  91. string str;
  92. int kol = 0;
  93. while (cin >> str)
  94. {
  95. cin >> x;
  96. if (str == "insert")
  97. {
  98. kol++;
  99. if (kol == 1)
  100. {
  101. v[kol].k = x;
  102. continue;
  103. }
  104. ins(x, 1, kol);
  105. }
  106. if (str == "delete")
  107. {
  108. del(1, x);
  109. }
  110.  
  111. if (str == "exists")
  112. {
  113. if (ex(x, 1))
  114. cout << "true\n";
  115. else
  116. cout << "false\n";
  117. }
  118.  
  119. if (str == "next")
  120. {
  121. bool f = 1, f2 = 1;
  122. int j = 1, suc = 0;
  123. while (j != 0)
  124. if (v[j].k > x)
  125. {
  126. f2 = 0;
  127. suc = j;
  128. j = v[j].l;
  129. }
  130. else if (v[j].r == 0 && f2)
  131. {
  132. f = 0;
  133. cout << "none\n";
  134. break;
  135. }
  136. else
  137. j = v[j].r;
  138. if (f)
  139. cout << v[suc].k <<"\n";
  140. }
  141.  
  142. if (str == "prev")
  143. {
  144. bool f = 1, f2 = 1;
  145. int j = 1, suc = 0;
  146. while (j != 0)
  147. if (v[j].k < x)
  148. {
  149. f2 = 0;
  150. suc = j;
  151. j = v[j].r;
  152. }
  153. else if (v[j].l == 0 && f2)
  154. {
  155. f = 0;
  156. cout << "none\n";
  157. break;
  158. }
  159. else
  160. j = v[j].l;
  161. if (f)
  162. cout << v[suc].k << "\n";
  163. }
  164. }
  165. return 0;
  166. }
  167. /*
  168. insert 100
  169. insert 50
  170. insert 150
  171. insert 130
  172. insert 140
  173. insert 120
  174. delete 130
  175. insert 140
  176. insert 110
  177. insert 135
  178. insert 145
  179. insert 136
  180. insert 134
  181. insert 132
  182. insert 125
  183. insert 105
  184. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement