Skip to content

Commit 36c7fea

Browse files
willmadisonmiorel
authored andcommitted
Add level order traversal Kotlin goody (#369)
Closes #314.
1 parent d8b4be6 commit 36c7fea

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package traverse_level_order
2+
3+
public data class TreeNode(val `val`: Int, var left: TreeNode? = null, var right: TreeNode? = null)
4+
5+
public fun TreeNode?.traverseLevelOrder(): Sequence<Collection<TreeNode>> {
6+
if (this == null) {
7+
return sequenceOf()
8+
}
9+
10+
var nodesAtLevel = listOf(this)
11+
12+
return sequence {
13+
while (nodesAtLevel.isNotEmpty()) {
14+
yield(nodesAtLevel)
15+
16+
nodesAtLevel = nodesAtLevel.flatMap { listOf(it.left, it.right) }.filterNotNull()
17+
}
18+
}
19+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package traverse_level_order
2+
3+
import kotlin.test.*
4+
5+
internal class LevelOrderTraversalTest {
6+
7+
@Test
8+
fun itTraversesLevelOrderEmptyTree() {
9+
val root: TreeNode? = null
10+
assertContentEquals(
11+
root.traverseLevelOrder().flatMap { it }.map { it.`val` }.toList(), emptyList<Int>())
12+
}
13+
14+
@Test
15+
fun itTraversesLevelOrderSingleNode() {
16+
val root: TreeNode = TreeNode(0)
17+
assertContentEquals(
18+
root.traverseLevelOrder().flatMap { it }.map { it.`val` }.toList(), listOf<Int>(0))
19+
}
20+
21+
@Test
22+
fun itTraversesLevelOrderFullTree() {
23+
val root: TreeNode =
24+
TreeNode(
25+
1,
26+
TreeNode(2, TreeNode(4, TreeNode(8)), TreeNode(5)),
27+
TreeNode(3, TreeNode(6), TreeNode(7)))
28+
assertContentEquals(
29+
root.traverseLevelOrder().flatMap { it }.map { it.`val` }.toList(),
30+
listOf<Int>(1, 2, 3, 4, 5, 6, 7, 8))
31+
}
32+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "traverseLevelOrder()"
3+
}

workspaces/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,6 +2496,32 @@ public fun lcm(a: Int, b: Int): Int = a / gcd(a, b) * b
24962496
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
24972497
`;
24982498

2499+
exports[`App can equip single goody: Kotlin traverseLevelOrder() 1`] = `
2500+
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
2501+
// Adventure Pack commit fake-commit-hash
2502+
// Running at: https://example.com/
2503+
2504+
public data class TreeNode(val \`val\`: Int, var left: TreeNode? = null, var right: TreeNode? = null)
2505+
2506+
public fun TreeNode?.traverseLevelOrder(): Sequence<Collection<TreeNode>> {
2507+
if (this == null) {
2508+
return sequenceOf()
2509+
}
2510+
2511+
var nodesAtLevel = listOf(this)
2512+
2513+
return sequence {
2514+
while (nodesAtLevel.isNotEmpty()) {
2515+
yield(nodesAtLevel)
2516+
2517+
nodesAtLevel = nodesAtLevel.flatMap { listOf(it.left, it.right) }.filterNotNull()
2518+
}
2519+
}
2520+
}
2521+
2522+
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
2523+
`;
2524+
24992525
exports[`App can equip single goody: Python 3 UnionFind 1`] = `
25002526
"########################## BEGIN ADVENTURE PACK CODE ###########################
25012527
# Adventure Pack commit fake-commit-hash

workspaces/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,28 @@ import gcd_int_int.gcd
13861386
public fun lcm(a: Int, b: Int): Int = a / gcd(a, b) * b"
13871387
`;
13881388
1389+
exports[`App can render goody: Kotlin traverseLevelOrder() 1`] = `
1390+
"package traverse_level_order
1391+
1392+
public data class TreeNode(val \`val\`: Int, var left: TreeNode? = null, var right: TreeNode? = null)
1393+
1394+
public fun TreeNode?.traverseLevelOrder(): Sequence<Collection<TreeNode>> {
1395+
if (this == null) {
1396+
return sequenceOf()
1397+
}
1398+
1399+
var nodesAtLevel = listOf(this)
1400+
1401+
return sequence {
1402+
while (nodesAtLevel.isNotEmpty()) {
1403+
yield(nodesAtLevel)
1404+
1405+
nodesAtLevel = nodesAtLevel.flatMap { listOf(it.left, it.right) }.filterNotNull()
1406+
}
1407+
}
1408+
}"
1409+
`;
1410+
13891411
exports[`App can render goody: Python 3 UnionFind 1`] = `
13901412
"class UnionFind:
13911413

0 commit comments

Comments
 (0)