Skip to content

Commit 4b2d3e9

Browse files
committed
chore: adding minimal search tree
1 parent 7c6ce29 commit 4b2d3e9

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@
7474

7575
## Working with this repo
7676

77-
Download or clone in local machine. Then run individual file in node console to see the results.
77+
- Download or clone in local machine.
78+
- Run `npm ci`
79+
- Then run individual file to see result on console.
80+
- You should use `node filename` in console to see results.
81+
82+
7883

7984
## What to practice?
8085

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "coding-examples-cs-fundamentals",
3+
"version": "1.0.0",
4+
"description": "Coding Interview Theory and Excercises",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/rupeshtiwari/coding-examples-cs-fundamentals.git"
12+
},
13+
"keywords": [
14+
"algorithm",
15+
"datastructure"
16+
],
17+
"author": "Rupesh Tiwari",
18+
"license": "ISC",
19+
"bugs": {
20+
"url": "https://github.com/rupeshtiwari/coding-examples-cs-fundamentals/issues"
21+
},
22+
"homepage": "https://github.com/rupeshtiwari/coding-examples-cs-fundamentals#readme",
23+
"dependencies": {}
24+
}

src/data-structure/interview-questions/graph/2-minimal-bs-tree.mjs renamed to src/interview-questions/graph/2-minimal-bs-tree.mjs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
class Node {
2-
constructor(data) {
3-
this.value = data;
2+
constructor(value) {
3+
this.value = value;
44
this.left = null;
55
this.right = null;
66
}
7+
78
toString() {
8-
return `${this.value}, left -> ${this.left?.value}, right -> ${this.right?.value}`;
9+
function print(prefix, node, isLeft) {
10+
if (!node) return;
11+
if (prefix) {
12+
console.log(prefix + (isLeft ? '↙️--' : '↘️--') + node.value);
13+
} else {
14+
console.log('▶️' + node.value);
15+
}
16+
print(prefix + (isLeft ? '🔽 ' : ' '), node.left, true);
17+
print(prefix + (isLeft ? '🔽 ' : ' '), node.right, false);
18+
}
19+
print('', this, false);
920
}
1021
}
1122

23+
/**
24+
* Time complexity O(nlogn)
25+
* @param arr Array
26+
* @param start start index
27+
* @param end end index of array
28+
* @returns root node
29+
*/
1230
export function createMinimalBSTree(arr, start, end) {
1331
if (end < start) {
1432
return;

0 commit comments

Comments
 (0)