The official website has an example of a binary sort tree, which adds the ability to find and delete nodes.
Code:
Package main//binary Search Trees//author:xiong Chuan liang//date:2015-2-1import ("FMT" "Math/rand") func main () {t: = New (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 (1) fmt. Println (Compare (T2, New (1)), "Compare () Same Contents") fmt. Println (Compare (T2, New (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}//in sequence traversal 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.va Lue: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.valu E, 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 {Retu RN False}return Deletenode (parent, T)}func Deletenode (parent, T *tree) bool {if t.left = nil && t.right = nil {f Mt. Println ("Delete () left and right trees are empty") if parent. left = = T {paRent. left = nil} else if parent. right = = T {parent. right = Nil}t = Nilreturn true}if T.right = = Nil {//R tree is empty FMT. Println ("Delete () Right tree is empty") parent. left = T.left.leftparent.value = T.left.valueparent.right = T.left.rightt.left = Nilt = Nilreturn true}if T.Left = = Nil { Left tree is empty fmt. Println ("Delete () left tree is empty") parent. left = T.right.leftparent.value = T.right.valueparent.right = T.right.rightt.right = Nilt = Nilreturn true}fmt. Println ("Delete () the tree is not empty") Previous: = t//Find the right-most node of the left Dial hand node, replace its value to the deleted node//And then clear the right-most leaf node, so to maintain the tree,//In this case, The right-most-left node is the node that is really deleted 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 Gor outine,//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 both walkers//that run simultaneously, and returns true//if T1 and T2 has the S Ame 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}
Operating effect:
Search (6) Truenode:1node:2node:3node:4node:5node:6node:7node:8node:9node:10delete () the left and right trees are empty delete (6) truenode:1n Ode:2node:3node:4node:5node:7node:8node:9node:10delete () The right tree is empty delete (9) Truenode:1node:2node:3node:4node:5node : 8node:10getmin () = 1GetMax () = 10true Compare () same contentsfalse Compare () differing Sizes
In the official website example, the Compare function is really awesome.
MAIL: [Email protected]
blog:http://blog.csdn.net/xcl168
Go language Implementation Two binary search tree (binary search Trees)