-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalgoexp.cpp
273 lines (239 loc) · 5.95 KB
/
algoexp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include<bits/stdc++.h>
using namespace std;
class BST{
public:
int data;
BST *left,*right;
BST();
BST(int);
BST* Insert(BST*,int);
BST* deleteNode(BST*,int);
BST* minValueNode(BST*);
BST* preorderTraversal(BST*);
BST* inorderTraversal(BST*);
BST* postorderTraversal(BST*);
};
BST::BST():data(0),left(NULL),right() {}
BST::BST(int val) {
data=val;
left=right=NULL;
}
BST * BST::Insert(BST *root, int val) {
if(!root){
return new BST(val);
}
if(val<root->data){
root->left=Insert(root->left,val);
}else{
root->right=Insert(root->right,val);
}
return root;
}
BST * BST::deleteNode(BST *root, int val) {
if(root==NULL) return NULL;
if(val<root->data){
root->left=deleteNode(root->left,val);
}else if(val>root->data){
root->right=deleteNode(root->right,val);
}else{
if(root->left==NULL){
BST *temp=root->right;
free(root);
return temp;
}else if(root->right==NULL){
BST *temp=root->left;
free(root);
return temp;
}
BST *temp=minValueNode(root->right);
root->data=temp->data;
root->right=deleteNode(root->right,temp->data);
}
return root;
}
BST * BST::minValueNode(BST *root) {
BST *min=root;
while(min&&min->left!=NULL){
min=min->left;
}
return min;
}
BST * BST::preorderTraversal(BST *root) {
if(root==NULL)
return NULL;
preorderTraversal(root->left);
preorderTraversal(root->right);
cout<<root->data<<" ";
}
BST * BST::inorderTraversal(BST *root) {
if(root==NULL)
return NULL;
inorderTraversal(root->left);
cout<<root->data<<" ";
inorderTraversal(root->right);
}
class BTree{
public:
int data;
BTree*left,*right;
};
BTree *newNode(int data) {
BTree* root=new BTree();
root->data=data;
root->right=NULL;
root->left=NULL;
return root;
}
class Graph{
int V;//No. of Vertices
list<int> *adj;
void DFSUtil(int v,vector<bool>&visited);
public:
Graph(int V);
void addEdge(int v,int w);
void DFS(int v);
};
Graph::Graph(int V) {
this->V=V;
adj=new list<int>[V];
}
void Graph::addEdge(int v, int w) {
adj[v].push_back(w);
}
void Graph::DFS(int v) {
/*
* time-O(V+E)
* space-O(V)*/
vector<bool> visited(V, false);
DFSUtil(v,visited);
}
void Graph::DFSUtil(int v, vector<bool> &visited) {
visited[v]=true;
cout<<v<<" ";
for(auto i=adj[v].begin();i!=adj[v].end();i++){
if(!visited[*i]){
DFSUtil(*i,visited);
}
}
}
class Node{
public:
int data;
Node* next,*prev;
Node(int data);
Node* deleteNode(int data);
Node* setHeadFromList(Node* head,int val);
Node* setTailFromList(Node* tail,int val);
Node* setHead(Node* head,int val);
Node* setTail(Node* tail,int val);
bool searchList(Node *,int val);
Node* deleteNodeWithVal(int data);
Node* InsertBefore(Node*,int,int);
Node* InsertAfter(Node*,int,int);
};
Node::Node(int data) {
Node();
}
void TwoSumNumber1(vector<int> arr, int s);
void TwoSumNumber2(vector<int> arr, int s);
void ClosestValueInBST(BST*,int);
vector<int> BranchSums(BTree*);
void calculateBranchSum(BTree*,int,vector<int>&);
int main(){
Node ll=new Node(1);
return 0;
}
void TwoSumNumber1(vector<int> arr, int s) {
/*
* Q- Given an array of unique elements and a sum 's',find 2 numbers in the array that sum to 's'
* @- Use hash-map to add traversed elements into it and check if s-n is in the hash-map
* Time Complexity- O(n)
* Space Complexity- O(n)
* */
unordered_map<int,bool> a;
bool flag=false;
for(int i=0;i<arr.size();i++){
int num=s-arr[i];
if(a.find(num)==a.end()){
a[arr[i]]=true;
}else{
cout<<a.find(num)->first<<" "<<arr[i]<<endl;
flag=true;
}
}
if(flag==false){
cout<<"No Pair of sums"<<endl;
}
}
void TwoSumNumber2(vector<int> arr, int s) {
/*
* Q- Given an array of unique elements and a sum 's',find 2 numbers in the array that sum to 's'
* @- Sort the array using an optimal sorting algo and use 2 ptrs pointing to 1st and last elmt and move left ptr until LeftPtr+RightPtr=val
* Time Complexity- O(nlog(n))
* Space Complexity- O(1)
* */
sort(arr.begin(),arr.end());
arr.begin();
int n=arr.size()-1;
bool f= false;
for(int i=0;i<=n;i++){
if(arr[i]+arr[n]>s){
n--;
}else{
if(arr[i]+arr[n]==s){
cout<<arr[i]<<" "<<arr[n]<<endl;
f=true;
}
}
}
if(!f){
cout<<"No pairs";
}
}
/*
* Time-O(log(n))
* Space- depth of tree
*/
int findClosestValueinBST(BST*root,int target,int closest){
if(root==NULL){
return closest;
}
if(abs(target-closest)>abs(target-root->data)){
closest=root->data;
}
if(target<root->data){
return findClosestValueinBST(root->left,target,closest);
}else if(target>root->data){
findClosestValueinBST(root->right,target,closest);
}else{
return closest;
}
}
void ClosestValueInBST(BST*root,int n){
int closest=root->data;
BST* tempRoot=root;
int diff=abs(closest-tempRoot->data);
int ans=findClosestValueinBST(tempRoot,n,closest);
cout<<ans<<endl;
}
vector<int> BranchSums(BTree* root){
/*
* O(n)-time @ O(n)-space*/
int runningSum=0;
BTree*troot=root;
vector<int>list;
calculateBranchSum(troot,runningSum,list);
return list;
}
void calculateBranchSum(BTree*root,int runningSum,vector<int>&list){
if(root==NULL){
return;
}
int newRunningSum=runningSum+root->data;
if(root->right==NULL&&root->left==NULL){
list.push_back(newRunningSum);
return;
}
calculateBranchSum(root->left,newRunningSum,list);
calculateBranchSum(root->right,newRunningSum,list);
}