-
-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathtreetraversal.go
90 lines (76 loc) · 1.57 KB
/
treetraversal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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")
}
}
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)
}