Skip to content

Commit bc384b0

Browse files
New code
1 parent e668bd1 commit bc384b0

File tree

6 files changed

+365
-134
lines changed

6 files changed

+365
-134
lines changed

Datastructure/BinaryTree/index.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,12 @@ 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-
// }
73-
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+
}
7473
lookup(value) {
7574
let currentNode = this.root;
7675
while(currentNode) {

Datastructure/BinaryTree/practice.js

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ class Node {
3232
}
3333
}
3434
class BinarySearchTree {
35+
3536
constructor(value) {
36-
this.root = new Node(value);
37-
this.count = 1;
37+
this.root = new Node(value);
38+
this.length = 1;
3839
}
39-
40+
4041
insert(value) {
4142
let newNode = new Node(value);
43+
4244
let traverse = node => {
4345
if(value < node.value) {
4446
if(!node.left) {
@@ -54,11 +56,8 @@ class BinarySearchTree {
5456
}
5557
}
5658
}
57-
traverse(this.root);
58-
}
5959

60-
size() {
61-
return this.count;
60+
traverse(this.root);
6261
}
6362

6463
min() {
@@ -83,14 +82,18 @@ class BinarySearchTree {
8382
if(value === currentNode.value) {
8483
return true;
8584
} else if(value < currentNode.value) {
86-
currentNode = currentNode.left
85+
currentNode = currentNode.left;
8786
} else {
8887
currentNode = currentNode.right;
8988
}
9089
}
9190
return false;
9291
}
9392

93+
size() {
94+
return this.length;
95+
}
96+
9497
dfsInorder() {
9598
// left, middle, right
9699
// 10
@@ -101,12 +104,12 @@ class BinarySearchTree {
101104
// / \
102105
// 2 4
103106

104-
// Output = 1,5,6,10,17,19,21
107+
// Output = 2,3,4,5,6,10,17,19,21
105108
let result = [];
106109
let searchTree = node => {
107110
if(node.left) searchTree(node.left);
108111
result.push(node.value);
109-
if(node.right) searchTree(node.right)
112+
if(node.right) searchTree(node.right);
110113
}
111114
searchTree(this.root);
112115
return result;
@@ -124,7 +127,7 @@ class BinarySearchTree {
124127
let searchTree = node => {
125128
result.push(node.value);
126129
if(node.left) searchTree(node.left);
127-
if(node.right) searchTree(node.right)
130+
if(node.right) searchTree(node.right);
128131
}
129132
searchTree(this.root);
130133
return result;
@@ -161,9 +164,12 @@ class BinarySearchTree {
161164
while(queue.length) {
162165
let currentNode = queue.shift();
163166
result.push(currentNode.value);
167+
164168
if(currentNode.left) {
165169
queue.push(currentNode.left);
166-
} if(currentNode.right) {
170+
}
171+
172+
if(currentNode.right) {
167173
queue.push(currentNode.right);
168174
}
169175
}
@@ -199,13 +205,13 @@ class BinarySearchTree {
199205
// bst.insert('S')
200206
// bst.insert('K')
201207

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)
208+
const bst = new BinarySearchTree(4)
209+
bst.insert(2)
210+
bst.insert(7)
211+
bst.insert(1)
212+
bst.insert(3)
213+
bst.insert(6)
214+
bst.insert(9)
209215

210216

211217
console.log("BST Min: ",bst.min());
@@ -233,47 +239,20 @@ function getLeafCountOfBinaryTree(node) {
233239
if(node.left == null && node.right == null) return 1;
234240
return getLeafCountOfBinaryTree(node.left) + getLeafCountOfBinaryTree(node.right);
235241
}
236-
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-
246242
console.log("COUNT LEAF NODE: ", getLeafCountOfBinaryTree(bst.root));
247243

248-
249244
function hightOfBinaryTree(node) {
250245
if(node == null) {
251246
return 0;
252247
} else {
253248
let lTree = hightOfBinaryTree(node.left);
254-
let RTree = hightOfBinaryTree(node.right);
255-
if(lTree < RTree) {
249+
let rTree = hightOfBinaryTree(node.right);
250+
if(lTree > rTree) {
256251
return lTree + 1;
257252
} else {
258-
return RTree + 1;
253+
return rTree + 1;
259254
}
260255
}
261256
}
262257

263-
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-
// }
279258
console.log("HIGHT OF BINARY TREE: ", hightOfBinaryTree(bst.root));

Datastructure/Graphs/practice.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
class UndirectedGraph {
2-
constructor() {
3-
this.noOfEdges = 0;
4-
this.adjecentList = {};
5-
}
6-
addVertices(node) {
7-
this.adjecentList[node] = [];
8-
}
9-
addEdges(node1, node2) {
10-
this.adjecentList[node1].push(node2);
11-
this.adjecentList[node2].push(node1);
12-
}
13-
showConnections() {
14-
console.log(this.adjecentList);
15-
}
2+
constructor() {
3+
this.noOfEdges = 0;
4+
this.adjacentList = {};
5+
}
6+
7+
addVertices(node) {
8+
this.adjacentList[node] = [];
9+
}
10+
11+
addEdges(node1, node2) {
12+
this.adjacentList[node1].push(node2);
13+
this.adjacentList[node2].push(node1);
14+
}
15+
16+
showConnections() {
17+
return console.log(this.adjacentList);
18+
}
1619
}
1720
const undirectedGrph = new UndirectedGraph();
1821
undirectedGrph.addVertices('0');

Datastructure/LinkedList/practice.js

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,51 +45,47 @@ class SinglyLinkedList {
4545
this.tail = this.head;
4646
this.length = 1;
4747
}
48-
prepand(value) {
48+
append(value) {
4949
let newNode = new Node(value);
5050
newNode.next = this.head;
51-
this.head.prev = newNode; // for doubly linkedList
51+
this.head.prev = newNode; // Doubly LinkedList
5252
this.head = newNode;
5353
this.length++;
5454
return this;
5555
}
56-
append(value) {
56+
prepand(value) {
5757
let newNode = new Node(value);
58-
newNode.prev = this.tail; // for doubly linkedList
5958
this.tail.next = newNode;
59+
newNode.prev = this.tail; // Doubly LinkedList
6060
this.tail = newNode;
6161
this.length++;
6262
return this;
6363
}
64-
insert(index, value) {
64+
insert(value, index) {
6565
if(index >= this.length) {
6666
return this.append(value);
6767
}
6868
let newNode = new Node(value);
69-
let leader = this.traverse(index - 1);
70-
let nextNode = leader.next;
71-
leader.next = newNode;
72-
newNode.prev = leader; // for doubly linkedList
69+
let leaderNode = this.traverse(index - 1);
70+
let nextNode = leaderNode.next;
71+
leaderNode.next = newNode;
72+
newNode.prev = leaderNode; // Doubly LinkedList
7373
newNode.next = nextNode;
74-
nextNode.prev = newNode // for doubly linkedList
74+
nextNode.prev = newNode; // Doubly LinkedList
75+
this.length++;
7576
return this.printList();
7677
}
77-
7878
remove(index) {
7979
if(index > this.length) {
80-
return "Not possible !!!";
81-
}
82-
let leader = this.traverse(index - 1);
83-
let unwantedNode = leader.next;
84-
leader.next = unwantedNode.next;
80+
return "Invalid Input !!!"
81+
}
82+
let leaderNode = this.traverse(index - 1);
83+
let unwantedNode = leaderNode.next;
84+
leaderNode.next = unwantedNode.next;
8585
this.length--;
8686
return this.printList();
8787
}
88-
8988
reverse() {
90-
if(!this.head.next) {
91-
return this.head;
92-
}
9389
this.tail = this.head;
9490
let first = this.head;
9591
let second = first.next;
@@ -103,13 +99,12 @@ class SinglyLinkedList {
10399
this.head = first;
104100
return this.printList();
105101
}
106-
107102
traverse(index) {
103+
let count = 0;
108104
let currentNode = this.head;
109-
let counter = 0;
110-
while(index != counter) {
105+
while(index != count) {
106+
count++;
111107
currentNode = currentNode.next;
112-
counter++;
113108
}
114109
return currentNode;
115110
}

0 commit comments

Comments
 (0)