package main

import (
"fmt"
"reflect"
) type BinaryNode struct {
Data interface{} //数据
lChild *BinaryNode //左子树
rChild *BinaryNode //右子树
} //创建二叉树
func (node *BinaryNode) Create() {
node = new(BinaryNode)
} //先序遍历
func (node *BinaryNode) PreOrder() {
if node == nil {
return
}
//DLR — 先序遍历,即先根再左再右
fmt.Println(node.Data) //递归遍历左子树
node.lChild.PreOrder()
//递归遍历右子树
node.rChild.PreOrder() } //中序遍历
func (node *BinaryNode) MidOrder() {
if node == nil {
return
} //LDR — 中序遍历,即先左再根再右
//递归遍历左子树
node.lChild.MidOrder()
//打印数据
fmt.Println(node.Data)
//递归遍历右子树
node.rChild.MidOrder() } //后序遍历
func (node *BinaryNode) RearOrder() {
if node == nil {
return
} //LRD — 后序遍历,即先左再右再根
//递归遍历左子树
node.lChild.RearOrder()
//递归遍历右子树
node.rChild.RearOrder()
//打印数据
fmt.Println(node.Data)
} //二叉树高度 深度
func (node *BinaryNode) TreeHeight() int {
if node == nil {
return 0
}
//进入下一层遍历
lh := node.lChild.TreeHeight()
rh := node.rChild.TreeHeight()
if lh > rh {
lh++
return lh
} else {
rh++
return rh
} } //二叉树叶子节点个数
//叶子节点是没有后继的节点
func (node *BinaryNode) LeafCount(num *int) {
if node == nil {
return
}
//判断没有后继的节点为叶子节点
if node.lChild == nil && node.rChild == nil {
(*num)++
}
node.lChild.LeafCount(num)
node.rChild.LeafCount(num)
} //二叉树数据查找
func (node *BinaryNode) Search(Data interface{}) {
if node == nil {
return
} //== 不支持slice 和 map
//reflect.DeepEqual()
if reflect.TypeOf(node.Data) == reflect.TypeOf(Data) && node.Data == Data {
fmt.Println("找到数据", node.Data)
return
}
node.lChild.Search(Data)
node.rChild.Search(Data)
} //二叉树销毁
func (node *BinaryNode) Destroy() {
if node == nil {
return
} node.lChild.Destroy()
node.lChild = nil
node.rChild.Destroy()
node.rChild = nil
node.Data = nil } //二叉树反转
//如果想反转二叉树 二叉树必须是一个满二叉树
func (node *BinaryNode) Reverse() {
if node == nil {
return
} //交换节点 多重赋值
node.lChild, node.rChild = node.rChild, node.lChild node.lChild.Reverse()
node.rChild.Reverse() } //二叉树拷贝
func (node *BinaryNode) Copy() *BinaryNode {
if node == nil {
return nil
}
lChild:=node.lChild.Copy()
rChild:=node.rChild.Copy() //创建写节点 拷贝数据
newNode:=new(BinaryNode)
newNode.Data=node.Data
newNode.lChild=lChild
newNode.rChild=rChild
return newNode
}

go语言 二叉树的更多相关文章

  1. Hello world!(内含自己编写的C语言二叉树同学录)

      修改:刷了一段时间的题,水平渐涨,发现同学录真的要做成市面可行的应用的话,应该按学号建立二叉平衡树,红黑树是一个可行的选择. 在同学的推荐下,来到博客园来找志同道合的人交流代码.3个月后参加蓝桥杯 ...

  2. c语言二叉树基本操作

    编译器为vs2013 #include "stdafx.h" #include<malloc.h> #include<stdlib.h> #define O ...

  3. C语言二叉树的建立与遍历

    二叉树的建立和遍历都要用到递归,先暂时保存一下代码,其中主要是理解递归的思想,其它的就都好理解了.这里是三种遍历方式,其实理解一种,其它的几个就都理解了,就是打印出来的顺序不一样而已.建立和遍历的方式 ...

  4. [数据结构]C语言二叉树的实现

    树和图是数据结构中比较麻烦的东西,里面涉及的概念比较多,也最有用, 就比如一般树广泛应用于人工智能的博弈上,而基于图的广度优先和深度优先搜索也广泛应用于人工智能寻路上面 首先我们要把树进行分类: &g ...

  5. Go语言二叉树定义及遍历算法实现

    // binary_tree 二叉树 package Algorithm import ( "reflect" ) // 二叉树定义 type BinaryTree struct ...

  6. C语言二叉树的创建、(先中后序)遍历以及存在的问题

    #include<stdlib.h> #include<stdio.h> #define True 1 #define False 0 typedef char TElemTy ...

  7. C语言二叉树遍历及路径查找

    #include<iostream> #include<stdio.h> #include<math.h> #include<malloc.h> usi ...

  8. c语言二叉树

    Department of Computing and Information SystemsCOMP10002 Foundations of AlgorithmsSemester 2, 2014As ...

  9. c语言二叉树的递归建立

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <malloc.h&g ...

随机推荐

  1. 深入浅出Mybatis系列一-Mybatis入门

    注:本文转载自南轲梦 注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 最近两年 springmvc + mybatis 的在这种搭配还是蛮火的,楼主我呢, ...

  2. Spark Streaming数据限流简述

      Spark Streaming对实时数据流进行分析处理,源源不断的从数据源接收数据切割成一个个时间间隔进行处理:   流处理与批处理有明显区别,批处理中的数据有明显的边界.数据规模已知:而流处理数 ...

  3. mysql之数据初始化update操作

    1.单表的:update user set name = (select name from user where id in (select id from user where name='小苏' ...

  4. H3C接口管理配置

    一.接口批量配置 当多个接口需要配置某功能(比如shutdown)时,需要逐个进入接口视图,在每个接口执行一遍命令,比较繁琐.此时,可以使用接口批量配置功能,对接口进行批量配置,节省配置工作量. 1. ...

  5. python package install error and little code bugs

    When you install packages using setup.py, the error: (py37) C:\Users\weda\Phd\python packages\visibi ...

  6. redis五大数据类型以及常用操作命令

    Redis的五大数据类型 String(字符串) string是redis最基本的类型,你可以理解成与Memcached一模一样的类型,一个key对应一个value.string类型是二进制安全的.意 ...

  7. 1级迁移类Q101-Oracle ASM 迁移文件系统(File System)非公

    项目文档引子系列是根据项目原型,制作的测试实验文档,目的是为了提升项目过程中的实际动手能力,打造精品文档AskScuti. 项目文档引子系列目前不对外发布,仅作为博客记录.如学员在实际工作过程中需提前 ...

  8. Tomcat的使⽤

    准备 1.官⽹地址:http://tomcat.apache.org下载. 2.解压文件,并放到指定路径,给该文件授权. chmod -R 755 3.启动和停止 进入到/Users/lucas/Do ...

  9. SIFT解析(一)高斯模糊

    "模糊"的算法有很多种,其中有一种叫做"高斯模糊"(Gaussian Blur).它将正态分布(又名"高斯分布")用于图像处理. 所谓&qu ...

  10. TODO:如何模拟cpu打满,磁盘打满,网卡打满

    背景: 测试活动中,需要构造cpu打满.磁盘打满.网卡打满的场景 场景1:cpu打满 环境信息: 虚拟机,物理核数16个,每个物理核的虚拟核数1个,虚拟核数16个: [root@vm10-0-0-8 ...