目录
满足以下两个条件的树就是叉树:
1.本身是有序树;
2.树中包含的各个节点的度不能超过2,即只能是0、1或者2;
示意图:
1,前序遍历
示例:前序遍历二叉树(根左右)
package main
import "fmt"
type student struct {
name string
age int
score float32
left *student
right *student
}
func main() {
//定义二叉树
//定义根
var root student
root.name = "root"
root.age = 23
root.score = 56
//定义一级左子树
var left1 student
left1.name = "left1"
left1.age = 28
left1.score = 88
root.left = &left1
//定义一级右子树
var right1 student
right1.name = "right"
right1.age = 25
right1.score = 60
root.right = &right1
//定义二级左子树
var left2 student
left2.name = "left2"
left2.age = 29
left2.score = 70
left1.left = &left2
Req(&root)
}
//遍历二叉树
func Req(tmp *student) {
if tmp != nil {
fmt.Println(*tmp) //输出根
Req(tmp.left) //递归的方法输出左分支
Req(tmp.right) //递归的方法输出右分支
}
}
[Running] go run "f:\goProject\src\dev_code\day19\example1\main\main.go"
{root 23 56 0xc0000c2450 0xc0000c2480}
{left1 28 88 0xc0000c24b0 <nil>}
{left2 29 70 <nil> <nil>}
{right 25 60 <nil> <nil>}
2,中序遍历
示例:中序遍历二叉树(左跟右)
package main
import "fmt"
type student struct {
name string
age int
score float32
left *student
right *student
}
func main() {
//定义二叉树
//定义根
var root student
root.name = "root"
root.age = 23
root.score = 56
//定义一级左子树
var left1 student
left1.name = "left1"
left1.age = 28
left1.score = 88
root.left = &left1
//定义一级右子树
var right1 student
right1.name = "right"
right1.age = 25
right1.score = 60
root.right = &right1
//定义二级左子树
var left2 student
left2.name = "left2"
left2.age = 29
left2.score = 70
left1.left = &left2
Req(&root)
}
//遍历二叉树
func Req(tmp *student) {
if tmp != nil {
Req(tmp.left) //递归的方法输出左分支
fmt.Println(*tmp) //输出根
Req(tmp.right) //递归的方法输出右分支
}
}
[Running] go run "f:\goProject\src\dev_code\day19\example1\main\main.go"
{left2 29 70 <nil> <nil>}
{left1 28 88 0xc0001104e0 <nil>}
{root 23 56 0xc000110480 0xc0001104b0}
{right 25 60 <nil> <nil>}
3,后序遍历
示例:中序遍历二叉树(左右根)
子树遍历是自左向右
package main
import "fmt"
type student struct {
name string
age int
score float32
left *student
right *student
}
func main() {
//定义二叉树
//定义根
var root student
root.name = "root"
root.age = 23
root.score = 56
//定义一级左子树
var left1 student
left1.name = "left1"
left1.age = 28
left1.score = 88
root.left = &left1
//定义一级右子树
var right1 student
right1.name = "right"
right1.age = 25
right1.score = 60
root.right = &right1
//定义二级左子树
var left2 student
left2.name = "left2"
left2.age = 29
left2.score = 70
left1.left = &left2
Req(&root)
}
//遍历二叉树
func Req(tmp *student) {
if tmp != nil {
Req(tmp.left) //递归的方法输出左分支
Req(tmp.right) //递归的方法输出右分支
fmt.Println(*tmp) //输出根
}
}
[Running] go run "f:\goProject\src\dev_code\day19\example1\main\main.go"
{left2 29 70 <nil> <nil>}
{left1 28 88 0xc0001104e0 <nil>}
{right 25 60 <nil> <nil>}
{root 23 56 0xc000110480 0xc0001104b0}
package main
import "fmt"
type student struct {
name string
age int
score float32
left *student
right *student
}
func main() {
//定义二叉树
//定义根
var root student
root.name = "root"
root.age = 23
root.score = 56
//定义一级左子树
var left1 student
left1.name = "left1"
left1.age = 28
left1.score = 88
root.left = &left1
//定义一级右子树
var right1 student
right1.name = "right"
right1.age = 25
right1.score = 60
root.right = &right1
//定义二级左子树
var left2 student
left2.name = "left2"
left2.age = 29
left2.score = 70
left1.left = &left2
//定义一级左的二级右子树
var lefright student
lefright.name = "lefright"
lefright.age = 100
lefright.score = 10000
left1.right = &lefright
Req(&root)
}
//遍历二叉树
func Req(tmp *student) {
if tmp != nil {
Req(tmp.left) //递归的方法输出左分支
Req(tmp.right) //递归的方法输出右分支
fmt.Println(*tmp) //输出根
}
}
[Running] go run "f:\goProject\src\dev_code\day19\example1\main\main.go"
{left2 29 70 <nil> <nil>}
{lefright 100 10000 <nil> <nil>}
{left1 28 88 0xc0001104e0 0xc000110510}
{right 25 60 <nil> <nil>}
{root 23 56 0xc000110480 0xc0001104b0}