随笔练习:二叉树 --- golang

free0day 2020-06-14

type node struct {
    data int
    lchild *node
    rchild *node
}

func newNode(data int) *node {
    return &node{data: data}
}

type binaryTree struct {
    root *node
}

func (o *binaryTree)append(data int)  {
    tmp := newNode(data)
    if o.root == nil {
        o.root = tmp
    }else {
        queue := make([]*node,0)
        queue = append(queue,o.root)
        for len(queue) > 0 {
            cur := queue[0]
            if cur.lchild == nil {
                cur.lchild = tmp
                break
            }
            if cur.rchild == nil {
                cur.rchild = tmp
                break
            }
            queue = append(queue,cur.lchild)
            queue = append(queue,cur.rchild)
            queue = queue[1:]
        }
    }
}

// 前 中 后便利
func (o *binaryTree)preorder(root *node){
    if root == nil{
        return
    }
    fmt.Println(root.data)
    o.preorder(root.lchild)
    o.preorder(root.rchild)
}
func (o *binaryTree)inorder(root *node){
    if root == nil{
        return
    }
    o.preorder(root.lchild)
    fmt.Println(root.data)
    o.preorder(root.rchild)
}
func (o *binaryTree)postorder(root *node){
    if root == nil{
        return
    }
    o.preorder(root.lchild)
    o.preorder(root.rchild)
    fmt.Println(root.data)
}

// 深度遍历
func (o *binaryTree)BFS(root *node) {
    if root == nil{
        return
    }

    queue := make([]*node,0)
    queue = append(queue,root)
    for len(queue) > 0{
        cur := queue[0]
        fmt.Println(cur.data)
        queue = queue[1:]

        if cur.lchild != nil{
            queue = append(queue, cur.lchild)
        }
        if cur.rchild != nil {
            queue = append(queue, cur.rchild)
        }
    }
}

// 二叉树节点个数
func (o *binaryTree)GetNodeNum(root *node) int{
    if root == nil{
        return 0
    }else {
        return o.GetNodeNum(root.lchild) + o.GetNodeNum(root.rchild) +1
    }
}

// 二叉树深度
func (o *binaryTree)GetDegree(root *node) int {
    if root == nil{
        return 0
    }

    var max int
    if o.GetDegree(root.lchild) > o.GetDegree(root.rchild){
        max = o.GetDegree(root.lchild)
    }else {
        max = o.GetDegree(root.rchild)
    }
    return max + 1
}

// k 层节点个数
func (o *binaryTree)GetKthNum(root *node,k int) int {
    if root == nil {
        return 0
    }
    if k == 1{
        return 1
    }
    return o.GetKthNum(root.lchild,k-1) + o.GetKthNum(root.rchild,k-1)
}

// 叶子节点个数
func (o *binaryTree)GetLeavNum(root *node) int {
    if root == nil{
        return 0
    }
    if root.lchild == nil && root.rchild == nil{
        return 1
    }
    return o.GetLeavNum(root.lchild) + o.GetLeavNum(root.rchild)
}

// 判断平衡二叉树
func (o *binaryTree)isBalanced(root *node) bool{
    if root == nil{
        return true
    }
    lde := o.GetDegree(root.lchild)
    rde := o.GetDegree(root.rchild)
    flag := false
    if (math.Abs(float64(lde-rde)))<=1{
        flag = true
    }else {
        flag = false
    }
    return flag && o.isBalanced(root.lchild) && o.isBalanced(root.rchild)

}

相关推荐