Skip to content

Commit e668bd1

Browse files
New code for google
1 parent b0f0bdf commit e668bd1

File tree

11 files changed

+333
-111
lines changed

11 files changed

+333
-111
lines changed

Algorithms/Sorting/mergeSort.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Divide and Conqure
2+
3+
// Time Complexity - O(n log(n))
4+
// Space Complexity - O(n)
5+
6+
7+
const merge = (arr1, arr2) => {
8+
9+
};
10+
11+
console.log(merge([2, 5, 3, 0, 57], [9, 12, 13]));

Algorithms/Sorting/quickSort.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Divide and conquer
2+
3+
// Time Complexity - O(n * 2)
4+
// Space Complexity - O(log(n))

Datastructure/BinaryTree/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,13 @@ class BinarySearchTree {
6464
return currentNode.value;
6565
}
6666

67-
hightOfBinaryTree() {
68-
// Rules b = (n-1)/2
69-
let totalNodes = this.count;
70-
let height = Math.floor((totalNodes - 1) / 2);
71-
return height;
72-
}
67+
// hightOfBinaryTree() {
68+
// // Rules b = (n-1)/2
69+
// let totalNodes = this.count;
70+
// let height = Math.floor((totalNodes - 1) / 2);
71+
// return height;
72+
// }
73+
7374
lookup(value) {
7475
let currentNode = this.root;
7576
while(currentNode) {

Datastructure/BinaryTree/practice.js

Lines changed: 101 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class BinarySearchTree {
3636
this.root = new Node(value);
3737
this.count = 1;
3838
}
39+
3940
insert(value) {
40-
this.count++;
4141
let newNode = new Node(value);
4242
let traverse = node => {
4343
if(value < node.value) {
@@ -56,52 +56,59 @@ class BinarySearchTree {
5656
}
5757
traverse(this.root);
5858
}
59+
5960
size() {
60-
return this.size;
61+
return this.count;
6162
}
62-
max() {
63+
64+
min() {
6365
let currentNode = this.root;
6466
while(currentNode.left) {
6567
currentNode = currentNode.left;
6668
}
6769
return currentNode.value;
6870
}
69-
min() {
71+
72+
max() {
7073
let currentNode = this.root;
7174
while(currentNode.right) {
7275
currentNode = currentNode.right;
7376
}
7477
return currentNode.value;
7578
}
79+
7680
lookup(value) {
7781
let currentNode = this.root;
7882
while(currentNode) {
7983
if(value === currentNode.value) {
8084
return true;
81-
} if(value < currentNode.value) {
82-
currentNode = currentNode.left;
85+
} else if(value < currentNode.value) {
86+
currentNode = currentNode.left
8387
} else {
8488
currentNode = currentNode.right;
8589
}
8690
}
8791
return false;
8892
}
93+
8994
dfsInorder() {
9095
// left, middle, right
9196
// 10
9297
// / \
9398
// 5 19
9499
// / \ / \
95-
// 1 6 17 21
96-
97-
// Output = 1, 5, 6, 10, 19, 21
100+
// 3 6 17 21
101+
// / \
102+
// 2 4
103+
104+
// Output = 1,5,6,10,17,19,21
98105
let result = [];
99-
let traverse = node => {
100-
if(node.left) traverse(node.left);
106+
let searchTree = node => {
107+
if(node.left) searchTree(node.left);
101108
result.push(node.value);
102-
if(node.right) traverse(node.right);
109+
if(node.right) searchTree(node.right)
103110
}
104-
traverse(this.root);
111+
searchTree(this.root);
105112
return result;
106113
}
107114
dfsPreorder() {
@@ -112,14 +119,14 @@ class BinarySearchTree {
112119
// / \ / \
113120
// 1 6 17 21
114121

115-
// Output = 10, 5, 1, 6, 19, 17, 21
122+
// Output = 10,5,1,6,19,17,21
116123
let result = [];
117-
let traverse = node => {
124+
let searchTree = node => {
118125
result.push(node.value);
119-
if(node.left) traverse(node.left);
120-
if(node.right) traverse(node.right);
126+
if(node.left) searchTree(node.left);
127+
if(node.right) searchTree(node.right)
121128
}
122-
traverse(this.root);
129+
searchTree(this.root);
123130
return result;
124131
}
125132
dfsPostorder() {
@@ -132,12 +139,12 @@ class BinarySearchTree {
132139

133140
// Output = 1,6,5,17,21,19,10
134141
let result = [];
135-
let traverse = node => {
136-
if(node.left) traverse(node.left);
137-
if(node.right) traverse(node.right);
142+
let searchTree = node => {
143+
if(node.left) searchTree(node.left);
144+
if(node.right) searchTree(node.right);
138145
result.push(node.value);
139146
}
140-
traverse(this.root);
147+
searchTree(this.root);
141148
return result;
142149
}
143150
bfs() {
@@ -146,7 +153,8 @@ class BinarySearchTree {
146153
// 5 19
147154
// / \ / \
148155
// 1 6 17 21
149-
// Output - 10, 5, 19, 1, 6, 17, 21
156+
157+
// Output - 10,5,19,1,6,17,21
150158
let result = [];
151159
let queue = [];
152160
queue.push(this.root);
@@ -155,39 +163,57 @@ class BinarySearchTree {
155163
result.push(currentNode.value);
156164
if(currentNode.left) {
157165
queue.push(currentNode.left);
158-
}
159-
if (currentNode.right) {
160-
queue.push(currentNode.right)
166+
} if(currentNode.right) {
167+
queue.push(currentNode.right);
161168
}
162169
}
163170
return result;
164171
}
165172
}
166173

167174

168-
169175
// const bst = new BinarySearchTree(10)
170176
// bst.insert(5)
171-
// bst.insert(1)
177+
// bst.insert(3)
178+
// bst.insert(6)
179+
// bst.insert(2)
180+
// bst.insert(4)
172181
// bst.insert(6)
173182
// bst.insert(19)
174183
// bst.insert(17)
175184
// bst.insert(21)
185+
// bst.insert(16)
186+
// bst.insert(20)
187+
// bst.insert(23)
188+
// bst.insert(7)
189+
// bst.insert(18)
190+
176191

177-
const bst = new BinarySearchTree(4)
178-
bst.insert(2)
179-
bst.insert(7)
180-
bst.insert(1)
181-
bst.insert(3)
182-
bst.insert(6)
183-
bst.insert(9)
192+
// const bst = new BinarySearchTree('G')
193+
// bst.insert('H')
194+
// bst.insert('I')
195+
// bst.insert('B')
196+
// bst.insert('P')
197+
// bst.insert('N')
198+
// bst.insert('O')
199+
// bst.insert('S')
200+
// bst.insert('K')
201+
202+
// const bst = new BinarySearchTree(4)
203+
// bst.insert(2)
204+
// bst.insert(7)
205+
// bst.insert(1)
206+
// bst.insert(3)
207+
// bst.insert(6)
208+
// bst.insert(9)
184209

185210

186211
console.log("BST Min: ",bst.min());
187212
console.log("BST Max: ",bst.max());
188213

189214
console.log("Is Found 2: ",bst.lookup(2));
190-
console.log("Is Found 6: ",bst.lookup(6));
215+
console.log("Is Found 61: ",bst.lookup(61));
216+
console.log("Is Found 17: ",bst.lookup(17));
191217

192218
// DFS!!!
193219
// in-order: 1, 5, 6, 10, 17, 19, 21
@@ -202,29 +228,52 @@ console.log("Postorder:",bst.dfsPostorder());
202228
// BFS: 10, 5, 19, 1, 6, 17, 21
203229
console.log("BFS:",bst.bfs());
204230

205-
206231
function getLeafCountOfBinaryTree(node) {
207-
if(node === null) return 0;
208-
if(node.left === null && node.right === null) return 1;
209-
return getLeafCountOfBinaryTree(node.left) + getLeafCountOfBinaryTree(node.right);
232+
if(node == null) return 0;
233+
if(node.left == null && node.right == null) return 1;
234+
return getLeafCountOfBinaryTree(node.left) + getLeafCountOfBinaryTree(node.right);
210235
}
211236

237+
238+
239+
240+
// function getLeafCountOfBinaryTree(node) {
241+
// if(node == null) return 0;
242+
// if(node.left == null && node.right == null) return 1;
243+
// return getLeafCountOfBinaryTree(node.left) + getLeafCountOfBinaryTree(node.right);
244+
// }
245+
212246
console.log("COUNT LEAF NODE: ", getLeafCountOfBinaryTree(bst.root));
213247

214248

215249
function hightOfBinaryTree(node) {
216-
if(node === null) {
217-
return 0;
218-
} else {
219-
let lTree = hightOfBinaryTree(node.left);
220-
let rTree = hightOfBinaryTree(node.right);
221-
if(lTree < rTree) {
222-
return lTree + 1;
223-
} else {
224-
return rTree + 1;
225-
}
226-
}
250+
if(node == null) {
251+
return 0;
252+
} else {
253+
let lTree = hightOfBinaryTree(node.left);
254+
let RTree = hightOfBinaryTree(node.right);
255+
if(lTree < RTree) {
256+
return lTree + 1;
257+
} else {
258+
return RTree + 1;
259+
}
260+
}
227261
}
228-
console.log("HIGHT OF BINARY TREE: ", hightOfBinaryTree(bst.root));
229262

230263

264+
265+
266+
// function hightOfBinaryTree(node) {
267+
// if(node == null) {
268+
// return 0;
269+
// } else {
270+
// let lTree = hightOfBinaryTree(node.left);
271+
// let RTree = hightOfBinaryTree(node.right);
272+
// if(lTree < RTree) {
273+
// return lTree + 1;
274+
// } else {
275+
// return RTree + 1;
276+
// }
277+
// }
278+
// }
279+
console.log("HIGHT OF BINARY TREE: ", hightOfBinaryTree(bst.root));

Datastructure/Graphs/practice.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ class UndirectedGraph {
44
this.adjecentList = {};
55
}
66
addVertices(node) {
7-
this.adjecentList[node] = [];
8-
this.noOfEdges++;
7+
this.adjecentList[node] = [];
98
}
109
addEdges(node1, node2) {
1110
this.adjecentList[node1].push(node2);

Datastructure/LinkedList/doublylinkedlist.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ class Node {
3434

3535
class LinkedList {
3636
constructor(value) {
37-
this.head = {
38-
value: value,
39-
next: null,
40-
prev: null // for doubly linkedList
41-
}
37+
this.head = new Node(value);
4238
this.tail = this.head;
4339
this.length = 1;
4440
}

0 commit comments

Comments
 (0)