-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncTree.js
More file actions
54 lines (45 loc) · 1.13 KB
/
AsyncTree.js
File metadata and controls
54 lines (45 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class TreeNode {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
constructor() {
this.root = null;
}
async insert(value) {
const newNode = new TreeNode(value);
if (!this.root) {
this.root = newNode;
} else {
await this._insertNode(this.root, newNode);
}
}
async _insertNode(node, newNode) {
if (newNode.value < node.value) {
if (!node.left) {
node.left = newNode;
} else {
await this._insertNode(node.left, newNode);
}
} else {
if (!node.right) {
node.right = newNode;
} else {
await this._insertNode(node.right, newNode);
}
}
}
// Other tree operations (search, remove, etc.) can be added here
}
// Example usage:
(async () => {
const bst = new BinarySearchTree();
const response = await fetch('https://api.example.com/data');
const data = await response.json();
for (const value of data.values) {
await bst.insert(value);
}
})();