树的创建及遍历

//tree
package main
import (
	_"strings"
	"reflect"
	"fmt"
	"test/stackqueue"
)
type Node struct {
	Data interface{}
	LChild *Node
	RChild *Node
}
func CreateNode(data interface{})*Node {
	return &Node{
		Data:data,
		LChild:nil,
		RChild:nil,
	}
}

/*func NewBinaryTree(arr []interface{}) *Node {
	idx := -1
	return createBinaryTree(arr,&idx)
}
func createBinaryTree(arr []interface{}, idx *int) *Node{
	(*idx)++
	if *idx >= len(arr) {
		return nil
	}
	var root *Node
	if arr[*idx] != 0 {
		root = &Node{arr[*idx],nil,nil}
		root.LChild = createBinaryTree(arr,idx)
		root.RChild = createBinaryTree(arr,idx)
	} else {
		return nil
	}
    return root
}*/
func NewBinaryTree(arr []interface{}) *Node {
	idx := 0
	root := &Node{nil,nil,nil}
	createBinaryTree(&root,arr,&idx)
	return root
}
func createBinaryTree(root **Node, arr []interface{}, idx *int) {
	if *idx < len(arr) && 0 != arr[*idx] {
		(*root) = &Node{arr[*idx],nil,nil}
		(*idx)++
		createBinaryTree(&(*root).LChild, arr, idx)
		(*idx)++
		createBinaryTree(&(*root).RChild, arr, idx)
	}
}
//前序-循环
func (bt *Node) PreOrder()[]*Node {
	s := stackqueue.NewStack(reflect.TypeOf(bt))
	ret := []*Node{}
	s.Push(bt)
	var pCur *Node = nil
	for !s.Empty() {
		pCur = s.Top().(*Node)
		s.Pop()
		for pCur != nil {
			ret = append(ret,pCur)
			if pCur.RChild != nil {
				s.Push(pCur.RChild)
			}
			pCur = pCur.LChild
		}
	}
	return ret
}
//前序-递归
func (bt *Node) PreOrder_Recursion()[]*Node {
	ret := []*Node{}
	pre_order(bt, &ret)
	return ret
}
func pre_order(root *Node, ret *[]*Node) {
	if root != nil {
		*ret = append(*ret, root)
		pre_order(root.LChild,ret)
		pre_order(root.RChild,ret)
	}
}
//中序-循环
func (bt *Node) InOrder()[]*Node{
	pCur := bt
	s := stackqueue.NewStack(reflect.TypeOf(bt))
	ret := []*Node{}
	for pCur != nil || !s.Empty() {
		for pCur != nil {
			s.Push(pCur)
			pCur = pCur.LChild
		}
		pTop := s.Top().(*Node)
		ret = append(ret, pTop)
		s.Pop()
		pCur = pTop.RChild
	}
	return ret
}
//中序-递归
func (bt *Node) InOrder_Recursion()[]*Node {
	ret := []*Node{}
	in_order(bt, &ret)
	return ret
}
func in_order(root *Node, ret *[]*Node) {
	if root != nil {
		in_order(root.LChild,ret)
		*ret = append(*ret, root)
		in_order(root.RChild,ret)
	}
}
//后序-循环
func (bt *Node) PostOrder()[]*Node{
	s := stackqueue.NewStack(reflect.TypeOf(bt))
	ret := []*Node{}
	pCur := bt
	pPre := &Node{}
	for pCur != nil || !s.Empty() {
		for pCur != nil {
			s.Push(pCur)
			pCur = pCur.LChild
		}
		pTop := s.Top().(*Node)
		if pTop.RChild == nil || pTop.RChild == pPre {
			ret = append(ret, pTop)
			pPre = pTop
			s.Pop()
		} else {
			pCur = pTop.RChild
		}
	}
	return ret
}
//后序-递归
func (bt *Node) PostOrder_Recursion()[]*Node {
	ret := []*Node{}
	post_order(bt, &ret)
	return ret
}
func post_order(root *Node, ret *[]*Node) {
	if root != nil {
		post_order(root.LChild,ret)
		post_order(root.RChild,ret)
		*ret = append(*ret, root)
	}
}
//层序
func (bt *Node) LevelOrder()[]*Node{
	q := stackqueue.NewQueue(reflect.TypeOf(bt))
	ret := []*Node{}
	q.Push(bt)
	for !q.Empty() {
		pCur := q.Front().(*Node)
		ret = append(ret, pCur)
		if pCur.LChild != nil{
			q.Push(pCur.LChild)
		}
		if pCur.RChild != nil {
			q.Push(pCur.RChild)
		}
		q.Pop()
	}
	return ret
}
//求二叉树的高-循环
func (bt *Node) Height()int{
	height := 0
	q := stackqueue.NewQueue(reflect.TypeOf(bt))
	q.Push(bt)
	for !q.Empty() {
		height++
		curSize := q.Size()
		count := 0 
		for count < curSize{
			pCur := q.Front().(*Node)
			q.Pop()
			count++
			if pCur.LChild != nil {
				q.Push(pCur.LChild)
			}
			if pCur.RChild != nil {
				q.Push(pCur.RChild)
			}
		}
	}
	return height
}
//求二叉树的高-递归
func (bt *Node) Height_Recursion()int{
	return height_recursion(bt)
}
func height_recursion(root *Node) int {
	if root != nil {
		left_height := height_recursion(root.LChild)
		right_height := height_recursion(root.RChild)
		if left_height >= right_height {
			return left_height+1
		} else {
			return right_height+1
		}
	}
	return 0
}
func main() {
	arr := []interface{}{1,2,3,0,4,5,0,0,6}
	t := NewBinaryTree(arr)
	/*t.LChild = CreateNode(2)
	t.RChild = CreateNode(3)
	t.LChild.LChild = CreateNode(4)
	t.LChild.RChild = CreateNode(5)
	t.RChild.LChild = CreateNode(6)
	t.RChild.RChild = CreateNode(7)
	t.LChild.LChild.LChild = CreateNode(8)*/
	ret := t.PreOrder()
	ret = t.PreOrder_Recursion()
	ret = t.InOrder()
	ret = t.InOrder_Recursion()
	ret = t.PostOrder()
	//ret = t.PostOrder_Recursion()
	//ret = t.LevelOrder()
	for _, v := range ret {
		fmt.Println(v.Data)
	}
	//fmt.Println(ret)
	fmt.Println(t.Height())
	fmt.Println(t.Height_Recursion())
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值