二叉树

树是n(n >= 0)个结点的有限集合,n = 0时称为空树。在任意一棵非空树中:

  1. 有且仅有一个特定的称为根的结点
  2. n > 1时,其余结点可分为m(m > 0)个互不相交的有限集T1,T2,……Tm,其中每个集合本身又是一棵树,并且称为根的子树

二叉树

二叉树是一种特殊的树,它的特点是每个结点最多有两个子树(即二叉树的度不能大于2),并且二叉树的子树有左右之分,其次序不能颠倒。

性质

  • 深度为k且有2^k-1个结点的二叉树称为满二叉树
  • 二叉树的第i层最多有2^(i-1)个结点
  • 深度为k的二叉树至多有2^k-1个结点
  • 二叉树中叶子节点个数n0,度为2的节点个数n2,则n2=n0-1

操作

package main

import (
    "container/list"
    "fmt"
    "strings"
)

type MyStack struct {
    List *list.List
}

type MyQueue struct {
    List *list.List
}

type BinaryTree struct {
    Value interface{}
    Left *BinaryTree
    Right *BinaryTree
}

type Tree struct {
    Value interface{}
    Children []*Tree
}

func (stack *MyStack) pop() interface{} {
    if elem := stack.List.Back(); elem != nil {
        stack.List.Remove(elem)
        return elem.Value
    }
    return nil
}

func (stack *MyStack) push(elem interface{}) {
    stack.List.PushBack(elem)
}

func (queue *MyQueue) pop() interface{} {
    if elem := queue.List.Front(); elem != nil {
        queue.List.Remove(elem)
        return elem.Value
    }
    return nil
}

func (queue *MyQueue) push(elem interface{}) {
    queue.List.PushBack(elem)
}

func maxDepth(root *BinaryTree) int{
	if root == nil {
		return 0
	}
	l := maxDepth(root.Left) + 1
	r := maxDepth(root.Right) + 1
	if l>r {
		return l
	}
	return r
}

func preOrderRecur(node *BinaryTree) {
    if node == nil {
        return
    }

    fmt.Println(node.Value)
    preOrderRecur(node.Left)
    preOrderRecur(node.Right)
}

func inOrderRecu(node *BinaryTree) {
    if node == nil {
        return
    }

    inOrderRecu(node.Left)
    fmt.Println(node.Value)
    inOrderRecu(node.Right)
}

func posOrderRecu(node *BinaryTree) {
    if node == nil {
        return
    }

    posOrderRecu(node.Left)
    posOrderRecu(node.Right)
    fmt.Println(node.Value)
}

func preOrder(node *BinaryTree) {
    stack := MyStack{List: list.New()}
    stack.push(node)

    elem := stack.pop()
    for elem != nil {
        node, _ := elem.(*BinaryTree)

        fmt.Println(node.Value)
        if right := node.Right; right != nil {
            stack.push(right)
        }
        if left := node.Left; left != nil {
            stack.push(left)
        }

        elem = stack.pop()
    }
}

func inOrder(node *BinaryTree) {
    stack := MyStack{List: list.New()}
    current := node

    for stack.List.Len() > 0 || current != nil {
        if current != nil {
            stack.push(current)
            current = current.Left
        } else {
            current = stack.pop().(*BinaryTree)
            fmt.Println(current.Value)
            current = current.Right
        }
    }
}

func postOrder(node *BinaryTree) {
    stack1, stack2 := MyStack{List: list.New()}, MyStack{List: list.New()}
    stack1.push(node)

    for stack1.List.Len() > 0 {
        elem := stack1.pop().(*BinaryTree)
        stack2.push(elem)

        if elem.Left != nil {
            stack1.push(elem.Left)
        }

        if elem.Right != nil {
            stack1.push(elem.Right)
        }
    }

    for stack2.List.Len() > 0 {
        elem := stack2.pop().(*BinaryTree)
        fmt.Println(elem.Value)
    }
}

func levelOrder(node *BinaryTree) {
    var nlast *BinaryTree
    last := node
    level := 1
    queue := MyQueue{List: list.New()}

    fmt.Println(fmt.Sprintf("-----this is %d level-----", level))
    queue.push(node)
    for queue.List.Len() > 0 {
        node := queue.pop().(*BinaryTree)

        if node.Left != nil {
            queue.push(node.Left)
            nlast = node.Left
        }

        if node.Right != nil {
            queue.push(node.Right)
            nlast = node.Right
        }

        fmt.Println(node.Value)
        if last == node && (node.Left != nil || node.Right != nil) {
            last = nlast
            level++
            fmt.Println()
            fmt.Println(fmt.Sprintf("-----this is %d level-----", level))
        }
    }
}

func levelTreeOrder(node *Tree) {
    var nlast *Tree
    last := node
    queue := MyQueue{List: list.New()}
    queue.push(node)

    for queue.List.Len() > 0 {
        node := queue.pop().(*Tree)

        for _, elem := range node.Children {
            queue.push(elem)
            nlast = elem
        }

        fmt.Println(node.Value)

        if last == node {
            last = nlast
            fmt.Println()
        }

    }
}

func preOrderToStr(node *BinaryTree) (ret string) {
    if node == nil {
        return "#!"
    }

    ret += fmt.Sprintf("%d!", node.Value)
    ret += preOrderToStr(node.Left)
    ret += preOrderToStr(node.Right)
    return ret
}

func strToBinaryTree(arr []string, index *int) *BinaryTree {
    if *index >= len(arr) {
        return nil
    }

    if arr[*index] == "#" {
        *index++
        return nil
    }

    node := &BinaryTree{}
    node.Value = arr[*index]
    *index++

    node.Left = strToBinaryTree(arr, index)
    node.Right = strToBinaryTree(arr, index)
    return node
}
posted @ 2020-03-13 14:43  星灬期灬五  阅读(325)  评论(0编辑  收藏  举报