Skip to content

Conversation

willmadison
Copy link
Contributor

@willmadison willmadison commented Aug 22, 2024

Closes #314.

@willmadison willmadison requested a review from miorel as a code owner August 22, 2024 14:22
@miorel miorel requested review from simona1 and volkmchl August 22, 2024 14:55
@willmadison willmadison force-pushed the kt-level-order-traversal branch from 5879847 to 078d7b0 Compare August 22, 2024 15:13
Copy link
Contributor

@volkmchl volkmchl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but please also wait for another approval before submitting.

yield(current)

if (current.left != null) {
q.add(current.left!!)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not familiar with Kotlin either, and I was curious whether the non-null assertion is necessary, given that within the if condition this is bound to be non-null. The same for line 22.

import common.TreeNode

public fun TreeNode?.traverseLevelOrder(): Sequence<TreeNode> {
if (this == null) return sequenceOf()
Copy link
Contributor

@simona1 simona1 Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I recall correctly, I came across a comment in a previous PR suggesting that for this repo we prefer wrapping return statements in braces. Let's check with Miorel if that's the style we should all adopt. In TypeScript, this can be added to the linter as a rule and auto-fixed, not sure what kind of lint is available for Kotlin.

Copy link
Contributor

@simona1 simona1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thank you for working on this. I enjoyed reading it despite my unfamiliarity with Kotlin!

Copy link
Contributor

@miorel miorel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @willmadison! Kotlin is a breath of fresh air compared to Java sometimes 😄

I left a few comments, in particular I think we'd want a slightly different API for level order, that makes it easier to work with a level at a time. Let me know what you think.

@miorel
Copy link
Contributor

miorel commented Aug 23, 2024

Regarding the checks: you can take a look at the job summary (see for example https://github.com/code-chronicles-code/leetcode-curriculum/actions/runs/10514921554?pr=369) to see what failed. It looks like it's the snapshot tests. You will have to run yarn package-goodies:test -u somewhere within the workspaces/adventure-pack directory to update the snapshots, sorry this is not better documented. The context is that we have some snapshot tests that assert each goody can be extracted successfully. I broke goody extraction at least once before so this has been helpful to have. It also highlights when a change to the extraction code changes what will be shown on the website.

Separately I see that you already discovered I prefer the more concise 2 space indentation rather than the 4, hope that's not too annoying for you 😄

Copy link
Contributor

@miorel miorel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for the PR @willmadison! I had a few more comments but we can address those in a follow-up if you are interested.


public data class TreeNode(val `val`: Int, var left: TreeNode? = null, var right: TreeNode? = null)

public fun TreeNode?.traverseLevelOrder(): Sequence<Collection<TreeNode>> {
Copy link
Contributor

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?

fun itTraversesLevelOrderSingleNode() {
val root: TreeNode = TreeNode(0)
assertContentEquals(
root.traverseLevelOrder().flatMap { it }.map { it.`val` }.toList(), listOf<Int>(0))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would imagine <Int> for the list type is inferred from the use of 0 as the value?

TreeNode(2, TreeNode(4, TreeNode(8)), TreeNode(5)),
TreeNode(3, TreeNode(6), TreeNode(7)))
assertContentEquals(
root.traverseLevelOrder().flatMap { it }.map { it.`val` }.toList(),
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

@miorel miorel changed the title adds level order traversal Kotlin goody Add level order traversal Kotlin goody Aug 24, 2024
@miorel miorel merged commit 50b0b91 into code-chronicles-code:main Aug 24, 2024
3 of 6 checks passed
miorel pushed a commit that referenced this pull request Aug 24, 2024
miorel pushed a commit that referenced this pull request Aug 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Kotlin goody for traversing a binary tree in level order
4 participants