Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: add a new file to keep functions for common user #16

Merged
merged 1 commit into from
Jul 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}