-
Notifications
You must be signed in to change notification settings - Fork 92
/
P66.kt
135 lines (118 loc) · 4.13 KB
/
P66.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
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
fun <T> Tree<T>.layout3(parentX: Int? = null, shiftFromParent: Int = 0, y: Int = 1): Tree<Positioned<T>> {
return when (this) {
End -> End
is Node<T> -> {
fun haveNoPositionOverlap(tree1: Tree<Positioned<*>>, tree2: Tree<Positioned<*>>): Boolean =
(tree1.nodes().map { it.value.point }.intersect(tree2.nodes().map { it.value.point })).isEmpty()
var shift = 1
while (shift < 100) {
var x = parentX?.plus(shiftFromParent)
val positionedLeft = left.layout3(x, -shift, y + 1)
if (parentX == null && positionedLeft is Node<Positioned<T>>) {
x = positionedLeft.value.point.x + shift
} else if (parentX == null) {
x = 1
}
val positionedRight = right.layout3(x, shift, y + 1)
if (haveNoPositionOverlap(positionedLeft, positionedRight)) {
return Node(Positioned(value, x!!, y), positionedLeft, positionedRight)
}
shift += 1
}
throw IllegalStateException()
}
}
}
class P66Test {
@Test fun `layout binary tree (3)`() {
assertThat(
Node("a").layout3().toPrettyString(),
equalTo("""
| 012
|0···
|1·a·
|2···
""".trimMargin()))
assertThat(
Node("a", Node("b")).layout3().toPrettyString(),
equalTo("""
| 0123
|0····
|1··a·
|2·b··
|3····
""".trimMargin()))
assertThat(
Node("a", Node("b", Node("c"))).layout3().toPrettyString(),
equalTo("""
| 01234
|0·····
|1···a·
|2··b··
|3·c···
|4·····
""".trimMargin()))
assertThat(
Node("a", Node("b", Node("c", Node("d")))).layout3().toPrettyString(),
equalTo("""
| 012345
|0······
|1····a·
|2···b··
|3··c···
|4·d····
|5······
""".trimMargin()))
assertThat(
Node("a", End, Node("b", End, Node("c"))).layout3().toPrettyString(),
equalTo("""
| 01234
|0·····
|1·a···
|2··b··
|3···c·
|4·····
""".trimMargin()))
assertThat(
Node("a", Node("b", End, Node("d")), Node("c")).layout3().toPrettyString(),
equalTo("""
| 01234
|0·····
|1··a··
|2·b·c·
|3··d··
|4·····
""".trimMargin()))
assertThat(
Node("a", Node("b", Node("d"), Node("e")), Node("c", Node("f"), Node("g"))).layout3().toPrettyString(),
equalTo("""
| 012345678
|0·········
|1····a····
|2··b···c··
|3·d·e·f·g·
|4·········
""".trimMargin()))
}
@Test fun `illustration example`() {
assertThat(
"nkmcaedgupq".toList().toTree().layout3().toPrettyString(),
equalTo("""
| 012345678
|0·········
|1·····n···
|2···k···u·
|3··c·m·p··
|4·a·e···q·
|5··d·g····
|6·········
""".trimMargin()))
}
}