-
Notifications
You must be signed in to change notification settings - Fork 13
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
Merged
miorel
merged 7 commits into
code-chronicles-code:main
from
willmadison:kt-level-order-traversal
Aug 24, 2024
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
88b2c7a
adds level order traversal Kotlin goody
willmadison ec4380b
small refactor to leverage Kotlin Sequences instead of Streams
willmadison 078d7b0
adds new line character to the end of files
willmadison a64c453
removing unnecessary itermediate list in favor of sequence scope + yield
willmadison 49e7289
properly formatting using yarn format.
willmadison 9fb6abd
tweaks Kotlin level order traversal to return Sequence<Collection<Tre…
willmadison fa604b7
updated snapshots and formatting
willmadison File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
workspaces/adventure-pack/goodies/kotlin/src/common/TreeNode.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package common | ||
|
||
public data class TreeNode(val value: Int, var left: TreeNode? = null, var right: TreeNode? = null) | ||
willmadison marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
26 changes: 26 additions & 0 deletions
26
workspaces/adventure-pack/goodies/kotlin/src/traverse_level_order/Main.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package traverse_level_order | ||
|
||
import common.TreeNode | ||
|
||
public fun TreeNode?.traverseLevelOrder(): Sequence<TreeNode> { | ||
willmadison marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (this == null) return sequenceOf() | ||
|
||
val q = ArrayDeque<TreeNode>() | ||
q.add(this) | ||
|
||
return sequence { | ||
while (q.isNotEmpty()) { | ||
val current = q.removeFirst() | ||
|
||
yield(current) | ||
|
||
if (current.left != null) { | ||
q.add(current.left!!) | ||
} | ||
|
||
if (current.right != null) { | ||
q.add(current.right!!) | ||
} | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
workspaces/adventure-pack/goodies/kotlin/src/traverse_level_order/Test.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package traverse_level_order | ||
|
||
import common.TreeNode | ||
import kotlin.test.* | ||
|
||
internal class LevelOrderTraversalTest { | ||
|
||
@Test | ||
fun itTraversesLevelOrderEmptyTree() { | ||
val root: TreeNode? = null | ||
assertContentEquals(root.traverseLevelOrder().map { it.value }.toList(), mutableListOf<Int>()) | ||
} | ||
|
||
@Test | ||
fun itTraversesLevelOrderSingleNode() { | ||
val root: TreeNode = TreeNode(0) | ||
assertContentEquals(root.traverseLevelOrder().map { it.value }.toList(), mutableListOf<Int>(0)) | ||
} | ||
|
||
@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().map { it.value }.toList(), | ||
mutableListOf<Int>(1, 2, 3, 4, 5, 6, 7, 8)) | ||
willmadison marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
workspaces/adventure-pack/goodies/kotlin/src/traverse_level_order/goody.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "traverseLevelOrder()" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.