golang简单实现二叉树的数据添加和遍历

代码实现

package tree

import "fmt"

type Node struct {
    elem        interface{}
    left, right *Node
}

type Tree struct {
    root *Node
}

func NewTree() *Tree {
    return &Tree{}
}

// 添加元素
func (this *Tree) Add(v interface{}) {
    node := &Node{elem: v}
    if this.root == nil {
        this.root = node
        return
    }

    c := make(chan *Node, 10)
    c <- this.root

    for len(c) > 0 {
        curNode := <-c
        if curNode.left == nil {
            curNode.left = node
            return
        } else {
            c <- curNode.left
        }
        if curNode.right == nil {
            curNode.right = node
            return
        } else {
            c <- curNode.right
        }
    }
}

// 广度优先遍历
func (this *Tree) Travel() {
    if this.root == nil {
        fmt.Println("empty tree")
        return
    }

    c := make(chan *Node, 10)
    c <- this.root

    for len(c) > 0 {
        curNode := <-c
        fmt.Println(curNode.elem)
        if curNode.left != nil {
            c <- curNode.left
        }
        if curNode.right != nil {
            c <- curNode.right
        }
    }
}

// 先序遍历
func (this *Tree) Xianxu() {
    xz(this.root)
}

// 中序遍历
func (this *Tree) ZhongXu() {
    zx(this.root)
}

// 后序遍历
func (this *Tree) HouXu() {
    hx(this.root)
}

func xz(node *Node) {
    if node == nil {
        return
    }
    fmt.Println(node.elem)
    xz(node.left)
    xz(node.right)
}

func zx(node *Node) {
    if node == nil {
        return
    }
    zx(node.left)
    fmt.Println(node.elem)
    zx(node.right)
}

func hx(node *Node) {
    if node == nil {
        return
    }
    hx(node.left)
    hx(node.right)
    fmt.Println(node.elem)
}

测试

package tree

import "testing"

var tree *Tree

func prepare() {
    tree = NewTree()
    tree.Add("mark")
    tree.Add("jack")
    tree.Add("timo")
    tree.Add("marry")
    tree.Add("toshiyuki")
    tree.Add("naruto")
    tree.Add("sakura")
}

func TestTree_Travel(t *testing.T) {
    prepare()
    tree.Travel()

}

func TestTree_Xianxu(t *testing.T) {
    prepare()
    tree.Xianxu()

}

func TestTree_ZhongXu(t *testing.T) {
    prepare()
    tree.ZhongXu()

}
func TestTree_HouXu(t *testing.T) {
    prepare()
    tree.HouXu()

}

测试结果

=== RUN   TestTree_Travel
mark
jack
timo
marry
toshiyuki
naruto
sakura
--- PASS: TestTree_Travel (0.00s)
=== RUN   TestTree_Xianxu
mark
jack
marry
toshiyuki
timo
naruto
sakura
--- PASS: TestTree_Xianxu (0.00s)
=== RUN   TestTree_ZhongXu
marry
jack
toshiyuki
mark
naruto
timo
sakura
--- PASS: TestTree_ZhongXu (0.00s)
=== RUN   TestTree_HouXu
marry
toshiyuki
jack
naruto
sakura
timo
mark
--- PASS: TestTree_HouXu (0.00s)
PASS

转载于:https://www.cnblogs.com/endurance9/p/10434738.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值