diff --git a/README.md b/README.md index 742ca0b..cc64607 100644 --- a/README.md +++ b/README.md @@ -31,33 +31,32 @@ I consider it to be in stable, perhaps even production, shape. There are no know With a healthy Go Language installed, simply run `go get github.com/petar/GoLLRB/llrb` ## Example - - package main - - import ( - "fmt" - "github.com/petar/GoLLRB/llrb" - ) - - func lessInt(a, b interface{}) bool { return a.(int) < b.(int) } - - func main() { - tree := llrb.New(lessInt) - tree.ReplaceOrInsert(1) - tree.ReplaceOrInsert(2) - tree.ReplaceOrInsert(3) - tree.ReplaceOrInsert(4) - tree.DeleteMin() - tree.Delete(4) - c := tree.IterAscend() - for { - u := <-c - if u == nil { - break - } - fmt.Printf("%d\n", int(u.(int))) - } - } + package main + + import ( + "fmt" + "github.com/petar/GoLLRB/llrb" + ) + + func printIntItem(i llrb.Item) bool { + fmt.Printf("%d\n", int(i.(llrb.Int))) + // return false to terminate the traversal procedure + return true + } + + func main() { + tree := llrb.New() + // llrb.Int implemented the Less() method + tree.ReplaceOrInsert(llrb.Int(1)) + tree.ReplaceOrInsert(llrb.Int(2)) + tree.ReplaceOrInsert(llrb.Int(3)) + tree.ReplaceOrInsert(llrb.Int(4)) + tree.AscendGreaterOrEqual(llrb.Int(0), printIntItem) + fmt.Printf("-------\n") + tree.DeleteMin() + tree.Delete(llrb.Int(4)) + tree.AscendGreaterOrEqual(llrb.Int(0), printIntItem) + } ## About diff --git a/example/ex1.go b/example/ex1.go index 6ebe4a6..d6cb6e7 100644 --- a/example/ex1.go +++ b/example/ex1.go @@ -5,22 +5,22 @@ import ( "github.com/petar/GoLLRB/llrb" ) -func lessInt(a, b interface{}) bool { return a.(int) < b.(int) } +func printIntItem(i llrb.Item) bool { + fmt.Printf("%d\n", int(i.(llrb.Int))) + // return false to terminate the traversal procedure + return true +} func main() { - tree := llrb.New(lessInt) - tree.ReplaceOrInsert(1) - tree.ReplaceOrInsert(2) - tree.ReplaceOrInsert(3) - tree.ReplaceOrInsert(4) + tree := llrb.New() + // llrb.Int implemented the Less() method + tree.ReplaceOrInsert(llrb.Int(1)) + tree.ReplaceOrInsert(llrb.Int(2)) + tree.ReplaceOrInsert(llrb.Int(3)) + tree.ReplaceOrInsert(llrb.Int(4)) + tree.AscendGreaterOrEqual(llrb.Int(0), printIntItem) + fmt.Printf("-------\n") tree.DeleteMin() - tree.Delete(4) - c := tree.IterAscend() - for { - u := <-c - if u == nil { - break - } - fmt.Printf("%d\n", int(u.(int))) - } + tree.Delete(llrb.Int(4)) + tree.AscendGreaterOrEqual(llrb.Int(0), printIntItem) }