-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathP65.kt
139 lines (122 loc) · 4.35 KB
/
P65.kt
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
139
package org.kotlin99.binarytrees
import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import org.junit.Test
import org.kotlin99.binarytrees.P64Test.Companion.toPrettyString
import org.kotlin99.binarytrees.Tree.End
import org.kotlin99.binarytrees.Tree.Node
import kotlin.math.pow
fun <T> Tree<T>.layout2(
x: Int = leftmostBranchXShift(),
y: Int = 1,
spaces: Spaces = Spaces(this)
): Tree<Positioned<T>> =
when (this) {
End -> End
is Node<T> -> Node(
value = Positioned(value, x, y),
left = left.layout2(x - spaces.toInt(), y + 1, spaces.decrease()),
right = right.layout2(x + spaces.toInt(), y + 1, spaces.decrease())
)
}
data class Spaces(val value: Int) {
constructor(tree: Tree<*>): this(tree.height() - 2)
fun decrease() = Spaces(value - 1)
fun toInt() = 2.pow(value)
}
private fun Tree<*>.leftmostBranchXShift(): Int {
fun leftmostBranchHeight(tree: Tree<*>): Int {
return when (tree) {
End -> 0
is Node -> leftmostBranchHeight(tree.left) + 1
}
}
val height = height() // Need the whole tree height here because leftmost branch might not be the tallest branch.
return (2..leftmostBranchHeight(this)).sumOf { Spaces(height - it).toInt() } + 1
}
private fun Int.pow(n: Int): Int = this.toDouble().pow(n.toDouble()).toInt()
class P65Test {
@Test fun `layout binary tree (2)`() {
assertThat(
Node("a").layout2().toPrettyString(),
equalTo("""
| 012
|0···
|1·a·
|2···
""".trimMargin()))
assertThat(
Node("a", Node("b")).layout2().toPrettyString(),
equalTo("""
| 0123
|0····
|1··a·
|2·b··
|3····
""".trimMargin()))
assertThat(
Node("a", Node("b", Node("c"))).layout2().toPrettyString(),
equalTo("""
| 012345
|0······
|1····a·
|2··b···
|3·c····
|4······
""".trimMargin()))
assertThat(
Node("a", Node("b", Node("c", Node("d")))).layout2().toPrettyString(),
equalTo("""
| 0123456789
|0··········
|1········a·
|2····b·····
|3··c·······
|4·d········
|5··········
""".trimMargin()))
assertThat(
Node("a", End, Node("b", End, Node("c"))).layout2().toPrettyString(),
equalTo("""
| 012345
|0······
|1·a····
|2···b··
|3····c·
|4······
""".trimMargin()))
assertThat(
Node("a", Node("b"), Node("c")).layout2().toPrettyString(),
equalTo("""
| 01234
|0·····
|1··a··
|2·b·c·
|3·····
""".trimMargin()))
assertThat(
Node("a", Node("b", Node("d"), Node("e")), Node("c", Node("f"), Node("g"))).layout2().toPrettyString(),
equalTo("""
| 012345678
|0·········
|1····a····
|2··b···c··
|3·d·e·f·g·
|4·········
""".trimMargin()))
}
@Test fun `illustration example`() {
assertThat(
"nkmcaedgupq".toList().toTree().layout2().toPrettyString(),
equalTo("""
| 0123456789012345678901234
|0·························
|1···············n·········
|2·······k···············u·
|3···c·······m·······p·····
|4·a···e···············q···
|5····d·g··················
|6·························
""".trimMargin()))
}
}