前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Golang之Struct(二叉树定义)

Golang之Struct(二叉树定义)

作者头像
超蛋lhy
发布2018-08-31 15:50:00
4140
发布2018-08-31 15:50:00
举报
文章被收录于专栏:PythonistaPythonista

接招吧,看代码:

代码语言:javascript
复制
package main

import "fmt"

//二叉树结构体
//如果每个节点有两个指针,分别用来指向左子树和右子树,我们把这样的结构叫做二叉树
type Student struct {
    Name  string
    Age   int
    Score float32
    left  *Student
    right *Student
}

func trans(root *Student) {
    if root == nil {
        return
    }
    //fmt.Println(root) //前序遍历
    trans(root.left)
    //fmt.Println(root) //中序遍历
    trans(root.right)
    fmt.Println(root) //后序遍历
}

func main() {
    var root *Student = new(Student)

    root.Name = "stu01"
    root.Age = 18
    root.Score = 100

    var left1 *Student = new(Student)
    left1.Name = "stu02"
    left1.Age = 18
    left1.Score = 100

    root.left = left1

    var right1 *Student = new(Student)
    right1.Name = "Stu04"
    right1.Age = 18
    right1.Score = 100

    root.right = right1

    var left2 *Student = new(Student)
    left2.Name = "Stu03"
    left2.Age = 18
    left2.Score = 100

    left1.left = left2
    trans(root)
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-01-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档