Skip to content

Commit a9a9c8b

Browse files
committed
chore: adding searching logic
1 parent 29560ab commit a9a9c8b

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ In Binary Search Tree nodes are:
297297

298298
BSTs get an average case of `O(log n)` on gets, adds, and deletes, but they can have a worst case of `O(n)` if you do something like add a sorted list to a BST. Go ahead, do a BST then add [1,2,3,4,5] to it.
299299

300+
- [Binary Search Tree Implementation Exercise](https://codepen.io/roopkt/pen/RwpJBOw?editors=0010)
301+
300302
### Trie
301303

302304
Trie is a tree, in which we store only one character at each node. This final key value pair is stored in the leaves.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
export class Tree {
2+
constructor(data) {
3+
this.data = data;
4+
this.left = null;
5+
this.right = null;
6+
}
7+
8+
add(data, start = null) {
9+
start = start || this;
10+
11+
if (!start.left & (data < start.data)) {
12+
start.left = new Tree(data);
13+
}
14+
15+
if (!start.right & (data >= start.data)) {
16+
start.right = new Tree(data);
17+
}
18+
19+
while (start.left && start.right) {
20+
if (data > start.left.data) {
21+
this.add(data, start.right);
22+
}
23+
}
24+
25+
while (start.left) {
26+
if (data < start.left.data) {
27+
this.add(data, start.left);
28+
}
29+
}
30+
}
31+
32+
toObject() {
33+
return this.data;
34+
}
35+
}
36+
37+
// Test
38+
39+
const tree = new Tree();
40+
tree.add([]);

0 commit comments

Comments
 (0)