GO-----二叉树结构

本文详细介绍了二叉树的前序、中序和后序遍历方法,通过Go语言实现,并给出了具体示例。前序遍历顺序为根-左-右,中序遍历顺序为左-根-右,后序遍历顺序为左-右-根。通过这些遍历方式,可以有效地访问和操作二叉树结构中的所有节点。
摘要由CSDN通过智能技术生成

目录

1,前序遍历

2,中序遍历

3,后序遍历


满足以下两个条件的树就是叉树:

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}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值