-
Notifications
You must be signed in to change notification settings - Fork 92
/
P63.kt
34 lines (30 loc) · 1.06 KB
/
P63.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
package org.kotlin99.binarytrees
import com.natpryce.hamkrest.assertion.assertThat
import org.junit.Test
import org.kotlin99.binarytrees.P57Test.Companion.equalToTree
import org.kotlin99.binarytrees.Tree.End
import org.kotlin99.binarytrees.Tree.Node
fun <T> completeBinaryTree(nodeAmount: Int, value: T): Tree<T> {
fun generate(nodeAddress: Int): Tree<T> =
if (nodeAddress > nodeAmount) End
else Node(value, generate(nodeAddress * 2), generate(nodeAddress * 2 + 1))
return generate(1)
}
class P63Test {
@Test fun `construct complete binary tree`() {
assertThat(completeBinaryTree(1, "x"), equalToTree(
Node("x")
))
assertThat(completeBinaryTree(2, "x"), equalToTree(
Node("x", Node("x"))
))
assertThat(completeBinaryTree(3, "x"), equalToTree(
Node("x", Node("x"), Node("x"))
))
assertThat(completeBinaryTree(6, "x"), equalToTree(
Node("x",
Node("x", Node("x"), Node("x")),
Node("x", Node("x"), End))
))
}
}