-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree.go
223 lines (181 loc) · 4.38 KB
/
tree.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package rbytree
import (
"bytes"
)
// Tree holds red-black tree.
// It is not goroutine-safe, make sure that
// the access to the instance of the tree is always synchronized.
type Tree struct {
root *node
size int
}
type color byte
const (
red color = iota
black
)
// node represents the node in the tree.
type node struct {
key []byte
value []byte
parent *node
left *node
right *node
color color
}
// New creates new empty instance of Red-black tree.
func New() *Tree {
return &Tree{}
}
// Put inserts the key with the associated value into the tree.
// If the key is already in the map, it overrides the value and
// returns the previous value.
// Since the value might be null, it also returns a boolean flag
// to distinguish between existent keys and not.
func (t *Tree) Put(key []byte, value []byte) ([]byte, bool) {
// too guarantee that the invariants are not violated
key = copyBytes(key)
newNode := &node{key, value, nil, nil, nil, red}
if t.root == nil {
newNode.color = black
t.root = newNode
t.size = 1
return nil, false
}
current := t.root
var parent *node
var cmp int
for current != nil {
parent = current
cmp = bytes.Compare(key, current.key)
if cmp == 0 {
prev := current.value
current.value = value
return prev, true
}
if cmp < 0 {
current = current.left
} else {
current = current.right
}
}
if cmp < 0 {
parent.left = newNode
} else {
parent.right = newNode
}
newNode.parent = parent
t.fixAfterInsertion(newNode)
t.size++
return nil, false
}
// Get searches the key and returns the associated value and true if found,
// otherwise nil and false.
func (t *Tree) Get(key []byte) ([]byte, bool) {
if t.root == nil {
return nil, false
}
current := t.root
for current != nil {
cmp := bytes.Compare(key, current.key)
if cmp < 0 {
current = current.left
} else if cmp > 0 {
current = current.right
} else {
return current.value, true
}
}
return nil, false
}
// ForEach traverses tree in ascending key order.
func (t *Tree) ForEach(action func(key []byte, value []byte)) {
for it := t.Iterator(); it.HasNext(); {
key, value := it.Next()
action(key, value)
}
}
// fixAfterInsertion fixes the tree to satisfy the red-black tree
// properties of the tree.
func (t *Tree) fixAfterInsertion(newNode *node) {
current := newNode
for current != t.root && current.parent.color == red {
if current.parent.parent.left == current.parent {
uncle := current.parent.parent.right
if uncle != nil && uncle.color == red {
current.parent.color = black
uncle.color = black
current.parent.parent.color = red
current = current.parent.parent
} else {
if current == current.parent.right {
current = current.parent
t.rotateLeft(current)
}
current.parent.color = black
current.parent.parent.color = red
t.rotateRight(current.parent.parent)
}
} else if current.parent.parent.right == current.parent {
uncle := current.parent.parent.left
if uncle != nil && uncle.color == red {
current.parent.color = black
uncle.color = black
current.parent.parent.color = red
current = current.parent.parent
} else {
if current == current.parent.left {
current = current.parent
t.rotateRight(current)
}
current.parent.color = black
current.parent.parent.color = red
t.rotateLeft(current.parent.parent)
}
}
}
t.root.color = black
}
func (t *Tree) rotateLeft(node *node) {
nodeRight := node.right
node.right = nodeRight.left
if nodeRight.left != nil {
nodeRight.left.parent = node
}
nodeRight.parent = node.parent
if node.parent == nil {
t.root = nodeRight
} else if node == node.parent.left {
node.parent.left = nodeRight
} else if node == node.parent.right {
node.parent.right = nodeRight
}
nodeRight.left = node
node.parent = nodeRight
}
func (t *Tree) rotateRight(node *node) {
nodeLeft := node.left
node.left = nodeLeft.right
if nodeLeft.right != nil {
nodeLeft.right.parent = node
}
nodeLeft.parent = node.parent
if node.parent == nil {
t.root = nodeLeft
} else if node == node.parent.left {
node.parent.left = nodeLeft
} else if node == node.parent.right {
node.parent.right = nodeLeft
}
nodeLeft.right = node
node.parent = nodeLeft
}
// Size returns tree size.
func (t *Tree) Size() int {
return t.size
}
func copyBytes(s []byte) []byte {
c := make([]byte, len(s))
copy(c, s)
return c
}