-
Notifications
You must be signed in to change notification settings - Fork 12
Add level order traversal Kotlin goody #369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
88b2c7a
ec4380b
078d7b0
a64c453
49e7289
9fb6abd
fa604b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package traverse_level_order | ||
|
||
public data class TreeNode(val `val`: Int, var left: TreeNode? = null, var right: TreeNode? = null) | ||
|
||
public fun TreeNode?.traverseLevelOrder(): Sequence<Collection<TreeNode>> { | ||
if (this == null) { | ||
return sequenceOf() | ||
} | ||
|
||
var nodesAtLevel = listOf(this) | ||
|
||
return sequence { | ||
while (nodesAtLevel.isNotEmpty()) { | ||
yield(nodesAtLevel) | ||
|
||
nodesAtLevel = nodesAtLevel.flatMap { listOf(it.left, it.right) }.filterNotNull() | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package traverse_level_order | ||
|
||
import kotlin.test.* | ||
|
||
internal class LevelOrderTraversalTest { | ||
|
||
@Test | ||
fun itTraversesLevelOrderEmptyTree() { | ||
val root: TreeNode? = null | ||
assertContentEquals( | ||
root.traverseLevelOrder().flatMap { it }.map { it.`val` }.toList(), emptyList<Int>()) | ||
} | ||
|
||
@Test | ||
fun itTraversesLevelOrderSingleNode() { | ||
val root: TreeNode = TreeNode(0) | ||
assertContentEquals( | ||
root.traverseLevelOrder().flatMap { it }.map { it.`val` }.toList(), listOf<Int>(0)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would imagine |
||
} | ||
|
||
@Test | ||
fun itTraversesLevelOrderFullTree() { | ||
val root: TreeNode = | ||
TreeNode( | ||
1, | ||
TreeNode(2, TreeNode(4, TreeNode(8)), TreeNode(5)), | ||
willmadison marked this conversation as resolved.
Show resolved
Hide resolved
|
||
TreeNode(3, TreeNode(6), TreeNode(7))) | ||
assertContentEquals( | ||
root.traverseLevelOrder().flatMap { it }.map { it.`val` }.toList(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the most effective test, I think it would be helpful to do an assert on the levels here. |
||
listOf<Int>(1, 2, 3, 4, 5, 6, 7, 8)) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "traverseLevelOrder()" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, any reason to prefer Collection over List for the return type?