Skip to content

add tree traversal in golang #406

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
merged 2 commits into from
Oct 5, 2018

Conversation

Liikt
Copy link

@Liikt Liikt commented Oct 1, 2018

compiled with go version go1.11 linux/amd64 and without errors

@leios leios added the Hacktoberfest The label for all Hacktoberfest related things! label Oct 1, 2018
@Liikt Liikt changed the title add the golang files add tree traversal in golang Oct 1, 2018
@june128 june128 added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Oct 1, 2018
@leios leios mentioned this pull request Oct 2, 2018
@leios
Copy link
Member

leios commented Oct 4, 2018

Hey @Liikt, I trust you, but we are lacking Go expertise. Do you mind if I do some of these reviews and ask some dumb questions about Go, just to make sure I understand the syntax?

Copy link
Contributor

@nic-hartley nic-hartley 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! A couple of minor change ideas, but nothing that can't safely stay the same.

children []*node
}

func dfsRecursive(n *node) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider returning an array of elements or taking a visitor function, rather than just printing all of them.

That goes for all of these functions, not just this one.

Copy link
Author

Choose a reason for hiding this comment

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

I rather have a pointer since it is more memory efficient. I also find it way easier. Tbh i'm not really sure why an array would be better, since now i can build a native tree structure and don't have to build an array and do crazy offset math to figure out the children.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Liikt Yeah, that got shot down in other places too, since we're just demonstrating the algorithm, not trying to build reusable chunks of code. Disregard this one.

fmt.Println(n.id)
default:
fmt.Println("This is not a binary tree")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be simplified a bit by running code if lengths are greater than a certain amount, rather than just if they're equal to it. I'm not confident enough with Golang to suggest an alternative without an interpreter handy, but in pseudocode:

if children.length > 2:
  throw "Not a binary tree"

if children.length > 1:
  inOrder(children[1])
printThis()
if children.length > 0:
  inOrder(children[0])

Copy link
Author

Choose a reason for hiding this comment

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

I agree with you here but I prefer the switch just for readability. It can definitely be made shorter like that though.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Liikt Yup! Which is why I still approved. Both of these were ideas, not must-fixes.

Copy link
Member

@leios leios left a comment

Choose a reason for hiding this comment

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

If @nic-hartley approves, so do I. Thanks a bunch for the review!

@leios leios merged commit fc53db1 into algorithm-archivists:master Oct 5, 2018
kenpower pushed a commit to kenpower/algorithm-archive that referenced this pull request Oct 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hacktoberfest The label for all Hacktoberfest related things! Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants