-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99ae022
commit a141ddd
Showing
9 changed files
with
407 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/BottomViewOfBinaryTree.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include<bits/stdc++.h> | ||
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){ | ||
map<int,int>mp; | ||
queue<pair<Node *,int>>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<<x.second<<" "; | ||
} | ||
|
||
} | ||
|
||
|
||
int main(){ | ||
Node *root=new Node(10); | ||
root->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; | ||
} |
23 changes: 23 additions & 0 deletions
23
1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/CeilinginBST.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
void printCeiling(int arr[], int n){ | ||
cout<<"-1"<<" "; | ||
set<int> s; | ||
s.insert(arr[0]); | ||
for(int i=1;i<n;i++){ | ||
if(s.lower_bound(arr[i])!=s.end()) | ||
cout<<*(s.lower_bound(arr[i]))<<" "; | ||
else | ||
cout<<"-1"<<" "; | ||
s.insert(arr[i]); | ||
} | ||
} | ||
|
||
int main() { | ||
|
||
int arr[]={2,8,30,15,25,12}; | ||
int n=sizeof(arr)/sizeof(arr[0]); | ||
|
||
printCeiling(arr,n); | ||
} |
44 changes: 44 additions & 0 deletions
44
1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/CheckForBST.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <bits/stdc++.h> | ||
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; | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/FixBSTwithTwoNodesSwapped.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// As it is Inorder Traversal - O(N) | ||
#include<bits/stdc++.h> | ||
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<<root->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->key<prevv->key){ | ||
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<<endl; | ||
fixBST(root); | ||
int temp=first->key; | ||
first->key=second->key; | ||
second->key=temp; | ||
inorder(root); | ||
return 0; | ||
} |
63 changes: 63 additions & 0 deletions
63
1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/KthLargest.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <bits/stdc++.h> | ||
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; | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/PairSum.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include <bits/stdc++.h> | ||
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<int> &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<int> s; | ||
cout<<isPairSum(root,sum,s); | ||
|
||
return 0; | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/TopViewOfBinaryTree.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include<bits/stdc++.h> | ||
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){ | ||
map<int,int>mp; | ||
queue<pair<Node *,int>>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<<x.second<<" "; | ||
} | ||
|
||
} | ||
|
||
|
||
int main(){ | ||
Node *root=new Node(10); | ||
root->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; | ||
} |
41 changes: 41 additions & 0 deletions
41
1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/VerticalSumOfBinaryTree.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <bits/stdc++.h> | ||
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<int,int> &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<int,int> mp; | ||
vSumR(root,0,mp); | ||
for(auto sum: mp) | ||
cout<<sum.second<<" "; | ||
} | ||
|
||
int main() { | ||
|
||
Node *root = new Node(10); | ||
root->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; | ||
|
||
} |
Oops, something went wrong.