-
-
Notifications
You must be signed in to change notification settings - Fork 359
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
Changes from all commits
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,90 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type node struct { | ||
id int | ||
children []*node | ||
} | ||
|
||
func dfsRecursive(n *node) { | ||
fmt.Println(n.id) | ||
for _, child := range n.children { | ||
dfsRecursive(child) | ||
} | ||
} | ||
|
||
func dfsRecursivePostorder(n *node) { | ||
for _, child := range n.children { | ||
dfsRecursive(child) | ||
} | ||
fmt.Println(n.id) | ||
} | ||
|
||
func dfsRecursiveInorderBtree(n *node) { | ||
switch len(n.children) { | ||
case 2: | ||
dfsRecursiveInorderBtree(n.children[0]) | ||
fmt.Println(n.id) | ||
dfsRecursiveInorderBtree(n.children[1]) | ||
case 1: | ||
dfsRecursiveInorderBtree(n.children[0]) | ||
fmt.Println(n.id) | ||
case 0: | ||
fmt.Println(n.id) | ||
default: | ||
fmt.Println("This is not a binary tree") | ||
} | ||
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. 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:
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 agree with you here but I prefer the switch just for readability. It can definitely be made shorter like that though. 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. @Liikt Yup! Which is why I still approved. Both of these were ideas, not must-fixes. |
||
} | ||
|
||
func dfsStack(n *node) { | ||
stack := []*node{n} | ||
|
||
for len(stack) > 0 { | ||
cur := stack[0] | ||
stack = stack[1:] | ||
fmt.Println(cur.id) | ||
stack = append(cur.children, stack...) | ||
} | ||
} | ||
|
||
func bfsQueue(n *node) { | ||
queue := []*node{n} | ||
|
||
for len(queue) > 0 { | ||
cur := queue[0] | ||
queue = queue[1:] | ||
fmt.Println(cur.id) | ||
queue = append(queue, cur.children...) | ||
} | ||
} | ||
|
||
func createTree(numRow, numChild int) *node { | ||
if numRow == 0 { | ||
return &node{id: 0} | ||
} | ||
|
||
cur := new(node) | ||
cur.id = numRow | ||
|
||
for x := 0; x < numChild; x++ { | ||
cur.children = append(cur.children, createTree(numRow-1, numChild)) | ||
} | ||
return cur | ||
} | ||
|
||
func main() { | ||
root := createTree(3, 3) | ||
binTree := createTree(3, 2) | ||
|
||
fmt.Println("DFS recursive:") | ||
dfsRecursive(root) | ||
fmt.Println("DFS post order recursive:") | ||
dfsRecursivePostorder(root) | ||
fmt.Println("DFS inorder binary tree:") | ||
dfsRecursiveInorderBtree(binTree) | ||
fmt.Println("DFS stack:") | ||
dfsStack(root) | ||
fmt.Println("BFS queue:") | ||
bfsQueue(root) | ||
} |
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.
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.
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.
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.
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.
@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.