Skip to content

Commit

Permalink
Merge pull request #16 from HuKeping/refactor
Browse files Browse the repository at this point in the history
Refactor: add a new file to keep functions for common user
  • Loading branch information
HuKeping committed Jul 13, 2015
2 parents 8fac6c9 + 25abdc3 commit b5a50ce
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 63 deletions.
20 changes: 0 additions & 20 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
43 changes: 0 additions & 43 deletions rbtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
70 changes: 70 additions & 0 deletions stats.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit b5a50ce

Please sign in to comment.