-
Notifications
You must be signed in to change notification settings - Fork 13
/
insert.go
191 lines (168 loc) · 3.49 KB
/
insert.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
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2020 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package avl
// Insert - insert a new node into the tree
// returns the possibly updated root
func (tree *Tree) Insert(key Item, value interface{}) bool {
added := false
tree.root, added, _ = insert(key, value, tree.root)
if added {
tree.count += 1
}
return added
}
// internal routine for insert
func insert(key Item, value interface{}, p *Node) (*Node, bool, bool) {
h := false
if nil == p { // insert new node
h = true
p = newNode(key, value)
return p, true, h
}
added := false
switch p.key.Compare(key) {
case +1: // p.key > key
p.left, added, h = insert(key, value, p.left)
if added {
p.leftNodes += 1
}
if h {
if nil != p.left {
p.left.up = p
}
// left branch has grown
if 1 == p.balance {
p.balance = 0
h = false
} else if 0 == p.balance {
p.balance = -1
} else { // balance == -1, rebalance
p1 := p.left
if -1 == p1.balance {
// single LL rotation
p.left = p1.right
p1.right = p
p.balance = 0
nn := 1 + p1.rightNodes + p.rightNodes
p.leftNodes = p1.rightNodes
p1.rightNodes = nn
p1.up = p.up
p.up = p1
if nil != p.left {
p.left.up = p
}
p = p1
} else {
// double LR rotation
p2 := p1.right
p1.right = p2.left
p2.left = p1
p.left = p2.right
p2.right = p
if -1 == p2.balance {
p.balance = 1
} else {
p.balance = 0
}
if +1 == p2.balance {
p1.balance = -1
} else {
p1.balance = 0
}
nl := 1 + p1.leftNodes + p2.leftNodes
nr := 1 + p2.rightNodes + p.rightNodes
p1.rightNodes = p2.leftNodes
p.leftNodes = p2.rightNodes
p2.leftNodes = nl
p2.rightNodes = nr
if nil != p.left {
p.left.up = p
}
if nil != p1.right {
p1.right.up = p1
}
p2.up = p.up
p.up = p2
p1.up = p2
p = p2
}
p.balance = 0
h = false
}
}
case -1: // p.key < key
p.right, added, h = insert(key, value, p.right)
if added {
p.rightNodes += 1
}
if h {
if nil != p.right {
p.right.up = p
}
// right branch has grown
if -1 == p.balance {
p.balance = 0
h = false
} else if 0 == p.balance {
p.balance = 1
} else { // balance = +1, rebalance
p1 := p.right
if 1 == p1.balance {
// single RR rotation
p.right = p1.left
p1.left = p
p.balance = 0
nn := 1 + p.leftNodes + p1.leftNodes
p.rightNodes = p1.leftNodes
p1.leftNodes = nn
p1.up = p.up
p.up = p1
if nil != p.right {
p.right.up = p
}
p = p1
} else {
// double RL rotation
p2 := p1.left
p1.left = p2.right
p2.right = p1
p.right = p2.left
p2.left = p
if +1 == p2.balance {
p.balance = -1
} else {
p.balance = 0
}
if -1 == p2.balance {
p1.balance = 1
} else {
p1.balance = 0
}
nl := 1 + p.leftNodes + p2.leftNodes
nr := 1 + p2.rightNodes + p1.rightNodes
p.rightNodes = p2.leftNodes
p1.leftNodes = p2.rightNodes
p2.leftNodes = nl
p2.rightNodes = nr
if nil != p.right {
p.right.up = p
}
if nil != p1.left {
p1.left.up = p1
}
p2.up = p.up
p.up = p2
p1.up = p2
p = p2
}
p.balance = 0
h = false
}
}
default:
p.value = value
}
return p, added, h
}