-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path5-bst.js
More file actions
39 lines (32 loc) · 1012 Bytes
/
5-bst.js
File metadata and controls
39 lines (32 loc) · 1012 Bytes
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
'use strict';
// Binary Search Tree
const tree = (data = null, left = null, right = null) => [data, left, right];
tree.data = (node, data = node[0]) => (node[0] = data, data);
tree.left = (node, data) => (data ? node[1] = tree(data) : node[1]);
tree.right = (node, data) => (data ? node[2] = tree(data) : node[2]);
tree.insert = (root, data) => {
if (data < root[0]) {
if (root[1] === null) root[1] = tree(data);
else tree.insert(root[1], data);
return;
}
if (root[2] === null) root[2] = tree(data);
else tree.insert(root[2], data);
};
tree.search = (root, data) => {
if (root === null) return null;
const value = root[0];
if (data === value) return root;
if (data < value) return tree.search(root[1], data);
return tree.search(root[2], data);
};
// Usage
const root = tree(5);
tree.insert(root, 7);
tree.insert(root, 9);
tree.insert(root, 2);
tree.insert(root, 3);
tree.insert(root, 1);
console.dir(root);
const node = tree.search(root, 2);
console.dir(node, { depth: 3 });