Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <tuple>
- #include <algorithm>
- #include <utility>
- using namespace std;
- #define ll long long int
- const ll INF = 9999999999999;
- struct node {
- int left, right, min, end = 0;
- node* child_left = nullptr, * child_right = nullptr;
- };
- void print_tree(node* p, int level)
- {
- if (p == nullptr)
- return;
- print_tree(p->child_left, level + 1);
- for (int i = 0; i < level; i++)
- cout << ".";
- cout << p->end << endl;
- print_tree(p->child_right, level + 1);
- }
- bool update(node* &a, int l)
- {
- if (a == nullptr)
- {
- a = new node;
- a->end = l;
- return true;
- }
- if (l == a->end)
- return false;
- else
- {
- if (l < a->end) {
- if (a->child_left != nullptr)
- return update(a->child_left, l);
- else
- {
- a->child_left = new node;
- a->child_left->end = l;
- return true;
- }
- }
- else {
- if (a->child_right != nullptr)
- return update(a->child_right, l);
- else {
- a->child_right = new node;
- a->child_right->end = l;
- return true;
- }
- }
- }
- return false;
- }
- bool exist(node* root, int l) {
- if (root == nullptr)
- return false;
- if (root->end == l)
- return true;
- if (l < root->end)
- return exist(root->child_left, l);
- else
- return exist(root->child_right, l);
- }
- int main()
- {
- node* root = nullptr;
- string str;
- while (cin >> str)
- {
- int j;
- if (str == "SEARCH") {
- cin >> j;
- if (exist(root, j)) cout << "YES" << endl; else cout << "NO" << endl;
- }
- if (str == "ADD") {
- cin >> j;
- if (update(root, j)) cout << "DONE" << endl; else cout << "ALREADY" << endl;
- }
- if (str == "PRINTTREE")
- print_tree(root, 0);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement