Golang数据结构-树

2020-05-25  本文已影响0人  31627a633847

二叉树

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

基本概念
特点
形态
存储
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]]
上一篇 下一篇

猜你喜欢

热点阅读