关于二叉树的代码实现,这里主要介绍的是完全二叉树的情形。
引用百度百科上对完全二叉树的判定:若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。
代码结构如下


tree文件的代码如下:

package tree

import "fmt"

type Object interface{}

type TreeNode struct {
    Data       Object
    LeftChild  *TreeNode
    RightChild *TreeNode
}

//(完全)二叉树结构
type Tree struct {
    RootNode *TreeNode
}

//追加元素 (广度优先,即按层级遍历后添加)
func (this *Tree) Add(object Object) {
    node := &TreeNode{Data: object}
    if this.RootNode == nil {
        this.RootNode = node
        return
    }
    queue := []*TreeNode{this.RootNode}
    for len(queue) != 0 {
        cur_node := queue[0]
        queue = queue[1:]

        if cur_node.LeftChild == nil {
            cur_node.LeftChild = node
            return
        } else {
            queue = append(queue, cur_node.LeftChild)
        }
        if cur_node.RightChild == nil {
            cur_node.RightChild = node
            return
        } else {
            queue = append(queue, cur_node.RightChild)
        }
    }
}

//广度遍历
func (this *Tree) BreadthTravel() {

    if this.RootNode == nil {
        return
    }
    queue := []*TreeNode{}
    queue = append(queue, this.RootNode)

    for len(queue) != 0 {
        //fmt.Printf("len(queue):%d\n", len(queue))
        cur_node := queue[0]
        queue = queue[1:]

        fmt.Printf("%v  ", cur_node.Data)

        if cur_node.LeftChild != nil {
            queue = append(queue, cur_node.LeftChild)
        }
        if cur_node.RightChild != nil {
            queue = append(queue, cur_node.RightChild)
        }
    }

}

/*
深度遍历:
1.先序遍历:根->左->右
2.中序遍历:左->中->右
3.后序遍历:左->右->根
*/

//先序遍历
func (this *Tree) PreOrder(node *TreeNode) {
    if node == nil {
        return
    }

    fmt.Printf("%v  ", node.Data)

    //if node.LeftChild != nil {
    this.PreOrder(node.LeftChild)
    //}
    //if node.RightChild != nil {
    this.PreOrder(node.RightChild)
    //}
}


//中序遍历
func (this *Tree) InOrder(node *TreeNode) {
    if node == nil {
        return
    }
    this.InOrder(node.LeftChild)
    fmt.Printf("%v  ", node.Data)
    this.InOrder(node.RightChild)
}

func (this *Tree) PostOrder(node *TreeNode)  {
    if node == nil {
        return
    }
    this.PostOrder(node.LeftChild)
    this.PostOrder(node.RightChild)
    fmt.Printf("%v  ", node.Data)
}

其中相关main函数的测试代码如下:

package main

import (
    "fmt"
    "algorithm/queue"
    "algorithm/tree"
)

func main() {
    tree := tree.Tree{}
    tree.Add(0)
    tree.Add(1)
    tree.Add(2)
    tree.Add(3)
    tree.Add(4)
    tree.Add(5)
    tree.Add(6)
    tree.Add(7)
    tree.Add(8)
    tree.Add(9)

    //广度优先遍历
    //tree.BreadthTravel()
    //fmt.Println("")

    //深度优先 先序遍历
    tree.PreOrder(tree.RootNode)
    fmt.Println("")

    //深度优先  中序遍历
    tree.InOrder(tree.RootNode)
    fmt.Println("")

    //深度优先  后序遍历
    tree.PostOrder(tree.RootNode)
    fmt.Println("")
}