Skip to content

Commit 85a2f1a

Browse files
prettier
1 parent 0dd4fc2 commit 85a2f1a

File tree

3 files changed

+93
-77
lines changed

3 files changed

+93
-77
lines changed

algorithms/bfs-dfs.md

-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ class Tree {
113113
let nodesToProcess : Array<number> = [0]
114114

115115
while(nodesToProcess.length){
116-
console.log(nodesToProcess, visitedNodes)
117116
let node = nodesToProcess.shift();
118117

119118
// process the node if there is a callback

algorithms/binarySearch.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ Implemenent Binary Search in an array
44

55
## Solution
66

7-
```js
8-
const binarysearch = function(array, number) {
7+
```ts
8+
const binarysearch = function(array: Array<number>, number: number) {
99
if (array.length === 0) return false;
1010
if (array.length === 1) return array[0] === number;
1111

algorithms/binarytree.md

+91-74
Original file line numberDiff line numberDiff line change
@@ -10,177 +10,194 @@
1010

1111
### Solution
1212

13-
```js
13+
```ts
14+
interface MyNode {
15+
value: number;
16+
left?: MyNode;
17+
right?: MyNode;
18+
}
19+
1420
// Tree representation
15-
const value13 = { value: 13, left: undefined, right: undefined};
16-
const value14 = { value: 14, left: value13, right: undefined};
17-
const value7 = { value: 7, left: undefined, right: undefined};
18-
const value4 = { value: 4, left: undefined, right: undefined};
19-
const value6 = { value: 6, left: value4, right: value7};
20-
const value1 = { value: 1, left: undefined, right: undefined};
21-
const value3 = { value: 3, left: value1, right: value6 };
22-
const value10 = { value: 10, left: undefined, right: value14 };
23-
const root = { value: 8, left: value3, right: value10 };
21+
const value13: MyNode = { value: 13, left: undefined, right: undefined };
22+
const value14: MyNode = { value: 14, left: value13, right: undefined };
23+
const value7: MyNode = { value: 7, left: undefined, right: undefined };
24+
const value4: MyNode = { value: 4, left: undefined, right: undefined };
25+
const value6: MyNode = { value: 6, left: value4, right: value7 };
26+
const value1: MyNode = { value: 1, left: undefined, right: undefined };
27+
const value3: MyNode = { value: 3, left: value1, right: value6 };
28+
const value10: MyNode = { value: 10, left: undefined, right: value14 };
29+
const root: MyNode = { value: 8, left: value3, right: value10 };
2430

2531
/**
26-
* Find the path from root to a value
27-
*
28-
* @param {object} root - The root of the tree
29-
* @param {number} value - The value to search for
30-
*/
31-
const findPath = function(root, value){
32-
if(!root || !root.value) return [];
32+
* Find the path from root to a value
33+
*
34+
* @param {MyNode} root
35+
* @param {number} value
36+
* @returns {Array<MyNode>}
37+
*/
38+
const findPath = function(root: MyNode, value: number): Array<MyNode> {
39+
if (!root || !root.value) return [];
3340
this.path = this.path || {};
3441
this.path[`${value}`] = this.path[`${value}`] || [];
3542
this.path[`${value}`].push(root.value);
3643

37-
if(root.value === value) {
44+
if (root.value === value) {
3845
return this.path[`${value}`];
3946
} else {
40-
if(value > root.value) findPath(root.right, value);
47+
if (value > root.value) findPath(root.right, value);
4148
else if (value < root.value) findPath(root.left, value);
4249
}
4350

4451
return this.path[`${value}`];
45-
}
52+
};
4653

4754
/**
4855
* Find a value in the tree
4956
*
50-
* @param {object} root - The root of the tree
57+
* @param {MyNode} root - The root of the tree
5158
* @param {number} value - The value to search for
59+
* @returns {MyNode}
5260
*/
53-
const find = function(root, value){
54-
if(!root || !root.value) return undefined;
61+
const find = function(root: MyNode, value: number): MyNode {
62+
if (!root || !root.value) return undefined;
5563

56-
if(root.value === value) {
64+
if (root.value === value) {
5765
return root;
5866
} else {
59-
if(value > root.value) return find(root.right, value);
67+
if (value > root.value) return find(root.right, value);
6068
else if (value < root.value) return find(root.left, value);
6169
}
6270

6371
return root;
64-
}
72+
};
6573

6674
/**
6775
* Insert a value in the tree
6876
*
69-
* @param {object} root - The root of the tree
77+
* @param {MyNode} root - The root of the tree
7078
* @param {number} value - The value to insert
79+
* @returns {MyNode}
7180
*/
72-
const insert = function(root, value){
73-
if (value > root.value){
74-
if(root.right === undefined){
81+
const insert = function(root: MyNode, value: number): MyNode {
82+
if (value > root.value) {
83+
if (root.right === undefined) {
7584
root.right = { value: value, left: undefined, right: undefined };
7685
} else {
7786
insert(root.right, value);
7887
}
79-
} else if(value < root.value){
80-
if(root.left === undefined){
88+
} else if (value < root.value) {
89+
if (root.left === undefined) {
8190
root.left = { value: value, left: undefined, right: undefined };
8291
} else {
8392
insert(root.left, value);
8493
}
8594
}
8695
return root;
87-
}
96+
};
8897

8998
/**
9099
* Traverse the tree and process it through a callback
91100
*
92-
* @param {object} root - The root of the tree
101+
* @param {MyNode} root - The root of the tree
93102
* @param {function} cb
94103
*/
95-
const traverse = function(root, cb){
96-
if(root && root.value){
104+
const traverse = function(root: MyNode, cb: Function) {
105+
if (root && root.value) {
97106
cb(root.value);
98107
traverse(root.right, cb);
99108
traverse(root.left, cb);
100109
}
101-
}
110+
};
102111

103112
/**
104113
* Find the minimum value in the tree
105114
*
106-
* @param {object} root - The root of the tree
115+
* @param {MyNode} root - The root of the tree
116+
* @returns {number}
107117
*/
108-
const findMin = function(root){
109-
if(root.left) return findMin(root.left)
118+
const findMin = function(root: MyNode): number {
119+
if (root.left) return findMin(root.left);
110120
else return root.value;
111-
}
121+
};
112122

113123
/**
114124
* Find the maximum value in the tree
115125
*
116-
* @param {object} root - The root of the tree
126+
* @param {MyNode} root - The root of the tree
127+
* @returns {number}
117128
*/
118-
const findMax = function(root){
119-
if(root.right) return findMax(root.right)
129+
const findMax = function(root: MyNode): number {
130+
if (root.right) return findMax(root.right);
120131
else return root.value;
121-
}
132+
};
122133

123134
/**
124135
* Find the parent of a value in the tree
125136
*
126-
* @param {object} root - The root of the tree
137+
* @param {MyNode} root - The root of the tree
127138
* @param {number} value - The value to insert
139+
* @returns {MyNode}
128140
*/
129-
const findParentOfToBeDeletedNode = function(root, value){
130-
let node = root;
131-
const cb = (root) => {
132-
if(root && ((root.right && root.right.value === value)||(root.left && root.left.value === value)))
133-
node = root;
134-
}
135-
traverse(root, cb)
136-
return node;
137-
}
141+
const findParentOfToBeDeletedNode = function(
142+
root: MyNode,
143+
value: number
144+
): MyNode {
145+
let node = root;
146+
const cb = root => {
147+
if (
148+
root &&
149+
((root.right && root.right.value === value) ||
150+
(root.left && root.left.value === value))
151+
)
152+
node = root;
153+
};
154+
traverse(root, cb);
155+
return node;
156+
};
138157

139158
/**
140159
* Delete a node from the tree
141160
*
142-
* @param {object} root - The root of the tree
161+
* @param {MyNode} root - The root of the tree
143162
* @param {number} value - The value to insert
144163
*/
145-
const deleteNode = function(root, value){
164+
const deleteNode = function(root: MyNode, value: number) {
146165
let parent = findParentOfToBeDeletedNode(root, value);
147166
let node = find(root, value);
148167

149168
// leaves
150-
if(node.left === undefined && node.right === undefined){
151-
if(parent.left.value === node.value){
169+
if (node.left === undefined && node.right === undefined) {
170+
if (parent.left.value === node.value) {
152171
parent.left = undefined;
153-
} else if(parent.right.value === node.value){
172+
} else if (parent.right.value === node.value) {
154173
parent.right = undefined;
155174
}
156-
} // node with 1 child
157-
else if (node.left === undefined && node.right !== undefined){
175+
} else if (node.left === undefined && node.right !== undefined) {
176+
// node with 1 child
158177
parent.right = node.right;
159-
} // node with 1 child
160-
else if (node.left !== undefined && node.right === undefined){
178+
} else if (node.left !== undefined && node.right === undefined) {
179+
// node with 1 child
161180
parent.left = node.left;
162-
} // node with 2 children
163-
else if (node.left && node.right){
181+
} else if (node.left && node.right) {
182+
// node with 2 children
164183
// find the min in the subtree
165184
// and swap it with the value that would be deleted
166185
let min = node.left.value;
167186
let pointer = node;
168-
while(pointer){
187+
while (pointer) {
169188
min = pointer.value;
170-
pointer = pointer.left
189+
pointer = pointer.left;
171190
}
172191
node.value = min;
173192

174193
// Delete the dublicate
175194
pointer = node;
176-
while(pointer){
177-
if(pointer.left && pointer.left.value === min){
195+
while (pointer) {
196+
if (pointer.left && pointer.left.value === min) {
178197
pointer.left = undefined;
179198
}
180-
pointer = pointer.left
199+
pointer = pointer.left;
181200
}
182-
183201
}
184-
}
185-
202+
};
186203
```

0 commit comments

Comments
 (0)