-
Notifications
You must be signed in to change notification settings - Fork 9
/
tree3.js
66 lines (54 loc) · 1.38 KB
/
tree3.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
57
58
59
60
61
62
63
64
65
66
const T = require('union-type');
const { K } = require('./combinators');
var Tree = T({
Empty: [],
Node: [K(true), Tree, Tree]
});
const { Empty, Node } = Tree;
Object.assign(Tree.prototype, {
drawTree() {
const draw = t => t.case({
Empty: K(['--(null)']),
Node(v, l, r) {
const [r_, ...rs] = draw(r);
const ls = draw(l);
const v_ = `--${v}`;
const ls_ = ls.map(x => ` |${x}`);
const rs_ = [' `' + r_, ...rs.map(x => ` ${x}`)];
return [v_, ...ls_, ...rs_];
}
});
return draw(this).join('\n');
}
});
const insert = (t, x) => {
return t.case({
Empty() {
return Node(x, Empty(), Empty());
},
Node(y, l, r) {
if (x < y) return Node(y, insert(l, x), r);
else if (y < x) return Node(y, l, insert(r, x));
else return t;
}
});
};
const buildTree_ = t => {
if (t.length === 0) {
return Empty();
} else {
const [x, ...xs] = t;
return insert(buildTree_(xs), x);
}
};
const buildTreeFromSortedList = l => {
if (l.length === 0) return Empty();
const n = Math.floor(l.length / 2);
const lt = l.slice(0, n);
const [x, ...rt] = l.slice(-(n + 1));
return Node(x, buildTreeFromSortedList(lt), buildTreeFromSortedList(rt));
};
console.log(
buildTreeFromSortedList([1, 2, 3, 4, 5, 6]).drawTree()
// [1,2,3,4,5,6].reduceRight(insert, Empty()).drawTree()
);