Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // AVLTree
- //
- // resending
- // Created by kshakh on 13.12.2021.
- //
- #include <algorithm>
- #include <cassert>
- #include <climits>
- #include <iostream>
- #include <vector>
- #include <cstring>
- using std::cin;
- using std::cout;
- using std::vector;
- using std::string;
- using std::max;
- using std::min;
- struct Node {
- int key;
- Node* left;
- Node* right;
- int height;
- Node() : left(nullptr), right(nullptr) {}
- Node(int val) : key(val), left(nullptr), right(nullptr), height(1) {}
- };
- class AVLTree {
- public:
- AVLTree() : root(nullptr) {}
- ~AVLTree() {
- deleteRoots(root);
- }
- void Add(int);
- void Delete(int);
- bool Exists(int);
- string Next(int);
- string Prev(int);
- private:
- Node* root;
- Node* insert(Node*, int);
- Node* erase(Node*, int);
- bool exists(Node*, int);
- string next(Node*, int);
- string prev(Node*, int);
- void deleteRoots(Node* p) {
- if (p == nullptr) {
- return;
- }
- deleteRoots(p->left);
- deleteRoots(p->right);
- delete p;
- }
- int Height(Node* p) {
- return (p != nullptr) ? p->height : 0;
- }
- int Diff(Node* p) {
- return Height(p->right) - Height(p->left);
- }
- void Update(Node* p) {
- p->height = max(Height(p->left), Height(p->right)) + 1;
- }
- Node* RightRotate(Node* p) {
- Node* q = p->left;
- p->left = q->right;
- q->right = p;
- Update(p);
- Update(q);
- return q;
- }
- Node* LeftRotate(Node* p) {
- Node* q = p->right;
- p->right = q->left;
- q->left = p;
- Update(p);
- Update(q);
- return q;
- }
- Node* Balance(Node* p) {
- Update(p);
- if (Diff(p) == -2) {
- if (Diff(p->left) > 0) {
- p->left = LeftRotate(p->left);
- }
- return RightRotate(p);
- } else if (Diff(p) == 2) {
- if (Diff(p->right) < 0) {
- p->right = RightRotate(p->right);
- }
- return LeftRotate(p);
- }
- return p;
- }
- Node* findMin(Node* p) {
- if (p->left == nullptr) {
- return p;
- } else {
- return findMin(p->left);
- }
- }
- Node* eraseMin(Node* p) {
- if (p->left == nullptr) {
- return p->right;
- }
- p->left = eraseMin(p->left);
- return Balance(p);
- }
- };
- void AVLTree::Add(int val) {
- root = insert(root, val);
- }
- Node* AVLTree::insert(Node* p, int val) {
- if (p == nullptr || Height(p) == 0) {
- return new Node(val);
- }
- if (val < p->key) {
- p->left = insert(p->left, val);
- } else if (val == p->key) {
- return Balance(p);
- } else {
- p->right = insert(p->right, val);
- }
- return Balance(p);
- }
- void AVLTree::Delete(int val) {
- root = erase(root, val);
- }
- Node* AVLTree::erase(Node *p, int val) {
- if (!exists(p, val) || p == nullptr) return p;
- if (val < p->key) {
- p->left = erase(p->left, val);
- } else if (val > p->key) {
- p->right = erase(p->right, val);
- } else {
- if (p->right == nullptr)
- return p->left;
- Node* sml = findMin(p->right);
- sml->right = eraseMin(p->right);
- sml->left = p->left;
- return Balance(sml);
- }
- return Balance(p);
- }
- bool AVLTree::Exists(int val) {
- return exists(root, val);
- }
- bool AVLTree::exists(Node *p, int val) {
- if (p == nullptr) {
- return false;
- }
- if (p->key == val) {
- return true;
- }
- if (p->key > val) {
- return exists(p->left, val);
- } else {
- return exists(p->right, val);
- }
- }
- string AVLTree::Next(int val) {
- return next(root, val);
- }
- string AVLTree::next(Node* p, int x) {
- if (p == nullptr) return "none";
- Node* curr = p;
- Node* nex = nullptr;
- while (curr != nullptr) {
- if (curr->key > x) {
- nex = curr;
- curr = curr->left;
- } else
- curr = curr->right;
- }
- if (nex == nullptr) return "none";
- return std::to_string(nex->key);
- }
- string AVLTree::Prev(int val) {
- return prev(root, val);
- }
- string AVLTree::prev(Node* p, int x) {
- if (p == nullptr) return "none";
- Node* curr = p;
- Node* prev = nullptr;
- while (curr != nullptr) {
- if (curr->key < x) {
- prev = curr;
- curr = curr->right;
- } else
- curr = curr->left;
- }
- if (prev == nullptr) return "none";
- return std::to_string(prev->key);
- }
- int main() {
- AVLTree tree;
- string String;
- int numb;
- while (cin >> String >> numb) {
- if (String[0] == 'i') {
- tree.Add(numb);
- } else if (String[0] == 'd') {
- tree.Delete(numb);
- } else if (String[0] == 'e') {
- bool fl = tree.Exists(numb);
- if (fl == true) {
- std::cout << "true\n";
- } else {
- std::cout << "false\n";
- }
- } else if (String[0] == 'n') {
- cout << tree.Next(numb) << "\n";
- } else if (String[0] == 'p') {
- cout << tree.Prev(numb) << "\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement