Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <map>
- using namespace std;
- struct Node {
- int data;
- Node* left = nullptr;
- Node* right = nullptr;
- Node(int value) {
- data = value;
- }
- ~Node() {
- delete left;
- delete right;
- }
- };
- class BST {
- private:
- Node* root;
- bool _exists(int value, Node* current) const {
- if (current) {
- if (current->data == value) {
- return true;
- } else if (value > current->data) {
- return _exists(value, current->right);
- } else {
- return _exists(value, current->left);
- }
- } else {
- return false;
- }
- }
- Node* _insert(int value, Node* current) {
- if (!current) {
- return new Node(value);
- }
- if (value < current->data) {
- current->left = _insert(value, current->left);
- } else if (value > current->data) {
- current->right = _insert(value, current->right);
- }
- return current;
- }
- void _print(Node* current) const {
- if (current) {
- _print(current->left);
- cout << current->data << " ";
- _print(current->right);
- }
- }
- Node* _remove(int value, Node* current) {
- if (!current) {
- return nullptr;
- }
- if (value < current->data) {
- current->left = _remove(value, current->left);
- } else if (value > current->data) {
- current->right = _remove(value, current->right);
- } else {
- if (!current->left && !current->right) {
- free(current);
- return nullptr;
- } else if (!current->left) {
- Node* tempRight = current->right;
- free(current);
- return tempRight;
- } else if (!current->right) {
- Node* tempLeft = current->left;
- free(current);
- return tempLeft;
- } else {
- Node* swapWith = current->right;
- while (swapWith->left) {
- swapWith = swapWith->left;
- }
- current->data = swapWith->data;
- current->right = _remove(swapWith->data, current->right);
- }
- }
- return current;
- }
- void _printTop(Node* root, int distance, int level, std::map<int, std::pair<int, int>>& m) {
- if (root == nullptr) {
- return;
- }
- if (m.find(distance) == m.end() ||
- level < m[distance].second) {
- m[distance] = { root->data, level };
- }
- _printTop(root->left, distance - 1, level + 1, m);
- _printTop(root->right, distance + 1, level + 1, m);
- }
- public:
- BST() {
- root = nullptr;
- }
- ~BST() {
- delete root;
- }
- bool exists(int value) const {
- return _exists(value, root);
- }
- void insert(int value) {
- root = _insert(value, root);
- }
- void remove(int value) {
- root = _remove(value, root);
- }
- void print() const {
- _print(root);
- }
- void levelorder() const {
- queue<Node*> que;
- que.push(root);
- while (!que.empty()) {
- Node* current = que.front();
- que.pop();
- if (current) {
- cout << current->data << " ";
- if (current->left) {
- que.push(current->left);
- }
- if (current->right) {
- que.push(current->right);
- }
- }
- }
- }
- void printTop() {
- //key - the horizontal distance to the node
- //value - std::pair<node->data, level>
- std::map<int, std::pair<int, int>> m;
- _printTop(root, 0, 0, m);
- for (auto it : m) {
- cout << it.second.first << " ";
- }
- }
- };
- int main() {
- BST s;
- s.insert(3);
- s.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement