Go語言實現二叉尋找樹(Binary Search Trees)

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

    官網有一個二叉排序樹的例子,在此基礎上增加了尋找和刪除節點功能。

   代碼:  

package main//Binary Search Trees//author: Xiong Chuan Liang//date: 2015-2-1import ("fmt""math/rand")func main() {t := New(10, 1)if Search(t, 6) {fmt.Println("Search(6) true")} else {fmt.Println("Search(6) false")}Print(t)if Delete(t, 6) {fmt.Println("Delete(6) true")} else {fmt.Println("Delete(6) false")}Print(t)if Delete(t, 9) {fmt.Println("Delete(9) true")} else {fmt.Println("Delete(9) false")}Print(t)min, foundMin := GetMin(t)if foundMin {fmt.Println("GetMin() =", min)}max, foundMax := GetMax(t)if foundMax {fmt.Println("GetMax() =", max)}t2 := New(100, 1)fmt.Println(Compare(t2, New(100, 1)), " Compare() Same Contents")fmt.Println(Compare(t2, New(99, 1)), " Compare() Differing Sizes")}type Tree struct {Left  *TreeValue intRight *Tree}func New(n, k int) *Tree {var t *Treefor _, v := range rand.Perm(n) {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)return t}t.Right = Insert(t.Right, v)return t}//中序遍曆func Print(t *Tree) { //Recursiveif t == nil {return}Print(t.Left)fmt.Println("node:", t.Value)Print(t.Right)}func Search(t *Tree, v int) bool {if t == nil {return false}switch {case v == t.Value:return truecase v < t.Value:return Search(t.Left, v)case v > t.Value:return Search(t.Right, v)}return false}func GetMin(t *Tree) (int, bool) {if t == nil {return -1, false}for {if t.Left != nil {t = t.Left} else {return t.Value, true}}}func GetMax(t *Tree) (int, bool) {if t == nil {return -1, false}for {if t.Right != nil {t = t.Right} else {return t.Value, true}}}func Delete(t *Tree, v int) bool {if t == nil {return false}parent := tfound := falsefor {if t == nil {break}if v == t.Value {found = truebreak}parent = tif v < t.Value { //leftt = t.Left} else {t = t.Right}}if found == false {return false}return deleteNode(parent, t)}func deleteNode(parent, t *Tree) bool {if t.Left == nil && t.Right == nil {fmt.Println("delete() 左右樹都為空白 ")if parent.Left == t {parent.Left = nil} else if parent.Right == t {parent.Right = nil}t = nilreturn true}if t.Right == nil { //右樹為空白fmt.Println("delete() 右樹為空白 ")parent.Left = t.Left.Leftparent.Value = t.Left.Valueparent.Right = t.Left.Rightt.Left = nilt = nilreturn true}if t.Left == nil { //左樹為空白fmt.Println("delete() 左樹為空白 ")parent.Left = t.Right.Leftparent.Value = t.Right.Valueparent.Right = t.Right.Rightt.Right = nilt = nilreturn true}fmt.Println("delete() 左右樹都不為空白 ")previous := t//找到左子節點的最右分葉節點,將其值替換至被刪除節點//然後將這個最右分葉節點清除,所以說,為了維持樹,//這種情況下,這個最右分葉節點才是真正被刪除的節點next := t.Leftfor {if next.Right == nil {break}previous = nextnext = next.Right}t.Value = next.Valueif previous.Left == next {previous.Left = next.Left} else {previous.Right = next.Right}next.Left = nilnext.Right = nilnext = nilreturn true}// Walk traverses a tree depth-first,// sending each Value on a channel.func Walk(t *Tree, ch chan int) {if t == nil {return}Walk(t.Left, ch)ch <- t.ValueWalk(t.Right, ch)}// Walker launches Walk in a new goroutine,// and returns a read-only channel of values.func Walker(t *Tree) <-chan int {ch := make(chan int)go func() {Walk(t, ch)close(ch)}()return ch}// Compare reads values from two Walkers// that run simultaneously, and returns true// if t1 and t2 have the same contents.func Compare(t1, t2 *Tree) bool {c1, c2 := Walker(t1), Walker(t2)for {v1, ok1 := <-c1v2, ok2 := <-c2if !ok1 || !ok2 {return ok1 == ok2}if v1 != v2 {break}}return false}
運行效果: 

Search(6) truenode: 1node: 2node: 3node: 4node: 5node: 6node: 7node: 8node: 9node: 10delete() 左右樹都為空白Delete(6) truenode: 1node: 2node: 3node: 4node: 5node: 7node: 8node: 9node: 10delete() 右樹為空白Delete(9) truenode: 1node: 2node: 3node: 4node: 5node: 8node: 10GetMin() = 1GetMax() = 10true  Compare() Same Contentsfalse  Compare() Differing Sizes
 官網的例子中,Compare函數真是贊。


MAIL : xcl_168@aliyun.com

BLOG: http://blog.csdn.net/xcl168




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.