-
Notifications
You must be signed in to change notification settings - Fork 11
/
avl.go
220 lines (196 loc) · 4.05 KB
/
avl.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
// Package avl is a Adelson-Velskii and Landis tree implemnation
// avl is self-balancing tree, i.e for all node in a tree, height difference
// between its left and right child will not exceed 1
// more information : https://en.wikipedia.org/wiki/AVL_tree
package avl
// Node of a tree
type Node struct {
Key int
Height int
Left, Right *Node
}
// NewTree create a new AVL tree
func NewTree() *Node {
return nil
}
// Get : return node with given key
func Get(root *Node, key int) *Node {
if root == nil {
return nil
}
if root.Key == key {
return root
} else if root.Key < key {
root = root.Right
} else {
root = root.Left
}
return Get(root, key)
}
// Insert a new item
func Insert(root **Node, key int) {
if *root == nil {
*root = &Node{
Key: key,
Height: 1,
}
return
}
if (*root).Key < key {
Insert(&(*root).Right, key)
} else if (*root).Key > key {
Insert(&(*root).Left, key)
}
// update height
(*root).Height = height(*root)
bFactor := balanceFactor(*root)
if bFactor == 2 { // L
bFactor = balanceFactor((*root).Left)
if bFactor == 1 { // LL
llRotation(root)
} else if bFactor == -1 { // LR
lrRotation(root)
}
} else if bFactor == -2 { // R
bFactor = balanceFactor((*root).Right)
if bFactor == 1 { // RL
rlRotation(root)
} else if bFactor == -1 { // RR
rrRotation(root)
}
}
}
// Delete : remove given key from the tree
func Delete(root **Node, key int) {
if root == nil {
return
}
if (*root).Key < key {
Delete(&(*root).Right, key)
} else if (*root).Key > key {
Delete(&(*root).Left, key)
} else {
// 3 cases
// 1. No Child
// 2. With One Child
// 3. With Two Child
if (*root).Left == nil && (*root).Right == nil {
*root = nil
} else if (*root).Left == nil {
*root = (*root).Right
} else if (*root).Right == nil {
*root = (*root).Left
} else {
minVal := min((*root).Right)
(*root).Key = minVal
Delete(root, minVal)
}
return
}
// update height
(*root).Height = height(*root)
bFactor := balanceFactor(*root)
if bFactor == 2 { // L
switch balanceFactor((*root).Left) {
case 1: // LL
llRotation(root)
case -1: // LR
lrRotation(root)
case 0: // LL OR LR
llRotation(root)
}
} else if bFactor == -2 { // L
switch balanceFactor((*root).Right) {
case 1: // RL
rlRotation(root)
case -1: // RR
rrRotation(root)
case 0: // RL OR RR
rrRotation(root)
}
}
}
// rotations
// 1. LL
// 2. LR
// 3. RR
// 4. RL
func llRotation(root **Node) {
b := (*root).Left
br := b.Right
b.Right = *root
(*root).Left = br
(*root).Height = height(*root)
b.Height = height(b)
*root = b
}
func lrRotation(root **Node) {
c := (*root).Left.Right
cl := c.Left
cr := c.Right
c.Left = (*root).Left
c.Right = (*root)
c.Left.Right = cl
(*root).Left = cr
(*root).Height = height(*root)
c.Left.Height = height(c.Left)
c.Height = height(c)
*root = c
}
func rrRotation(root **Node) {
b := (*root).Right
bl := b.Left
b.Left = *root
(*root).Right = bl
(*root).Height = height(*root)
b.Height = height(b)
*root = b
}
func rlRotation(root **Node) {
c := (*root).Right.Left
cl := c.Left
cr := c.Right
c.Right = (*root).Right
c.Right.Left = cr
c.Left = *root
(*root).Right = cl
(*root).Height = height(*root)
c.Right.Height = height(c.Right)
c.Height = height(c)
*root = c
}
// balanceFactor : -ve balance factor means subtree root is heavy toward left
// and +ve balance factor means subtree root is heavy toward right side
func balanceFactor(root *Node) int {
var leftHeight, rightHeight int
if root.Left != nil {
leftHeight = root.Left.Height
}
if root.Right != nil {
rightHeight = root.Right.Height
}
return leftHeight - rightHeight
}
func height(root *Node) int {
if root == nil {
return 0
}
var leftHeight, rightHeight int
if root.Left != nil {
leftHeight = root.Left.Height
}
if root.Right != nil {
rightHeight = root.Right.Height
}
max := leftHeight
if rightHeight > leftHeight {
max = rightHeight
}
return 1 + max
}
func min(root *Node) int {
if root.Left == nil {
return root.Key
}
return min(root.Left)
}