-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: add a new file to keep functions for common user
Signed-off-by: Hu Keping <[email protected]>
- Loading branch information
Showing
3 changed files
with
70 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |