-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path6-bst-opt.js
More file actions
31 lines (25 loc) · 838 Bytes
/
6-bst-opt.js
File metadata and controls
31 lines (25 loc) · 838 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
'use strict';
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) => {
const i = data < root[0] ? 1 : 2;
if (!root[i]) root[i] = tree(data);
else tree.insert(root[i], data);
};
tree.search = (root, data) => {
const value = root[0];
if (data === value) return root;
const next = root[data < value ? 1 : 2];
return next ? tree.search(next, data) : null;
};
// 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);
const node = tree.search(root, 2);
console.dir(node, { depth: 3 });