diff --git a/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/BottomViewOfBinaryTree.cpp b/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/BottomViewOfBinaryTree.cpp new file mode 100644 index 000000000..f6c59c821 --- /dev/null +++ b/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/BottomViewOfBinaryTree.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; + + +struct Node{ + struct Node *left; + struct Node *right; + int key; + Node(int k){ + key=k; + left=right=NULL; + } +}; + +void bottomView(Node *root){ + mapmp; + queue>q; + q.push({root,0}); + while(!q.empty()){ + auto p=q.front(); + Node *curr=p.first; + int hd=p.second; + mp[hd]=curr->key; + q.pop(); + if(curr->left!=NULL)q.push({curr->left,hd-1}); + if(curr->right!=NULL)q.push({curr->right,hd+1}); + } + for(auto x:mp){ + cout<left=new Node(20); + root->right=new Node(30); + root->left->left=new Node(40); + root->left->right=new Node(50); + + bottomView(root); + return 0; +} \ No newline at end of file diff --git a/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/CeilinginBST.cpp b/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/CeilinginBST.cpp new file mode 100644 index 000000000..02e3a2060 --- /dev/null +++ b/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/CeilinginBST.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +void printCeiling(int arr[], int n){ + cout<<"-1"<<" "; + set s; + s.insert(arr[0]); + for(int i=1;i +using namespace std; + +struct Node +{ + int key; + struct Node *left; + struct Node *right; + Node(int k){ + key=k; + left=right=NULL; + } +}; + +int prevv=INT_MIN; +bool isBST(Node* root) +{ + if (root == NULL) + return true; + + if(isBST(root->left)==false)return false; + + if(root->key<=prevv)return false; + prevv=root->key; + + return isBST(root->right); +} + +int main() { + + Node *root = new Node(4); + root->left = new Node(2); + root->right = new Node(5); + root->left->left = new Node(1); + root->left->right = new Node(3); + + if(isBST(root)) + cout<<"Is BST"; + else + cout<<"Not a BST"; + + return 0; + +} \ No newline at end of file diff --git a/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/FixBSTwithTwoNodesSwapped.cpp b/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/FixBSTwithTwoNodesSwapped.cpp new file mode 100644 index 000000000..90c195e32 --- /dev/null +++ b/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/FixBSTwithTwoNodesSwapped.cpp @@ -0,0 +1,56 @@ +// As it is Inorder Traversal - O(N) +#include +using namespace std; + +struct Node{ + int key; + struct Node *left; + struct Node *right; + Node(int k){ + key=k; + left=right=NULL; + } +}; + + +// Inorder Traversal +void inorder(Node *root){ + if(root!=NULL){ + inorder(root->left); + cout<key<<" "; + inorder(root->right); + } +} + +// Fixing the BST +Node *prevv=NULL,*first=NULL,*second=NULL; +void fixBST(Node *root){ + if(root==NULL)return ; + fixBST(root->left); + if(prevv!=NULL && root->keykey){ + if(first==NULL) first=prevv; + second=root; + + } + prevv=root; + fixBST(root->right); +} + + +int main(){ + Node *root=new Node(18); + root->left=new Node(60); + root->right=new Node(70); + root->left->left=new Node(40); + root->right->left=new Node(8); + root->right->right=new Node(80); + + inorder(root); + cout<key; + first->key=second->key; + second->key=temp; + inorder(root); + return 0; +} \ No newline at end of file diff --git a/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/KthLargest.cpp b/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/KthLargest.cpp new file mode 100644 index 000000000..13ca2c1e1 --- /dev/null +++ b/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/KthLargest.cpp @@ -0,0 +1,63 @@ +#include +using namespace std; + +struct Node +{ + int key; + struct Node *left; + struct Node *right; + int lCount; + Node(int k){ + key=k; + left=right=NULL; + lCount=0; + } +}; + +Node* insert(Node* root, int x) +{ + if (root == NULL) + return new Node(x); + + if (x < root->key) { + root->left = insert(root->left, x); + root->lCount++; + } + + else if (x > root->key) + root->right = insert(root->right, x); + return root; +} + +Node* kthSmallest(Node* root, int k) +{ + if (root == NULL) + return NULL; + + int count = root->lCount + 1; + if (count == k) + return root; + + if (count > k) + return kthSmallest(root->left, k); + + return kthSmallest(root->right, k - count); +} + +int main() { + + Node* root = NULL; + int keys[] = { 20, 8, 22, 4, 12, 10, 14 }; + + for (int x : keys) + root = insert(root, x); + + int k = 4; + Node* res = kthSmallest(root, k); + if (res == NULL) + cout << "There are less than k nodes in the BST"; + else + cout << "K-th Smallest Element is " << res->key; + return 0; + +} \ No newline at end of file diff --git a/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/PairSum.cpp b/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/PairSum.cpp new file mode 100644 index 000000000..3f5f8e686 --- /dev/null +++ b/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/PairSum.cpp @@ -0,0 +1,46 @@ +#include +using namespace std; + +struct Node +{ + int key; + struct Node *left; + struct Node *right; + Node(int k){ + key=k; + left=right=NULL; + } +}; + +bool isPairSum(Node *root, int sum, unordered_set &s) + { + if(root==NULL)return false; + + if(isPairSum(root->left,sum,s)==true) + return true; + + if(s.find(sum-root->key)!=s.end()) + return true; + else + s.insert(root->key); + return isPairSum(root->right,sum,s); + } + +int main() { + + Node *root = new Node(10); + root->left = new Node(8); + root->right = new Node(20); + root->left->left = new Node(4); + root->left->right = new Node(9); + root->right->left = new Node(11); + root->right->right = new Node(30); + root->right->right->left = new Node(25); + + int sum=33; + unordered_set s; + cout< +using namespace std; + + +struct Node{ + struct Node *left; + struct Node *right; + int key; + Node(int k){ + key=k; + left=right=NULL; + } +}; + +void topView(Node *root){ + mapmp; + queue>q; + q.push({root,0}); + while(!q.empty()){ + auto p=q.front(); + Node *curr=p.first; + int hd=p.second; + if(mp.find(hd)==mp.end()) + mp[hd]=curr->key; + q.pop(); + if(curr->left!=NULL)q.push({curr->left,hd-1}); + if(curr->right!=NULL)q.push({curr->right,hd+1}); + } + for(auto x:mp){ + cout<left=new Node(20); + root->right=new Node(30); + root->left->left=new Node(40); + root->left->right=new Node(50); + + topView(root); + return 0; +} \ No newline at end of file diff --git a/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/VerticalSumOfBinaryTree.cpp b/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/VerticalSumOfBinaryTree.cpp new file mode 100644 index 000000000..3c7010f9c --- /dev/null +++ b/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/VerticalSumOfBinaryTree.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; + +struct Node +{ + int key; + struct Node *left; + struct Node *right; + Node(int k){ + key=k; + left=right=NULL; + } +}; + +void vSumR(Node *root,int hd,map &mp){ + if(root==NULL)return; + vSumR(root->left,hd-1,mp); + mp[hd]+=root->key; + vSumR(root->right,hd+1,mp); +} + +void vSum(Node *root){ + map mp; + vSumR(root,0,mp); + for(auto sum: mp) + cout<left = new Node(20); + root->right = new Node(50); + root->left->left = new Node(30); + root->left->right = new Node(40); + + vSum(root); + + return 0; + +} \ No newline at end of file diff --git a/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/VerticalTraversalOfBinaryTree.cpp b/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/VerticalTraversalOfBinaryTree.cpp new file mode 100644 index 000000000..5b7b1a296 --- /dev/null +++ b/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/VerticalTraversalOfBinaryTree.cpp @@ -0,0 +1,45 @@ +#include +using namespace std; + + +struct Node{ + struct Node *left; + struct Node *right; + int key; + Node(int k){ + key=k; + left=right=NULL; + } +}; + +void vTraversal(Node *root){ + map>mp; + queue>q; + q.push({root,0}); + while(!q.empty()){ + auto p=q.front(); + Node *curr=p.first; + int hd=p.second; + mp[hd].push_back(curr->key); + q.pop(); + if(curr->left!=NULL)q.push({curr->left,hd-1}); + if(curr->right!=NULL)q.push({curr->right,hd+1}); + } + for(auto x:mp){ + for(int y:x.second) + cout<left=new Node(20); + root->right=new Node(30); + root->left->left=new Node(40); + root->left->right=new Node(50); + + vTraversal(root); + return 0; +} \ No newline at end of file