Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- #include<queue>
- #include<string>
- #include<iomanip>
- using namespace std;
- struct Node {
- int data;
- Node* left;
- Node* right;
- Node(int data) {
- this->data = data;
- left = nullptr;
- right = nullptr;
- }
- };
- class BST {
- Node* root = nullptr;
- Node* _add(Node* tmp, int data) {
- if (tmp == nullptr)
- return new Node(data);
- if (tmp->data > data)
- tmp->left = _add(tmp->left, data);
- if (tmp->data < data)
- tmp->right = _add(tmp->right, data);
- return tmp;
- }
- Node* _remove(Node* tmp, int data) {
- if (tmp == nullptr)
- return nullptr;
- if (tmp->data > data)
- tmp->left = _remove(tmp->left, data);
- else if (tmp->data < data)
- tmp->right = _remove(tmp->right, data);
- else
- {
- if (!tmp->left && !tmp->right) {
- //leaf
- delete tmp;
- return nullptr;
- }
- else if (!tmp->left) {
- Node* del = tmp;
- tmp = tmp->right;
- delete del;
- del = nullptr;
- return tmp;
- }
- else if (!tmp->right) {
- Node*del = tmp;
- tmp = tmp->left;
- delete del;
- del = nullptr;
- return tmp;
- }
- else {
- //has both nodes;
- //take minimal from right and swap with current
- Node* minimal = tmp->right;
- while (minimal->left != nullptr)
- minimal = minimal->left;
- std::swap(tmp->data, minimal->data);
- tmp->right = _remove(tmp->right, minimal->data);
- }
- }
- return tmp;
- }
- void _print(Node* tmp) {
- if (tmp == nullptr)
- return;
- std::cout << tmp->data << " ";
- _print(tmp->left);
- _print(tmp->right);
- }
- void _print_odd_layers(Node* tmp) {
- if (tmp == nullptr)
- return;
- int level = 1;
- std::queue< std::pair<Node*, int> > next_to_process;
- next_to_process.push({ tmp,level });
- while (!next_to_process.empty()) {
- Node* next = next_to_process.front().first;
- int current_level = next_to_process.front().second;
- next_to_process.pop();
- if (next)
- if (current_level % 2 != 0)
- std::cout << next->data << " ";
- if (next->left)
- next_to_process.push({ next->left,current_level + 1 });
- if (next->right)
- next_to_process.push({ next->right,current_level + 1 });
- }
- }
- Node* _mirror_tree(Node* tmp)
- {
- if (tmp == nullptr)
- return nullptr;
- _mirror_tree(tmp->left);
- _mirror_tree(tmp->right);
- Node* right = tmp->right;
- tmp->right = tmp->left;
- tmp->left = right;
- return tmp;
- }
- public:
- BST() {
- }
- void add(int data) {
- this->root = _add(this->root, data);
- }
- void print() {
- _print(this->root);
- }
- void remove(int data) {
- this->root = _remove(this->root, data);
- }
- void print_odd_layers() {
- _print_odd_layers(this->root);
- }
- void mirror_tree()
- {
- this->root = _mirror_tree(this->root);
- }
- };
- int main() {
- /* Enter your code here. Read input from STDIN. Print output to STDOUT */
- BST* tree = new BST();
- int operations;
- std::cin >> operations;
- for (int i = 0; i < operations; i++) {
- std::string operation;
- std::cin >> operation;
- if (operation == "add") {
- int number;
- std::cin >> number;
- tree->add(number);
- }
- }
- tree->mirror_tree();
- tree->print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement