-
Notifications
You must be signed in to change notification settings - Fork 1
/
bst.js
56 lines (50 loc) · 1.13 KB
/
bst.js
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
55
56
class Node {
constructor(key, val, next) {
this.key = key;
this.val = val;
this.next = next;
}
}
class BST {
left = null;
right = null;
root = null;
constructor(key, val, N) {
this.key = key;
this.val = val;
this.N = N;
}
size() {
return this.nodeSize(this.root);
}
nodeSize(node) {
if (node === null) return 0;
return node.N;
}
get(key) {
this.getNodeVal(node, key);
}
getNodeVal(node, key) {
if (node === null) return null;
const cmp = key.compareTo(node.key);
if (cmp < 0) return this.getNodeVal(x.left, key);
if (cmp > 0) return this.getNodeVal(x.right, key);
return x.val;
}
put(key, val) {
this.putValOnTree(this.root, key, val);
}
putValOnTree(node, key, val) {
if (node === null) return new Node(key, val, 1);
const cmp = key.compareTo(node.key);
if (cmp < 0) {
node.left = this.putValOnTree(x.left, key, val);
} else if (cmp > 0) {
node.right = this.putValOnTree(x.right, key, val);
} else {
x.val = val;
}
node.N = this.nodeSize(x.left) + this.nodeSize(x.right) + 1;
return node;
}
}