-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
183 lines (144 loc) · 3.21 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
package interval
import (
"math"
"sync"
)
type color int
const (
red color = 0
black color = 1
sentinelPayload string = "sentinel"
)
type ErrNotFound string
func (e ErrNotFound) Error() string {
return string(e)
}
// Tree represents an Interval tree with a root node and Mutex to
// protect concurrent access.
type Tree struct {
lock sync.RWMutex
root *node
sentinel *node
}
// Result is a search result when looking up an interval in the tree.
type Result struct {
Interval Interval
Payload interface{}
}
// NewIntervalTree returns an initialized but empty interval tree.
func NewIntervalTree() *Tree {
sentinel := &node{color: black, payload: sentinelPayload}
return &Tree{
lock: sync.RWMutex{},
root: sentinel,
sentinel: sentinel,
}
}
// Root returns a Result of the payload of the root node of the tree or an
// ErrNotFound if the tree is empty.
func (t *Tree) Root() (Result, error) {
t.lock.RLock()
defer t.lock.RUnlock()
if t.root == t.sentinel {
return Result{}, ErrNotFound("tree is empty")
}
return Result{
Interval: t.root.key,
Payload: t.root.payload,
}, nil
}
// Height returns the height (max depth) of the tree. Returns -1 if the tree
// has no nodes. A (rooted) tree with only a single node has a height of zero.
func (t *Tree) Height() int {
t.lock.RLock()
defer t.lock.RUnlock()
return int(t.height(t.root))
}
func (t *Tree) height(node *node) float64 {
if node == t.sentinel {
return -1
}
return 1 + math.Max(t.height(node.left), t.height(node.right))
}
// Min returns a Result of the lowest interval in the tree or an ErrNotFound if
// the tree is empty.
func (t *Tree) Min() (Result, error) {
t.lock.RLock()
defer t.lock.RUnlock()
n := t.min(t.root)
if n == t.sentinel {
return Result{}, ErrNotFound("tree is empty")
}
return Result{
Interval: n.key,
Payload: n.payload,
}, nil
}
func (t *Tree) rotateLeft(x *node) {
// y's left subtree will be x's right subtree.
y := x.right
x.right = y.left
if y.left != t.sentinel {
y.left.parent = x
}
// Restore parent relationships.
y.parent = x.parent
switch {
case x.parent == t.sentinel:
t.root = y
case x.parent.left == x:
x.parent.left = y
default:
x.parent.right = y
}
// x will be y's new left-child.
y.left = x
x.parent = y
t.updateMax(x)
}
func (t *Tree) rotateRight(x *node) {
y := x.left
x.left = y.right
if y.right != t.sentinel {
y.right.parent = x
}
y.parent = x.parent
switch {
case x.parent == t.sentinel:
t.root = y
case x.parent.left == x:
x.parent.left = y
default:
x.parent.right = y
}
y.right = x
x.parent = y
t.updateMax(y)
}
func (t *Tree) newLeaf(key Interval, p interface{}) *node {
return &node{
key: key,
payload: p,
left: t.sentinel,
right: t.sentinel,
max: key.high,
}
}
func (t *Tree) isLeaf(z *node) bool {
return z.left == t.sentinel && z.right == t.sentinel
}
func (t *Tree) min(z *node) *node {
for z != t.sentinel && z.left != t.sentinel {
z = z.left
}
return z
}
func (t *Tree) updateMax(z *node) {
z.max = z.key.high
if z.right != t.sentinel && z.right.max.After(z.max) {
z.max = z.right.max
}
if z.left != t.sentinel && z.left.max.After(z.max) {
z.max = z.left.max
}
}