-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrees.js
138 lines (119 loc) · 3.73 KB
/
trees.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* A little library for manipulating trees */
function leaf(color=undefined) { return { isLeaf: true, color: color } }
function branch(l,r,color=undefined) {
return { isLeaf: false, left: l, right: r, color: color };
}
/* Helper functions for adding and removing branches */
// a path is a list ['l','r','l'] that identifies a certain node in a tree
// replace a leaf at position `path` by branch(leaf(),leaf())
function bud(tree, path, color=undefined) {
if (tree.isLeaf && path === '') {
return branch(leaf(color),leaf(color), tree.color);
}
else if (!tree.isLeaf && path[0] === 'l') {
return branch(bud(tree.left, path.slice(1), color), tree.right, tree.color);
}
else if (!tree.isLeaf && path[0] === 'r') {
return branch(tree.left, bud(tree.right, path.slice(1), color), tree.color);
}
else {
console.log("incorrect path while budding");
}
}
// replace a leaf at position `path` by branch(leaf(),leaf())
function prune(tree, path) {
if (path === '') {
return leaf(tree.color);
}
else if (path[0] === 'l') {
return branch(prune(tree.left, path.slice(1)), tree.right, tree.color);
}
else if (path[0] === 'r') {
return branch(tree.left, prune(tree.right, path.slice(1)), tree.color);
}
else {
console.log("incorrect path while pruning");
}
}
/* The 'Seven trees in One' algorithm */
function split(taggedtrees, n, t, tl, tr) {
let [tag, trees] = taggedtrees;
if (trees.length == (n+1) && tag === t) {
if (trees[n].isLeaf) {
return [ tl, trees.slice(0,n) ];
} else {
let tree = trees[n];
return [ tr, trees.slice(0,n).concat([tree.left, tree.right]) ]
}
}
else {
return taggedtrees;
}
}
function merge(taggedtrees, n, t, tl, tr) {
let [tag, trees]= taggedtrees;
if (trees.length == (n+2) && tag == tr) {
let l = trees[n];
let r = trees[n+1];
return [ t, trees.slice(0,n).concat([ branch(l,r) ]) ];
}
else if (trees.length == n && tag == tl) {
return [ t, trees.concat([ leaf() ]) ];
}
else {
return taggedtrees;
}
}
function makeCommand(command, n, { t=0, tl=0, tr=0 } = {}) {
return { command: command, n: n, t:t, tl:tl, tr:tr };
}
function invertCommand(cmd) {
return makeCommand(
cmd.command == 'split' ? 'merge' : 'split',
cmd.n,
cmd);
}
function runCommand(cmd, taggedtrees) {
return (cmd.command == 'split')
? split(taggedtrees, cmd.n, cmd.t, cmd.tl, cmd.tr)
: merge(taggedtrees, cmd.n, cmd.t, cmd.tl, cmd.tr);
}
// A winning strategy for nuclear pennies
let cmdsOneToSeven = [
makeCommand('split',0),
makeCommand('split',1),
makeCommand('split',2),
makeCommand('split',3),
makeCommand('merge',0,{t:1}),
makeCommand('merge',1,{tl:1}),
makeCommand('split',4),
makeCommand('merge',2),
makeCommand('split',5),
makeCommand('merge',1),
makeCommand('split',4),
makeCommand('merge',2),
makeCommand('split',5,{tr:1}),
makeCommand('split',6,{t:1}),
makeCommand('merge',3),
makeCommand('merge',4),
makeCommand('merge',5),
makeCommand('merge',6)
]
let cmdsSevenToOne = cmdsOneToSeven.map(invertCommand).reverse();
function oneToSeven(tree) {
let taggedtrees = [0,[tree]];
for (var cmd of cmdsOneToSeven) {
taggedtrees = runCommand(cmd, taggedtrees);
}
return taggedtrees[1];
}
function sevenToOne(trees) {
let taggedtrees = [0,trees];
for (var cmd of cmdsSevenToOne) {
taggedtrees = runCommand(cmd, taggedtrees);
}
return taggedtrees[1][0];
}
let tr = branch(branch(leaf(),leaf()),leaf());
let sev = oneToSeven(tr);
let re = sevenToOne(sev);