Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- class Node
- {
- public:
- int data;
- Node *left;
- Node *right;
- Node *parent;
- Node(int val, Node* par)
- {
- data = val;
- left = right = NULL;
- parent = par;
- }
- };
- Node* insert(Node *root, int value, Node *parent)
- {
- if (root == NULL)
- return new Node(value, parent);
- if (value < root->data)
- {
- root->left = insert(root->left, value, root);
- }
- else
- {
- root->right = insert(root->right, value, root);
- }
- return root;
- }
- Node* buildBST(vector<int> arr)
- {
- Node *root = NULL;
- for (int x : arr)
- {
- root = insert(root, x, nullptr);
- }
- return root;
- }
- void print(Node *root)
- {
- if (root == NULL)
- return;
- print(root->left);
- cout << root->data << " ";
- print(root->right);
- }
- void printStk(Node *root2)
- {
- stack<Node*> s;
- Node *root = root2;
- while (root != nullptr || !s.empty())
- {
- while (root != nullptr)
- {
- s.push(root);
- root = root->left;
- }
- root = s.top();
- s.pop();
- cout << root->data << " ";
- root = root->right;
- }
- }
- void printPost(Node *root)
- {
- if (root == nullptr)
- return;
- printPost(root->left);
- printPost(root->right);
- cout << root->data << " ";
- }
- void printPre(Node *root)
- {
- if (root == nullptr)
- return;
- cout << root->data << " ";
- printPre(root->left);
- printPre(root->right);
- }
- Node* search(Node*& root, int val)
- {
- if (root == nullptr)
- return nullptr;
- if (val < root->data)
- return search(root->left, val);
- else if(val > root->data)
- return search(root->right, val);
- else if(val == root->data)
- return root;
- }
- Node* searchItr(Node* root, int val) {
- while (root != nullptr)
- {
- if(val == root->data) {
- return root;
- }
- else if(val < root->data) root = root->left;
- else if(val > root->data) root = root->right;
- }
- return root;
- }
- Node* findMini(Node*& root) {
- if(root->left == nullptr) return root;
- return findMini(root->left);
- }
- Node* findMaxi(Node* root) {
- if(root->right == nullptr) return root;
- return findMaxi(root->right);
- }
- void printPreStack(Node* root) {
- stack<Node*>s;
- s.push(nullptr);
- Node* ptr = root;
- while(ptr != nullptr) {
- cout << ptr->data << " ";
- if(ptr->right != nullptr) s.push(ptr->right);
- if(ptr->left != nullptr) ptr = ptr->left;
- else {
- // cout << s.top()->data << " ";
- ptr = s.top();
- s.pop();
- }
- }
- }
- Node* successor(Node*& nodeTree) {
- if(nodeTree->right != nullptr) {
- return findMini(nodeTree->right);
- }
- else{
- Node* y = nodeTree->parent;
- while (y!=nullptr && nodeTree == y->right)
- {
- nodeTree = y;
- y = y->parent;
- }
- return y;
- }
- }
- Node* predecessor(Node* nodeTree) {
- if(nodeTree->left != nullptr) {
- return findMaxi(nodeTree->left);
- }
- else{
- Node* y = nodeTree->parent;
- while (y!=nullptr && nodeTree == y->left)
- {
- nodeTree = y;
- y = y->parent;
- }
- return y;
- }
- }
- void del(Node*& root, int key) {
- Node* treeNode = search(root, key);
- if(treeNode == nullptr) {
- return;
- }
- if(treeNode->left == nullptr && treeNode->right == nullptr) {
- if(treeNode == treeNode->parent->left) treeNode->parent->left = nullptr;
- else treeNode->parent->right = nullptr;
- delete treeNode;
- return;
- }
- else if(treeNode->left != nullptr && treeNode->right == nullptr) {
- if(treeNode->parent->left == treeNode) treeNode->parent->left = treeNode->left;
- else treeNode->parent->right = treeNode->left;
- // treeNode->left = nullptr;
- return;
- }
- else if(treeNode->left == nullptr && treeNode->right != nullptr) {
- if(treeNode->parent->left == treeNode) treeNode->parent->left = treeNode->right;
- else treeNode->parent->right = treeNode->right;
- // treeNode->right = nullptr;
- return;
- }
- else{
- Node* succ = successor(treeNode);
- // treeNode->data = succ->data;
- swap(treeNode->data,succ->data);
- del(succ, succ->data);
- }
- }
- int main()
- {
- vector<int> arr = {5, 4, 6, 2, 3, 7, 8};
- Node *root = buildBST(arr);
- // cout << "Printed normally: ";
- // print(root);
- // cout << endl;
- // cout << "Printed in stack: ";
- // printStk(root);
- // cout << endl;
- // cout << "Printed post order: ";
- // printPost(root);
- // cout << endl;
- // cout << "Printed pre order: ";
- // printPre(root);
- // cout << endl;
- // cout << "Printed pre order with stack: ";
- // printPreStack(root);
- // cout << endl;
- // Node* succ = successor(search(root, 2));
- // cout << "Successor of 2 is: " << succ->data;
- // cout << endl;
- // Node* pre = predecessor(search(root, 5));
- // cout << "Predecessor of 5 is: " << pre->data;
- // cout << endl;
- // Node* n1 = search(root, 8);
- // Node* n2 = search(root, 3);
- print(root);
- cout << "\n";
- del(root, 2);
- print(root);
- cout << "\n";
- // cout << endl;
- // del(root, 3);
- // print(root);
- // cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment