golang简单实现二叉树的数据添加和遍历
代码实现
package tree
import "fmt"
type Node struct {
elem interface{}
left, right *Node
}
type Tree struct {
root *Node
}
func NewTree() *Tree {
return &Tree{}
}
// 添加元素
func (this *Tree) Add(v interface{}) {
node := &Node{elem: v}
if this.root == nil {
this.root = node
return
}
c := make(chan *Node, 10)
c <- this.root
for len(c) > 0 {
curNode := <-c
if curNode.left == nil {
curNode.left = node
return
} else {
c <- curNode.left
}
if curNode.right == nil {
curNode.right = node
return
} else {
c <- curNode.right
}
}
}
// 广度优先遍历
func (this *Tree) Travel() {
if this.root == nil {
fmt.Println("empty tree")
return
}
c := make(chan *Node, 10)
c <- this.root
for len(c) > 0 {
curNode := <-c
fmt.Println(curNode.elem)
if curNode.left != nil {
c <- curNode.left
}
if curNode.right != nil {
c <- curNode.right
}
}
}
// 先序遍历
func (this *Tree) Xianxu() {
xz(this.root)
}
// 中序遍历
func (this *Tree) ZhongXu() {
zx(this.root)
}
// 后序遍历
func (this *Tree) HouXu() {
hx(this.root)
}
func xz(node *Node) {
if node == nil {
return
}
fmt.Println(node.elem)
xz(node.left)
xz(node.right)
}
func zx(node *Node) {
if node == nil {
return
}
zx(node.left)
fmt.Println(node.elem)
zx(node.right)
}
func hx(node *Node) {
if node == nil {
return
}
hx(node.left)
hx(node.right)
fmt.Println(node.elem)
}
测试
package tree
import "testing"
var tree *Tree
func prepare() {
tree = NewTree()
tree.Add("mark")
tree.Add("jack")
tree.Add("timo")
tree.Add("marry")
tree.Add("toshiyuki")
tree.Add("naruto")
tree.Add("sakura")
}
func TestTree_Travel(t *testing.T) {
prepare()
tree.Travel()
}
func TestTree_Xianxu(t *testing.T) {
prepare()
tree.Xianxu()
}
func TestTree_ZhongXu(t *testing.T) {
prepare()
tree.ZhongXu()
}
func TestTree_HouXu(t *testing.T) {
prepare()
tree.HouXu()
}
测试结果
=== RUN TestTree_Travel
mark
jack
timo
marry
toshiyuki
naruto
sakura
--- PASS: TestTree_Travel (0.00s)
=== RUN TestTree_Xianxu
mark
jack
marry
toshiyuki
timo
naruto
sakura
--- PASS: TestTree_Xianxu (0.00s)
=== RUN TestTree_ZhongXu
marry
jack
toshiyuki
mark
naruto
timo
sakura
--- PASS: TestTree_ZhongXu (0.00s)
=== RUN TestTree_HouXu
marry
toshiyuki
jack
naruto
sakura
timo
mark
--- PASS: TestTree_HouXu (0.00s)
PASS
golang简单实现二叉树的数据添加和遍历的更多相关文章
- Golang+chromedp+goquery 简单爬取动态数据
目录 Golang+chromedp+goquery 简单爬取动态数据 Golang的安装 下载golang软件 解压golang 配置golang 重新导入配置 chromedp框架的使用 实际的代 ...
- Python学习入门基础教程(learning Python)--8.1 字典数据添加与删除
1. 字典数据添加 这个很简单,像赋值那样一项项赋值即可.语法结构如下 dict_obj[key] = value 添加数据项示例如下 >>> d1 = {'cod ...
- .NET 利用反射将对象数据添加到数据库
.NET 利用反射将对象数据添加到数据库 一些小型的项目,在不使用其他的框架(LINQ,NHibernate,EF等等框架)的前提下,这时候一些反复的增删改查就会让我们感到极其的繁琐,厌烦,为了避 ...
- 使用JMeter进行一次简单的带json数据的post请求测试
使用JMeter进行一次简单的带json数据的post请求测试 原文:https://www.cnblogs.com/summer-mm/p/7717812.html 1.启动jmeter:在bin下 ...
- postman上传excel,java后台读取excel生成到指定位置进行备份,并且把excel中的数据添加到数据库
最近要做个前端网页上传excel,数据直接添加到数据库的功能..在此写个读取excel的demo. 首先新建springboot的web项目 导包,读取excel可以用poi也可以用jxl,这里本文用 ...
- c++简单实现二叉树
专业术语: 节点 父节点 根节点 子孙 堂兄弟 深度: 从根节点到最底层节点的层数称为深度 叶子节点: 没有子节点的节点称为叶子节点 非终端节点: 实际就是非叶子节点 度: 子节点的个数称为度 树的分 ...
- Angular02 将数据添加到组件中
准备:已经搭建好angular-cli环境.知道如何创建组件 一.将一个数据添加到组件中 1 创建一个新的组件 user-item 2 将组件添加到静态模板中 3 为组件添加属性,并利用构造器赋值 4 ...
- C# 将Access中时间段条件查询的数据添加到ListView中
C# 将Access中时间段条件查询的数据添加到ListView中 一.让ListView控件显示表头的方法 在窗体中添加ListView 空间,其属性中设置:View属性设置为:Detail,Col ...
- 6月16 ThinkPHP连接数据库及Model数据模型层--------查询及数据添加
连接数据库配置及Model数据模型层 convertion.php config.php 1.在config.php做数据库连接配置 2.修改配置 /* 数据库设置 */ 'DB_TYPE' => ...
随机推荐
- PytorchZerotoAll学习笔记(一)
Pytorch的安装请参考torch的官方文档,传送门:https://pytorch.org/get-started/locally/ Numpy的复习 如果你之前没有学过Numpy的话,建议去看看 ...
- string类型和int类型之间的转换
一.string转int 1. 使用string流 /* 字符串转整型 */ /* * istringstream:从 string 读取数据 * ostringstream:向 string 写入数 ...
- 在js中保存数据
localStorage: localStorage.setItem("key", "value"); localStorage.getItem("k ...
- 软工实践-Beta 冲刺 (7/7)
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 1.界面的修改与完善 展示GitHub当日代码/文档签入记 ...
- Beta Scrum Day 4 — 听说
听说
- POJ 1185 炮兵阵地 状压dp
题目链接: http://poj.org/problem?id=1185 炮兵阵地 Time Limit: 2000MS Memory Limit: 65536K 问题描述 司令部的将军们打算在N*M ...
- Ubuntu下tensorboard的使用
1. 找到运行程序的事件输出路径 找到路径并进入,例如我的是在路径/home/ly/codes下: 2. 打开tensorboard服务器 在终端输入(--logdir=自己所存的路径): t ...
- 代码上传不到github远程仓库的经历和总结
第二次的作业是分布式版本控制系统Git的安装与使用.一切都好端端地进行,知道最后的上传到给远程仓库时一直都上传失败.舍友也过来调试和助教的指导,依然不成功.我也上网进行了大量的翻查资料也未能成功.这是 ...
- 数据结构复习笔记(ADT栈/LIFO表)
栈是一种特殊的表,只在表首进行插入和删除操作,表首称之为栈顶,表尾称为栈底:栈的核心原则是先进后出,简称Last In First Out(LIFO表):常用的运算有:1.是否为空栈判断:2.栈是否满 ...
- js 代码几种方式
var nameSpace={ //public } (function(){ //private })(); var module=(function(){ //private return { / ...