Golang数据结构-树

二叉树

二叉树是n(n>=0)个节点的有限集合,该集合或者为空集(称为空二叉树),或者由一个根节点和两棵互不相交的、分别称为根节点的左子树和右子树的二叉树组成

基本概念
  • 度:节点拥有的子树数称为节点的度
  • 叶子节点|终端节点:度为0的节点称为叶子节点或者终端节点
  • 分支节点|非终端节点:度不为0的节点称为分支节点或者非终端节点,除根节点外,也称为内部节点
  • 树的度:是树内各节点的度的最大值
  • 孩子节点:节点的子树称为该节点的孩子节点
  • 双亲节点:节点的子树的根称为双亲节点
  • 兄弟节点:同一个双亲节点的孩子之间互称为兄弟节点
  • 祖先:节点的祖先是从根到该节点所经分支上的所有节点
  • 子孙:以某节点为根的子树中的任一节点都称为该节点的子孙
  • 节点的层:根为第一层,根的孩子为第二层,若某节点在第i层,则其子树的节点就在第i+1层。
  • 树的深度|高度:树中节点的最大层次称为树的深度或者高度。
  • 有序树:如果将树中节点的各子树看成从左至右是右次序的。不能互换的,则称为树为有序树,反之为无序树
  • 森林:是m棵互不相交的树的集合,对树中的每个节点而言,其子树的集合即为森林。
特点
  • 每个节点最多有两棵子树。所以二叉树中不存在度大于3的节点。
  • 左子树和右子树是有顺序的,次序不能任意颠倒。
  • 即使树中某节点只有一棵子树,也要区分它是左子树还是右子树。
形态
  • 空二叉树
  • 只有一个根节点的二叉树
  • 根节点只有左子树
  • 根节点只有右子树
  • 根节点既有左子树又有右子树
  • 斜树:所有节点都只有左子树的二叉树叫左斜树,只有右子树的称右斜树,统称斜树
  • 满二叉树:在一棵二叉树中,所有分支节点都存在左、右子树,并且所有叶子都在同一层,称为满二叉树
  • 完全二叉树:对一棵具有n个节点的二叉树按层序编号,如果编号为i(1<=i<=n)的节点与同样深度的满二叉树中编号为i的节点在二叉树中位置完全相同,称为完全二叉树。
存储
  • 顺序存储结构:二叉树的顺序存储结构就是用一维数组存储二叉树中的节点。并且节点的存储位置也就是数组的下标要能体现节点之间的逻辑关系。一般只用于完全二叉树
  • 二叉链表:二叉树每个节点最多有两个孩子,所有为它设计一个数据域和两个指针域。
type BinaryTree struct {
    Data        interface{}
    Left, Right *BinaryTree
}
遍历
  • 前序遍历:规则是若二叉树为空,则返回空。否则先访问根节点,然后前序遍历左子树,在前序遍历右子树。(也可理解为每个节点的遍历:根-左-右)
    //递归-前序遍历
func (binaryTree *BinaryTree) PreOrderRecursion() []interface{} {
    result, tmpLeft, tmpRight := make([]interface{}, 0), make([]interface{}, 0), make([]interface{}, 0)
    if binaryTree == nil {
        return result
    }
    result = append(result, binaryTree.Data)
    if binaryTree.Left != nil {
        tmpLeft = binaryTree.Left.PreOrderRecursion()
        result = append(result, tmpLeft...)
    }
    if binaryTree.Right != nil {
        tmpRight = binaryTree.Right.PreOrderRecursion()
        result = append(result, tmpRight...)
    }
    return result
}

//非递归-前序遍历(根->左->右)
func (binaryTree *BinaryTree) PreOrder() []interface{} {
    btSlice := make([]*BinaryTree, 0)
    result := make([]interface{}, 0)
    if binaryTree == nil {
        return result
    }
    btSlice = append(btSlice, binaryTree)
    for len(btSlice) > 0 {
        curTreeNode := btSlice[len(btSlice)-1]
        btSlice = btSlice[:len(btSlice)-1]
        result = append(result, curTreeNode.Data)
        if curTreeNode.Right != nil {
            btSlice = append(btSlice, curTreeNode.Right)
        }
        if curTreeNode.Left != nil {
            btSlice = append(btSlice, curTreeNode.Left)
        }
    }
    return result
}
//返回结果:[1 2 4 5 3 6 7]
  • 中序遍历:规则是若树为空,则返回空,否则从根节点开始(并不是先访问根节点),中序遍历根节点的左子树,然后访问根节点,最后中序遍历右子树。(也可理解为每个节点的遍历:左-根-右)
//递归-中序遍历
func (binaryTree *BinaryTree) MiddleOrderRecursion() []interface{} {
    result, tmpLeft, tmpRight := make([]interface{}, 0), make([]interface{}, 0), make([]interface{}, 0)
    if binaryTree == nil {
        return result
    }
    if binaryTree.Left != nil {
        tmpLeft = binaryTree.Left.MiddleOrderRecursion()
        result = append(result, tmpLeft...)
    }
    result = append(result, binaryTree.Data)
    if binaryTree.Right != nil {
        tmpRight = binaryTree.Right.MiddleOrderRecursion()
        result = append(result, tmpRight...)
    }
    return result
}

//非递归-中序遍历(左-根-右)
func (binaryTree *BinaryTree) MiddleOrder() []interface{} {
    btSlice := make([]*BinaryTree, 0)
    result := make([]interface{}, 0)
    if binaryTree == nil {
        return result
    }
    curTreeNode := binaryTree
    for len(btSlice) > 0 || curTreeNode != nil {
        if curTreeNode != nil {
            btSlice = append(btSlice, curTreeNode)
            curTreeNode = curTreeNode.Left
        } else {
            tmp := btSlice[len(btSlice)-1]
            result = append(result, tmp.Data)
            btSlice = btSlice[:len(btSlice)-1]
            curTreeNode = tmp.Right
        }
    }
    return result
}
//返回结果:[4 2 5 1 6 3 7]
  • 后序遍历:规则是若树为空,则返回空,否则从左到右先叶子后节点的方式遍历访问左右子树,最后访问根节点。(也可理解每个节点的遍历:左-右-根)
//递归-后序遍历:
func (binaryTree *BinaryTree) AfterOrderRecursion() []interface{} {
    result, tmpLeft, tmpRight := make([]interface{}, 0), make([]interface{}, 0), make([]interface{}, 0)
    if binaryTree == nil {
        return result
    }
    if binaryTree.Left != nil {
        tmpLeft = binaryTree.Left.AfterOrderRecursion()
        result = append(result, tmpLeft...)
    }
    if binaryTree.Right != nil {
        tmpRight = binaryTree.Right.AfterOrderRecursion()
        result = append(result, tmpRight...)
    }
    result = append(result, binaryTree.Data)
    return result
}

//非递归-后序遍历
func (binaryTree *BinaryTree) AfterOrder() []interface{} {
    btSlice := make([]*BinaryTree, 0)
    tmpSlice := make([]*BinaryTree, 0)
    result := make([]interface{}, 0)

    if binaryTree == nil {
        return result
    }
    btSlice = append(btSlice, binaryTree)
    for len(btSlice) > 0 {
        curTreeNode := btSlice[len(btSlice)-1]
        btSlice = btSlice[:len(btSlice)-1]
        tmpSlice = append(tmpSlice, curTreeNode)
        if curTreeNode.Left != nil {
            btSlice = append(btSlice, curTreeNode.Left)
        }
        if curTreeNode.Right != nil {
            btSlice = append(btSlice, curTreeNode.Right)
        }
    }
    for len(tmpSlice) > 0 {
        curTmpSlice := tmpSlice[len(tmpSlice)-1]
        tmpSlice = tmpSlice[:len(tmpSlice)-1]
        result = append(result, curTmpSlice.Data)
    }
    return result
}

//返回结果:[4 5 2 6 7 3 1]
  • 层序遍历:规则是若树为空,则返回空,否则从树的第一层,也就是根节点开始访问,从上而下逐层遍历,在同一层中,按从左到右的顺序对节点逐个访问。
func (binaryTree *BinaryTree) LayersOrder() [][]interface{} {
    result := make([][]interface{}, 0)
    btSlice := make([]*BinaryTree, 0)
    if binaryTree == nil {
        return result
    }
    btSlice = append(btSlice, binaryTree)
    for len(btSlice) > 0 {
        currLevel := make([]interface{}, 0)
        allTreeNode := btSlice[:]
        btSlice = []*BinaryTree{}
        for _, node := range allTreeNode {
            currLevel = append(currLevel, node.Data)
            if node.Left != nil {
                btSlice = append(btSlice, node.Left)
            }
            if node.Right != nil {
                btSlice = append(btSlice, node.Right)
            }
        }
        result = append(result, currLevel)
    }
    return result;
}
//返回结果:[[1] [2 3] [4 5 6 7]]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,188评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,464评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,562评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,893评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,917评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,708评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,430评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,342评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,801评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,976评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,115评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,804评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,458评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,008评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,135评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,365评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,055评论 2 355