Advertisement
OMEGAHEAD_MonkoX

Untitled

Oct 20th, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <tuple>
  4. #include <algorithm>
  5. #include <utility>
  6.  
  7. using namespace std;
  8.  
  9. #define ll long long int
  10.  
  11. const ll INF = 9999999999999;
  12.  
  13. struct node {
  14. int left, right, min, end = 0;
  15. node* child_left = nullptr, * child_right = nullptr;
  16. };
  17.  
  18. void print_tree(node* p, int level)
  19.  
  20. {
  21.  
  22. if (p == nullptr)
  23.  
  24. return;
  25.  
  26. print_tree(p->child_left, level + 1);
  27.  
  28. for (int i = 0; i < level; i++)
  29.  
  30. cout << ".";
  31.  
  32. cout << p->end << endl;
  33.  
  34. print_tree(p->child_right, level + 1);
  35.  
  36. }
  37.  
  38. bool update(node* &a, int l)
  39. {
  40. if (a == nullptr)
  41. {
  42. a = new node;
  43. a->end = l;
  44. return true;
  45. }
  46. if (l == a->end)
  47. return false;
  48. else
  49. {
  50. if (l < a->end) {
  51. if (a->child_left != nullptr)
  52. return update(a->child_left, l);
  53. else
  54. {
  55. a->child_left = new node;
  56. a->child_left->end = l;
  57. return true;
  58. }
  59. }
  60. else {
  61. if (a->child_right != nullptr)
  62. return update(a->child_right, l);
  63. else {
  64. a->child_right = new node;
  65. a->child_right->end = l;
  66. return true;
  67. }
  68. }
  69. }
  70. return false;
  71. }
  72.  
  73. bool exist(node* root, int l) {
  74. if (root == nullptr)
  75. return false;
  76. if (root->end == l)
  77. return true;
  78. if (l < root->end)
  79. return exist(root->child_left, l);
  80. else
  81. return exist(root->child_right, l);
  82. }
  83.  
  84. int main()
  85. {
  86. node* root = nullptr;
  87. string str;
  88. while (cin >> str)
  89. {
  90. int j;
  91. if (str == "SEARCH") {
  92. cin >> j;
  93. if (exist(root, j)) cout << "YES" << endl; else cout << "NO" << endl;
  94. }
  95. if (str == "ADD") {
  96. cin >> j;
  97. if (update(root, j)) cout << "DONE" << endl; else cout << "ALREADY" << endl;
  98. }
  99. if (str == "PRINTTREE")
  100. print_tree(root, 0);
  101. }
  102. return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement