-
Notifications
You must be signed in to change notification settings - Fork 37
/
stats.go
88 lines (68 loc) · 1.92 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// 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.
// Len returns number of nodes in the tree.
func (t *Rbtree) Len() uint { return t.count }
// Insert func inserts a item as a new RED node
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})
}
//InsertOrGet inserts or retrieves the item in the tree. If the
//item is already in the tree then the return value will be that.
//If the item is not in the tree the return value will be the item
//you put in.
func (t *Rbtree) InsertOrGet(item Item) Item {
if item == nil {
return nil
}
return t.insert(&Node{t.NIL, t.NIL, t.NIL, RED, item}).Item
}
//Delete delete the item in the tree
func (t *Rbtree) Delete(item Item) Item {
if item == nil {
return nil
}
// The `color` field here is nobody
return t.delete(&Node{t.NIL, t.NIL, t.NIL, RED, item}).Item
}
//Get search for the specified items which is carried by a Node
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
}
// Search does only search the node which includes it node
//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})
}
// Min return the item minimum one
func (t *Rbtree) Min() Item {
x := t.min(t.root)
if x == t.NIL {
return nil
}
return x.Item
}
// Max return the item maxmum one
func (t *Rbtree) Max() Item {
x := t.max(t.root)
if x == t.NIL {
return nil
}
return x.Item
}