41. 等價二叉樹

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

實現兩個二叉樹的比較。二叉樹的基本類型和函數來源於 “golang.org/x/tour/tree”,為了避免網路問題影響代碼運行,我把源碼直接加入到了代碼中。

// A Tree is a binary tree with integer values.type Tree struct {    Left  *Tree    Value int    Right *Tree}// New returns a new, random binary tree holding the values k, 2k, ..., 10k.func New(k int) *Tree {    var t *Tree    for _, v := range rand.Perm(10) {        t = insert(t, (1+v)*k)    }    return t}func insert(t *Tree, v int) *Tree {    if t == nil {        return &Tree{nil, v, nil}    }    if v < t.Value {        t.Left = insert(t.Left, v)    } else {        t.Right = insert(t.Right, v)    }    return t}func (t *Tree) String() string {    if t == nil {        return "()"    }    s := ""    if t.Left != nil {        s += t.Left.String() + " "    }    s += fmt.Sprint(t.Value)    if t.Right != nil {        s += " " + t.Right.String()    }    return "(" + s + ")"}

以上代碼實現了二叉樹的準系統。下面我們來實現二叉樹的比較。
要比較兩個二叉樹是否一致。我們建立一個函數。Same(t1, t2 *Tree) bool
為了比較二叉樹,必要把二叉樹的值放進一個通道中。共使用兩個通道來進行比較。放入通道中的函數我們建立為 Walk(t *Tree, ch chan int) 同時再建立一個遞迴函式,用來遍曆二叉樹所有的葉子節點 rangeTree(t *Tree, ch chan int)

//遍曆二叉樹,把當前節點值傳入通道func rangeTree(t *Tree, ch chan int)  {    if t != nil{        rangeTree(t.Left, ch)        ch <- t.Value        rangeTree(t.Right, ch)    }}// Walk 步進 tree t 將所有的值從 tree 發送到 channel ch。func Walk(t *Tree, ch chan int){    rangeTree(t, ch)    close(ch)}// Same 檢測樹 t1 和 t2 是否含有相同的值。func Same(t1, t2 *Tree) bool{    //建立兩個通道    ch1 := make(chan int)    ch2 := make(chan int)    //遍曆兩個二叉樹,把值傳入各自的通道    go Walk(t1, ch1)    go Walk(t2, ch2)    //遍曆通道進行比較,有不同的值就返回false    for i := range ch1{        if i != <- ch2{            return false        }    }    return true}

然後在 main函數中比較兩個 tree

    fmt.Println(Same(New(1), New(1)))    fmt.Println(Same(New(1), New(2)))

完整程式碼範例

package mainimport (    "fmt"    "math/rand")// A Tree is a binary tree with integer values.type Tree struct {    Left  *Tree    Value int    Right *Tree}// New returns a new, random binary tree holding the values k, 2k, ..., 10k.func New(k int) *Tree {    var t *Tree    for _, v := range rand.Perm(10) {        t = insert(t, (1+v)*k)    }    return t}func insert(t *Tree, v int) *Tree {    if t == nil {        return &Tree{nil, v, nil}    }    if v < t.Value {        t.Left = insert(t.Left, v)    } else {        t.Right = insert(t.Right, v)    }    return t}func (t *Tree) String() string {    if t == nil {        return "()"    }    s := ""    if t.Left != nil {        s += t.Left.String() + " "    }    s += fmt.Sprint(t.Value)    if t.Right != nil {        s += " " + t.Right.String()    }    return "(" + s + ")"}// Walk 步進 tree t 將所有的值從 tree 發送到 channel ch。func Walk(t *Tree, ch chan int){    rangeTree(t, ch)    close(ch)}//遍曆二叉樹,把當前節點值傳入通道func rangeTree(t *Tree, ch chan int)  {    if t != nil{        rangeTree(t.Left, ch)        ch <- t.Value        rangeTree(t.Right, ch)    }}// Same 檢測樹 t1 和 t2 是否含有相同的值。func Same(t1, t2 *Tree) bool{    //建立兩個通道    ch1 := make(chan int)    ch2 := make(chan int)    //遍曆兩個二叉樹,把值傳入各自的通道    go Walk(t1, ch1)    go Walk(t2, ch2)    //遍曆通道進行比較,有不同的值就返回false    for i := range ch1{        if i != <- ch2{            return false        }    }    return true}func main() {    fmt.Println("二叉樹遍曆比較")    fmt.Println("列印 New(1)的值")    //列印 New(1)的值    var ch1 = make(chan int)    go Walk(New(1), ch1)    for v := range ch1{        fmt.Println(v)    }    fmt.Println("列印 New(2)的值")    //列印 New(2)的值    var ch2  = make(chan int)    go Walk(New(2), ch2)    for v := range ch2{        fmt.Println(v)    }    //比較兩個tree的值是否相等    fmt.Println(Same(New(1), New(1)))    fmt.Println(Same(New(1), New(2)))}

運行結果

二叉樹遍曆比較列印 New(1)的值12345678910列印 New(2)的值2468101214161820truefalse
相關關鍵詞:
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.