zoukankan      html  css  js  c++  java
  • 随笔练习:二叉树 --- golang

    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)
    
    }
  • 相关阅读:
    POJ 1811 Prime Test 素性测试 分解素因子
    sysbench的安装与使用
    电脑中已有VS2005和VS2010安装.NET3.5失败的解决方案
    I.MX6 show battery states in commandLine
    RPi 2B Raspbian system install
    I.MX6 bq27441 driver porting
    I.MX6 隐藏电池图标
    I.MX6 Power off register hacking
    I.MX6 Goodix GT9xx touchscreen driver porting
    busybox filesystem httpd php-5.5.31 sqlite3 webserver
  • 原文地址:https://www.cnblogs.com/zengxm/p/13125759.html
Copyright © 2011-2022 走看看