-
Notifications
You must be signed in to change notification settings - Fork 13
/
insert.go
156 lines (143 loc) · 2.78 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
// Copyright (c) 2014-2016 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package avl
// 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 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
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
}
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 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
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
}
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
}