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. JS获取样式

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. Git常用命令简记

    创建仓库 添加需要版本控制的文件到仓库中 提交到版本库 修改位于顶端的commit的日志 分支管理 版本回退 切换与合并分支 本地版本库与远程关联 克隆 Tag的使用 问题与解决 创建git仓库 gi ...

  3. Educational Codeforces Round 65 (Rated for Div. 2)B. Lost Numbers(交互)

    This is an interactive problem. Remember to flush your output while communicating with the testing p ...

  4. 1304F2 - Animal Observation (hard version) 线段树or单调队列 +DP

    1304F2 - Animal Observation (hard version) 线段树or单调队列 +DP 题意 用摄像机观察动物,有两个摄像机,一个可以放在奇数天,一个可以放在偶数天.摄像机在 ...

  5. vuejs在解析时出现闪烁的原因及防止闪烁的方法

    原因: 在使用vuejs.angularjs开发时,经常会遇见在如Chrome这类能够快速解析的浏览器上出现表达式({{ express }} ),或者是模块(div)的闪烁.对于这个问题由于Java ...

  6. 论文阅读笔记(三)【AAAI2017】:Learning Heterogeneous Dictionary Pair with Feature Projection Matrix for Pedestrian Video Retrieval via Single Query Image

    Introduction (1)IVPR问题: 根据一张图片从视频中识别出行人的方法称为 image to video person re-id(IVPR) 应用: ① 通过嫌犯照片,从视频中识别出嫌 ...

  7. centos7在命令界面使用命令可以执行,但在jenkins中输入命令报Chrome has crashed.

    问题:selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: ex ...

  8. [CF859C] Pie Rules - dp,博弈论

    有一个长度为n的序列,Alice和Bob在玩游戏.Bob先手掌握决策权. 他们从左向右扫整个序列,在任意时刻,拥有决策权的人有如下两个选择: 将当前的数加到自己的得分中,并将决策权给对方,对方将获得下 ...

  9. php Allowed memory size of 134217728 bytes exhausted

    报错:PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 72 bytes) in ...

  10. 全栈高级web前端工程师的必经之路

    这是最近整理的一个进阶高级web工程师的技术栈 同时也是激励自己每天来看一下离真正的王者还有多少距离! 过段时间再来反思一下自己进步了多少? ------------------------20190 ...