Permalink
Browse files

fixed readme, added example

  • Loading branch information...
1 parent 8671e8a commit c8dda739ce1484a8e525367cd969dbbb7edc28cd Petar Maymounkov committed Apr 28, 2011
Showing with 47 additions and 25 deletions.
  1. +9 −13 README.md
  2. +26 −0 example/ex1.go
  3. +12 −12 llrb/llrb.go
View
@@ -40,27 +40,23 @@ With a healthy Google Go installed, simply run `goinstall github.com/petar/GoLLR
"github.com/petar/GoLLRB/llrb"
)
- type IntItem int
-
- func (item IntItem) LessThan(other interface{}) bool {
- return int(item) < int(other.(IntItem))
- }
+ func lessInt(a, b interface{}) bool { return a.(int) < b.(int) }
func main() {
- var tree llrb.Tree
- tree.InsertOrReplace(IntItem(1))
- tree.InsertOrReplace(IntItem(2))
- tree.InsertOrReplace(IntItem(3))
- tree.InsertOrReplace(IntItem(4))
+ tree := llrb.New(lessInt)
+ tree.ReplaceOrInsert(1)
+ tree.ReplaceOrInsert(2)
+ tree.ReplaceOrInsert(3)
+ tree.ReplaceOrInsert(4)
tree.DeleteMin()
- tree.Delete(IntItem(4))
- c := tree.Iter()
+ tree.Delete(4)
+ c := tree.IterAscend()
for {
u := <-c
if u == nil {
break
}
- fmt.Printf("%d\n", int(u.(IntItem)))
+ fmt.Printf("%d\n", int(u.(int)))
}
}
View
@@ -0,0 +1,26 @@
+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)))
+ }
+}
View
@@ -125,17 +125,6 @@ func (t *Tree) ReplaceOrInsert(item Item) Item {
return replaced
}
-// InsertOrReplace() inserts @item into the tree. If an existing
-// element has the same order, both elements remain in the tree.
-func (t *Tree) InsertNoReplace(item Item) {
- if item == nil {
- panic("inserting nil item")
- }
- t.root = t.insertNoReplace(t.root, item)
- t.root.black = true
- t.count++
-}
-
func (t *Tree) replaceOrInsert(h *Node, item Item) (*Node, Item) {
if h == nil {
return newNode(item), nil
@@ -144,7 +133,7 @@ func (t *Tree) replaceOrInsert(h *Node, item Item) (*Node, Item) {
h = walkDownRot23(h)
var replaced Item
- if t.less(item, h.item) {
+ if t.less(item, h.item) { // BUG
h.left, replaced = t.replaceOrInsert(h.left, item)
} else if t.less(h.item, item) {
h.right, replaced = t.replaceOrInsert(h.right, item)
@@ -157,6 +146,17 @@ func (t *Tree) replaceOrInsert(h *Node, item Item) (*Node, Item) {
return h, replaced
}
+// InsertNoReplace() inserts @item into the tree. If an existing
+// element has the same order, both elements remain in the tree.
+func (t *Tree) InsertNoReplace(item Item) {
+ if item == nil {
+ panic("inserting nil item")
+ }
+ t.root = t.insertNoReplace(t.root, item)
+ t.root.black = true
+ t.count++
+}
+
func (t *Tree) insertNoReplace(h *Node, item Item) *Node {
if h == nil {
return newNode(item)

0 comments on commit c8dda73

Please sign in to comment.