用go 實(shí)現(xiàn)一個(gè)簡(jiǎn)單的樹(shù)
最近在拾起數(shù)據(jù)結(jié)構(gòu),于是用go寫(xiě)了個(gè)簡(jiǎn)單的二叉樹(shù)
package data
import (
"fmt"
"reflect"
)
type Binary struct {
Data interface{}
left *Binary
right *Binary
}
// 創(chuàng)建二叉樹(shù) —— 目前打印下來(lái) 全是左節(jié)點(diǎn)
var i = -1
func (node *Binary) Create(data []interface{})*Binary {
if node == nil {
return nil
}
i = i +1
// 創(chuàng)建二叉樹(shù)結(jié)點(diǎn)
var bin *Binary
if i >= len(data) {
return nil
}else {
bin = new(Binary)
bin.Data = data[i]
bin.left = bin.Create(data)
bin.right = bin.Create(data)
}
return bin
}
func (node *Binary)New(index int,data []interface{})*Binary{
if node == nil {
return nil
}
bin := &Binary{data[index],nil,nil}
// 設(shè)置完全二叉樹(shù)左節(jié)點(diǎn) 其特征是深度 *2+1為左節(jié)點(diǎn) +2為右節(jié)點(diǎn)
if index< len(data) && 2*index+1 < len(data){
fmt.Println()
bin.left = bin.New(index*2+1,data)
}
if i< len(data) && 2*index+2 < len(data){
bin.right = bin.New(index*2+2,data)
}
return bin
}
// 遍歷二叉樹(shù) —— 先序遍歷: DLR
func (node *Binary) PrevSort() {
// 遞歸出口
if node == nil {
return
}
// 先D, 先打印數(shù)據(jù)域
fmt.Print(node.Data, " ")
// 再左, 左子樹(shù)遞歸調(diào)用本函數(shù)
node.left.PrevSort()
// 再右,右子樹(shù)遞歸調(diào)用本函數(shù)
node.right.PrevSort()
}
// 遍歷二叉樹(shù) —— 中序遍歷: LDR
func (node *Binary) MidSort() {
if node == nil {
return
}
// 先左,左子樹(shù)遞歸調(diào)用本函數(shù)
node.left.MidSort()
// 再中, 打印數(shù)據(jù)域
fmt.Print(node.Data, " ")
// 再右, 右子樹(shù)遞歸調(diào)用本函數(shù)
node.right.MidSort()
}
// 遍歷二叉樹(shù) —— 后序遍歷: LRD --- 73415620
func (node *Binary) PostSort() {
if node == nil {
return
}
// 先左, 左遞歸
node.left.PostSort()
// 再右, 右遞歸
node.right.PostSort()
// 再中, 打印
fmt.Print(node.Data, " ")
}
// 獲取二叉樹(shù)的高度(深度)
func (node *Binary) Height() int {
// 遞歸出口
if node == nil {
return 0
}
// 左子樹(shù)遞歸進(jìn)入
lh := node.left.Height()
// 右子樹(shù)遞歸進(jìn)入
rh := node.right.Height()
// 累加遞歸的返回值,左子樹(shù) 和 右子樹(shù) 比較
if lh > rh {
lh++
return lh
} else {
rh++
return rh
}
}
// 統(tǒng)計(jì)二叉樹(shù)的葉子結(jié)點(diǎn)個(gè)數(shù) --- 全局變量
var num = 0 // 記錄葉子數(shù)
func (node *Binary) LeafNum() {
// 遞歸出口
if node == nil {
return
}
// 找尋葉子結(jié)點(diǎn)。 左子樹(shù) == 右子樹(shù) == nil
if node.left == nil && node.right == nil {
num++
}
// 左右子樹(shù)各自遞歸調(diào)用本函數(shù)
node.left.LeafNum()
node.right.LeafNum()
}
// 統(tǒng)計(jì)二叉樹(shù)的葉子結(jié)點(diǎn)個(gè)數(shù) --- 指針傳參
func (node *Binary) LeafNum2(n *int) {
// 遞歸出口
if node == nil {
return
}
if node.left == nil && node.right == nil {
(*n)++
}
// 左右子樹(shù)各自遞歸調(diào)用本函數(shù)
node.left.LeafNum2(n)
node.right.LeafNum2(n)
}
// 查找是否存在數(shù)據(jù)
func (node *Binary) Search(Data interface{}) {
if node == nil {
return
}
// 比較類型、數(shù)值
if reflect.DeepEqual(node.Data, Data) {
fmt.Println("存在!")
return
}
// 左右子樹(shù)各自遞歸調(diào)用本函數(shù)
node.left.Search(Data)
node.right.Search(Data)
}
// 銷毀二叉樹(shù)
func (node *Binary) Destroy() {
if node == nil {
return
}
// 左子樹(shù)遞歸調(diào)用本函數(shù), 銷毀左子樹(shù)
node.left.Destroy()
node.left = nil
// 右子樹(shù)遞歸調(diào)用本函數(shù), 右毀左子樹(shù)
node.right.Destroy()
node.right = nil
node = nil
}
// 二叉樹(shù)的翻轉(zhuǎn)
func (node *Binary) Reverse() {
if node == nil {
return
}
// 利用Go特有多重賦值,交換左、右子樹(shù)
node.left, node.right = node.right, node.left
// 遞歸
node.left.Reverse()
node.right.Reverse()
}
// 二叉樹(shù)的拷貝
func (node *Binary) Copy() *Binary {
if node == nil {
return nil
}
// 左子樹(shù)遞歸進(jìn)入。
lChild := node.left.Copy()
// 右子樹(shù)遞歸進(jìn)入。
rChild := node.right.Copy()
// 創(chuàng)建新結(jié)點(diǎn)
newNode := new(Binary)
newNode.Data = node.Data
newNode.left = lChild
newNode.right = rChild
return newNode
}
func NewBinary(){
node := new(Binary)
node = node.New(0,[]interface{}{1,2,3,4,5,6,7,8,9})
node.PrevSort()
}