-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllrb.go
152 lines (125 loc) · 2.21 KB
/
llrb.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
package llrb
import "math"
type Color bool
const (
BLACK Color = true
RED Color = false
)
// Key is base interface for abstraction
type Key interface{ Compare(with Key) int }
// Value is base interface for abstraction
type Value interface{}
// Node ...
type Node struct {
left, right *Node
key Key
value Value
color Color
}
func isRed(n *Node) bool {
if n == nil {
return false
}
return n.color == RED
}
func newNode(k Key, v Value) *Node {
return &Node{key: k, value: v, color: RED}
}
// Tree ...
type Tree struct {
root *Node
size int
}
func NewTree() *Tree {
return &Tree{}
}
func (t *Tree) insert(k Key, v Value) {
t.root = insertHelper(t.root, k, v)
t.root.color = BLACK
}
func insertHelper(n *Node, k Key, v Value) *Node {
if n == nil {
return newNode(k, v)
}
// Insertion
cmp := n.key.Compare(k)
if cmp == 0 {
n.value = v
}
if cmp < 0 {
n.left = insertHelper(n.left, k, v)
}
if cmp > 0 {
n.right = insertHelper(n.right, k, v)
}
// Balancing
if isRed(n.right) && !isRed(n.left) {
n = rotateLeft(n)
}
if isRed(n.left) && isRed(n.left.left) {
n = rotateRight(n)
}
if isRed(n.left) && isRed(n.right) {
colorFlip(n)
}
return n
}
func rotateLeft(n *Node) *Node {
x := n.right
n.right = x.left
x.left = n
x.color = n.color
n.color = RED
return x
}
func rotateRight(n *Node) *Node {
x := n.left
n.left = x.right
x.right = n
x.color = n.color
n.color = RED
return x
}
func colorFlip(n *Node) {
n.color = !n.color
n.left.color = !n.left.color
n.right.color = !n.right.color
}
// Search searches given key in the tree and returns nil if search fails
func (t *Tree) search(k Key) Value {
x := t.root
for x != nil {
cmp := x.key.Compare(k)
if cmp == 0 {
return x.value
}
if cmp < 0 {
x = x.left
}
if cmp > 0 {
x = x.right
}
}
return nil
}
func isBalanced(n *Node) bool {
if n == nil {
return false
}
lh := heightOf(n.left)
rh := heightOf(n.right)
if math.Abs(float64(lh-rh)) <= 1 &&
isBalanced(n.left) &&
isBalanced(n.right) {
return true
}
return false
}
func heightOf(n *Node) int {
if n == nil {
return 0
}
lh := float64(heightOf(n.left))
rh := float64(heightOf(n.right))
return 1 + int(math.Max(lh, rh))
}