Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- vector<int> v;
- class bst
- {
- public:
- int data;
- bst* left;
- bst* right;
- bst(int data)
- {
- this->data = data;
- left = NULL;
- right = NULL;
- }
- };
- bst* insert(bst* root, int item)
- {
- if (root == NULL)
- {
- bst* NODE = new bst(item);
- return NODE;
- }
- if (item < root->data)
- {
- root->left = insert(root->left, item);
- }
- if (item > root->data)
- {
- root->right = insert(root->right, item);
- }
- return root;
- }
- bst* deletekar(bst* root, int item)
- {
- if (root == NULL)
- {
- return NULL;
- }
- if (item < root->data)
- {
- root->left = deletekar(root->left, item);
- }
- if (item > root->data)
- {
- root->right = deletekar(root->right, item);
- }
- if (item == root->data)
- {
- if (root->left == NULL && root->right == NULL)
- {
- delete(root);
- return NULL;
- }
- else if (root->left == NULL)
- {
- bst* k = new bst(root->right->data);
- delete(root);
- return k;
- }
- else if (root->right == NULL)
- {
- bst* k = new bst(root->left->data);
- delete(root);
- return k;
- }
- else
- {
- bst* joy = root->right;
- while (joy->left != NULL)
- {
- joy = joy->left;
- }
- root->data = joy->data; //swapping done
- //delete joy from right;
- root->right = deletekar(root->right, joy->data);
- /////////////////////2nd approach////////////////////////
- bst* succParent = root;
- bst* succ = root->right;
- while (succ->left != NULL)
- {
- succParent = succ;
- succ = succ->left;
- }
- if (succParent != root)
- succParent->left = succ->right;
- else
- succParent->right = succ->right;
- root->data = succ->data;
- delete succ;
- }
- }
- return root;
- }
- void printPostorder(bst* node)
- {
- if (node == NULL)
- return;
- printPostorder(node->left);
- printPostorder(node->right);
- cout << node->data << " ";
- }
- void printInorder(bst* node)
- {
- if (node == NULL)
- return;
- printInorder(node->left);
- cout << node->data << " ";
- printInorder(node->right);
- }
- void printPreorder(bst* node)
- {
- if (node == NULL)
- return;
- cout << node->data << " ";
- printPreorder(node->left);
- printPreorder(node->right);
- }
- bool search(bst* root, int item)
- {
- if (root == NULL)
- {
- return false;
- }
- if (root->data == item)
- {
- return true;
- }
- if (item < root->data)
- {
- return search(root->left, item);
- }
- return search(root->right, item);
- }
- int findmin(bst* root)
- {
- while (root->left != NULL)
- {
- root = (root->left);
- }
- return root->data;
- }
- int findmax(bst* root)
- {
- while (root->right != NULL)
- {
- root = (root->right);
- }
- return root->data;
- }
- void levelorder(bst* root)
- {
- if (root == NULL) return;
- queue<bst*> q;
- q.push(root);
- while (q.size() != 0)
- {
- int count = q.size();
- while (count > 0)
- {
- bst* node = q.front();
- cout << node->data << " ";
- q.pop();
- if (node->left != NULL) q.push(node->left);
- if (node->right != NULL) q.push(node->right);
- count--;
- }
- cout << endl;
- }
- }
- int main()
- {
- bst* root = NULL;
- // cout << sizeof(NULL) << endl;
- root = insert(root, 6);
- root = insert(root, 3);
- root = insert(root, 5);
- root = insert(root, 2);
- root = insert(root, 8);
- root = insert(root, 7);
- root = insert(root, 9);
- // printPostorder(root);
- // cout << endl;
- // printPreorder(root);
- // cout << endl;
- levelorder(root);
- // for (int i = 0; i < v.size(); i++)
- // {
- // cout << v[i] << endl;
- // }
- // cout << endl;
- // if (search(root, 6)) cout << "Found" << endl;
- // else cout << "Not Found" << endl;
- // cout << "findmin " << findmin(root) << " " << endl;
- // cout << "findmax " << findmax(root) << " " << endl;
- return 0;
- }
- //////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////
- ///////////leetcode level order traversal level wise//////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- // class Solution
- // {
- // public:
- // vector<vector<int>> levelOrder(TreeNode* root)
- // {
- // vector<vector<int>> v;
- // if (root == NULL) return v;
- // queue<TreeNode*> q;
- // q.push(root);
- // int i=0;
- // while (q.size()!=0)
- // {
- // int nodeCount = q.size();
- // vector<int> temp;
- // while (nodeCount > 0)
- // {
- // TreeNode* node = q.front();
- // q.pop();
- // temp.push_back(node->val);
- // if (node->left != NULL)
- // q.push(node->left);
- // if (node->right != NULL)
- // q.push(node->right);
- // nodeCount--;
- // }
- // v.push_back(temp);
- // temp.clear();
- // }
- // return v;
- // }
- // };
- ////////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////
- // #include<bits/stdc++.h>
- // using namespace std;
- // class bst {
- // public:
- // int data;
- // bst* left;
- // bst* right;
- // bst(int data)
- // {
- // this->data = data;
- // right = NULL;
- // left = NULL;
- // }
- // };
- // bst* insert(bst* root, int item)
- // {
- // if (root == NULL)
- // {
- // bst* node = new bst(item);
- // return node;
- // }
- // if (item < root->data)
- // {
- // root->left = insert(root->left, item);
- // }
- // if (item > root->data)
- // {
- // root->right = insert(root->right, item);
- // }
- // return root;
- // }
- // bool search(bst* root, int item)
- // {
- // if (root == NULL)
- // {
- // return false;
- // }
- // if (root->data == item)
- // {
- // return true;
- // }
- // if (item < root->data)
- // {
- // return search(root->left, item);
- // }
- // return search(root->right, item);
- // }
- // void inorder(bst* root)
- // {
- // if (root == NULL) return;
- // inorder(root->left);
- // cout << root->data << " ";
- // inorder(root->right);
- // }
- // bst* deletekar(bst* root, int item)
- // {
- // if (root == NULL)
- // {
- // return NULL;
- // }
- // if (item < root->data)
- // {
- // root->left = deletekar(root->left, item);
- // }
- // if (item > root->data)
- // {
- // root->right = deletekar(root->right, item);
- // }
- // if (item == root->data)
- // {
- // if (root->left == NULL && root->right == NULL)
- // {
- // delete(root);
- // return NULL;
- // }
- // else if (root->left == NULL)
- // {
- // bst* k = new bst(root->right->data);
- // delete(root);
- // return k;
- // }
- // else if (root->right == NULL)
- // {
- // bst* k = new bst(root->left->data);
- // delete(root);
- // return k;
- // }
- // else
- // {
- // bst* joy = root->right;
- // while (joy->left != NULL)
- // {
- // joy = joy->left;
- // }
- // root->data = joy->data; //swapping done
- // //delete joy from right;
- // root->right = deletekar(root->right, joy->data);
- // }
- // }
- // return root;
- // }
- // int main()
- // {
- // bst* root = NULL;
- // root = insert(root, 18);
- // root = insert(root, 88);
- // root = insert(root, 83);
- // root = insert(root, 34);
- // root = insert(root, 6);
- // root = insert(root, 8);
- // root = insert(root, 21);
- // root = insert(root, 13);
- // root = insert(root, 19);
- // inorder(root);
- // if (search(root, 9)) cout << "found" << endl;
- // else cout << "not found" << endl;
- // root = deletekar(root, 18);
- // inorder(root);
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement