Skip to content

Commit 4bb8377

Browse files
committed
Refactor code: constants
1 parent 79408b7 commit 4bb8377

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

JavaScript/9-bst-ready.js

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,81 @@
11
'use strict';
22

3+
const TREE_KEY = 0;
4+
const TREE_VALUE = 1;
5+
const TREE_PARENT = 2;
6+
const TREE_LEFT = 3;
7+
const TREE_RIGHT = 4;
8+
39
const tree = (
410
key = null, value = null, parent = null, left = null, right = null
511
) => [
612
key, value, parent, left, right
713
];
814

915
const data = (node, key, value) => {
10-
if (!key) return node[0, 1];
11-
node[0] = key;
12-
node[1] = value;
16+
if (!key) return node.slice(TREE_KEY, TREE_VALUE);
17+
node[TREE_KEY] = key;
18+
node[TREE_VALUE] = value;
1319
};
1420

1521
const parent = (node, parent) => {
16-
if (!parent) return node[2];
17-
node[2] = parent;
22+
if (!parent) return node[TREE_PARENT];
23+
node[TREE_PARENT] = parent;
1824
};
1925

2026
const left = (node, key, value) => {
21-
if (!key) return node[3];
22-
node[3] = tree(key, value, node);
27+
if (!key) return node[TREE_LEFT];
28+
node[TREE_LEFT] = tree(key, value, node);
2329
};
2430

2531
const right = (node, key, value) => {
26-
if (!key) return node[4];
27-
node[4] = tree(key, value, node);
32+
if (!key) return node[TREE_RIGHT];
33+
node[TREE_RIGHT] = tree(key, value, node);
2834
};
2935

3036
const insert = (root, key, value) => {
31-
if (root[0] === null) return tree.data(root, key, value);
32-
const i = key < root[0] ? 3 : 4;
33-
if (root[i]) tree.insert(root[i], key, value);
34-
else root[i] = tree(key, value, root);
37+
if (root[TREE_KEY] === null) return tree.data(root, key, value);
38+
const edge = key < root[TREE_KEY] ? TREE_LEFT : TREE_RIGHT;
39+
if (root[edge]) tree.insert(root[edge], key, value);
40+
else root[edge] = tree(key, value, root);
3541
};
3642

3743
const push = (root, node) => {
38-
const i = node[0] < root[0] ? 3 : 4;
44+
const i = node[TREE_KEY] < root[TREE_KEY] ? TREE_LEFT : TREE_RIGHT;
3945
if (root[i]) {
4046
tree.push(root[i], node);
4147
return;
4248
}
4349
root[i] = node;
44-
node[2] = root;
50+
node[TREE_PARENT] = root;
4551
};
4652

4753
const search = (root, key) => {
48-
const k = root[0];
54+
const k = root[TREE_KEY];
4955
if (key === k) return root;
50-
const next = root[key < k ? 3 : 4];
56+
const next = root[key < k ? TREE_LEFT : TREE_RIGHT];
5157
if (next) {
52-
if (key === next[0]) return next;
58+
if (key === next[TREE_KEY]) return next;
5359
return tree.search(next, key);
5460
}
5561
};
5662

5763
const get = (root, key) => {
5864
const node = tree.search(root, key);
59-
if (node) return node[1];
65+
if (node) return node[TREE_VALUE];
6066
};
6167

6268
const set = (root, key, value) => {
6369
const node = tree.search(root, key);
64-
if (node) node[1] = value;
70+
if (node) node[TREE_VALUE] = value;
6571
else tree.insert();
6672
};
6773

6874
const del = (root, key) => {
6975
const node = tree.search(root, key);
7076
if (node) {
7177
const [, , parent, left, right] = node;
72-
const i = parent[3] === node ? 3 : 4;
78+
const i = parent[TREE_LEFT] === node ? TREE_LEFT : TREE_RIGHT;
7379
parent[i] = null;
7480
if (left) tree.push(parent, left);
7581
if (right) tree.push(parent, right);

0 commit comments

Comments
 (0)