diff --git a/iterator.go b/iterator.go index ba0d7ae..35aeade 100644 --- a/iterator.go +++ b/iterator.go @@ -79,23 +79,3 @@ func (t *Rbtree) ascendRange(x *Node, inf, sup Item, iterator Iterator) bool { } return t.ascendRange(x.Right, inf, sup, iterator) } - -func (t *Rbtree) Min() Item { - x := t.min(t.root) - - if x == t.NIL { - return nil - } - - return x.Item -} - -func (t *Rbtree) Max() Item { - x := t.max(t.root) - - if x == t.NIL { - return nil - } - - return x.Item -} diff --git a/rbtree.go b/rbtree.go index 57cc1d4..678e985 100644 --- a/rbtree.go +++ b/rbtree.go @@ -48,8 +48,6 @@ func less(x, y Item) bool { // New returns an initialized Red-Black tree func New() *Rbtree { return new(Rbtree).Init() } -func (t *Rbtree) Len() uint { return t.count } - func (t *Rbtree) Init() *Rbtree { node := &Node{nil, nil, nil, BLACK, nil} return &Rbtree{ @@ -135,41 +133,6 @@ func (t *Rbtree) rightRotate(x *Node) { x.Parent = y } -// Use by client -func (t *Rbtree) Insert(item Item) { - if item == nil { - return - } - - // Always insert a RED node - t.insert(&Node{t.NIL, t.NIL, t.NIL, RED, item}) -} - -// Use by client -func (t *Rbtree) Delete(item Item) { - if item == nil { - return - } - - // The `color` field here is nobody - t.delete(&Node{t.NIL, t.NIL, t.NIL, RED, item}) -} - -// Use by client -func (t *Rbtree) Get(item Item) Item { - if item == nil { - return nil - } - - // The `color` field here is nobody - ret := t.search(&Node{t.NIL, t.NIL, t.NIL, RED, item}) - if ret == nil { - return nil - } - - return ret.Item -} - func (t *Rbtree) insert(z *Node) { x := t.root y := t.NIL @@ -313,12 +276,6 @@ func (t *Rbtree) max(x *Node) *Node { return x } -//TODO: This is for debug, delete it in the future -func (t *Rbtree) Search(item Item) *Node { - - return t.search(&Node{t.NIL, t.NIL, t.NIL, RED, item}) -} - func (t *Rbtree) search(x *Node) *Node { p := t.root diff --git a/stats.go b/stats.go new file mode 100644 index 0000000..8b2b36a --- /dev/null +++ b/stats.go @@ -0,0 +1,70 @@ +// Copyright 2015, Hu Keping . All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package rbtree + +// This file contains most of the methods that can be used +// by the user. Anyone who wants to look for some API about +// the rbtree, this is the right place. + +// Number of nodes in the tree. +func (t *Rbtree) Len() uint { return t.count } + +func (t *Rbtree) Insert(item Item) { + if item == nil { + return + } + + // Always insert a RED node + t.insert(&Node{t.NIL, t.NIL, t.NIL, RED, item}) +} + +func (t *Rbtree) Delete(item Item) { + if item == nil { + return + } + + // The `color` field here is nobody + t.delete(&Node{t.NIL, t.NIL, t.NIL, RED, item}) +} + +func (t *Rbtree) Get(item Item) Item { + if item == nil { + return nil + } + + // The `color` field here is nobody + ret := t.search(&Node{t.NIL, t.NIL, t.NIL, RED, item}) + if ret == nil { + return nil + } + + return ret.Item +} + +//TODO: This is for debug, delete it in the future +func (t *Rbtree) Search(item Item) *Node { + + return t.search(&Node{t.NIL, t.NIL, t.NIL, RED, item}) +} + +func (t *Rbtree) Min() Item { + x := t.min(t.root) + + if x == t.NIL { + return nil + } + + return x.Item +} + +func (t *Rbtree) Max() Item { + x := t.max(t.root) + + if x == t.NIL { + return nil + } + + return x.Item +}