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
type Node struct {
Value int
Left, Right *Node
}

func createNode(value int) *Node {
return &Node{
Value: value,
}
}

func traversing(root *Node) {

if root == nil {
return
}
// 中序遍历
traversing(root.Left)
fmt.Printf("%d\n", root.Value)
traversing(root.Right)

}

func main() {
var root *Node = createNode(3)
root.Left = createNode(1)
root.Right = createNode(5)
root.Left.Left = createNode(0)
root.Left.Right = createNode(2)
root.Right.Left = createNode(4)
root.Right.Right = createNode(6)

traversing(root)
}
------------------
0
1
2
3
4
5
6

上述代码在创建并初始化的二叉树结构如下图
Go语言创建一个简单的二叉树