-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcompression.diff
More file actions
197 lines (192 loc) · 4.41 KB
/
Copy pathcompression.diff
File metadata and controls
197 lines (192 loc) · 4.41 KB
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
--- input/input.go
+++ output/output.go
@@ -174,184 +174,37 @@
func (avl *AVL[T]) pushHelper(root *AVLNode[T], key T) *AVLNode[T] {
if root == avl._NIL {
return &AVLNode[T]{
- key: key,
- left: avl._NIL,
- right: avl._NIL,
- parent: avl._NIL,
- height: 1,
- }
- }
-
- switch {
- case key < root.key:
- tmp := avl.pushHelper(root.left, key)
- tmp.parent = root
- root.left = tmp
- case key > root.key:
- tmp := avl.pushHelper(root.right, key)
- tmp.parent = root
- root.right = tmp
- default:
- return root
- }
-
- // balance the tree
- root.height = avl.height(root)
- bFactor := avl.balanceFactor(root)
- if bFactor > 1 {
- switch {
- case key < root.left.key:
- return avl.rightRotate(root)
- case key > root.left.key:
- root.left = avl.leftRotate(root.left)
- return avl.rightRotate(root)
- }
- }
-
- if bFactor < -1 {
- switch {
- case key > root.right.key:
- return avl.leftRotate(root)
- case key < root.right.key:
- root.right = avl.rightRotate(root.right)
- return avl.leftRotate(root)
- }
- }
-
+ { … 44 line(s) … ⟦tj:980887955ae29a60ec188453f315bb2b⟧ }
return root
-}
func (avl *AVL[T]) deleteHelper(root *AVLNode[T], key T) *AVLNode[T] {
if root == avl._NIL {
return root
- }
-
- switch {
- case key < root.key:
- tmp := avl.deleteHelper(root.left, key)
- root.left = tmp
- if tmp != avl._NIL {
- tmp.parent = root
- }
- case key > root.key:
- tmp := avl.deleteHelper(root.right, key)
- root.right = tmp
- if tmp != avl._NIL {
- tmp.parent = root
- }
- default:
- if root.left == avl._NIL || root.right == avl._NIL {
- tmp := root.left
- if root.right != avl._NIL {
- tmp = root.right
- }
-
- if tmp == avl._NIL {
- root = avl._NIL
- } else {
- tmp.parent = root.parent
- root = tmp
- }
- } else {
- tmp := minimum[T](root.right, avl._NIL).(*AVLNode[T])
- root.key = tmp.key
- del := avl.deleteHelper(root.right, tmp.key)
- root.right = del
- if del != avl._NIL {
- del.parent = root
- }
- }
- }
-
- if root == avl._NIL {
- return root
- }
-
- // balance the tree
- root.height = avl.height(root)
- bFactor := avl.balanceFactor(root)
- switch {
- case bFactor > 1:
- switch {
- case avl.balanceFactor(root.left) >= 0:
- return avl.rightRotate(root)
- default:
- root.left = avl.leftRotate(root.left)
- return avl.rightRotate(root)
- }
- case bFactor < -1:
- switch {
- case avl.balanceFactor(root.right) <= 0:
- return avl.leftRotate(root)
- default:
- root.right = avl.rightRotate(root.right)
- return avl.leftRotate(root)
- }
- }
-
+ { … 65 line(s) … ⟦tj:5968df42eb2e9bf0e038350f6a17601a⟧ }
return root
-}
func (avl *AVL[T]) height(root *AVLNode[T]) int {
if root == avl._NIL {
return 1
- }
-
- var leftHeight, rightHeight int
- if root.left != avl._NIL {
- leftHeight = root.left.height
- }
- if root.right != avl._NIL {
- rightHeight = root.right.height
- }
+ { … 9 line(s) … ⟦tj:ac4c4a02d31189805dbfdda04678ad4e⟧ }
return 1 + max.Int(leftHeight, rightHeight)
-}
// balanceFactor : negative balance factor means subtree Root is heavy toward Left
// and positive balance factor means subtree Root is heavy toward Right side
func (avl *AVL[T]) balanceFactor(root *AVLNode[T]) int {
- var leftHeight, rightHeight int
- if root.left != avl._NIL {
- leftHeight = root.left.height
- }
- if root.right != avl._NIL {
- rightHeight = root.right.height
- }
- return leftHeight - rightHeight
-}
+ { … 9 line(s) … ⟦tj:6833e3b19b64eaeaa420e27831f254b4⟧ }
func (avl *AVL[T]) leftRotate(x *AVLNode[T]) *AVLNode[T] {
y := x.right
yl := y.left
- y.left = x
- x.right = yl
-
- if yl != avl._NIL {
- yl.parent = x
- }
-
- y.parent = x.parent
- x.parent = y
-
- x.height = avl.height(x)
- y.height = avl.height(y)
+ { … 12 line(s) … ⟦tj:805efcbfabd2ec7742d2c78dcb28d09e⟧ }
return y
-}
func (avl *AVL[T]) rightRotate(x *AVLNode[T]) *AVLNode[T] {
y := x.left
yr := y.right
- y.right = x
- x.left = yr
-
- if yr != avl._NIL {
- yr.parent = x
- }
-
- y.parent = x.parent
- x.parent = y
-
- x.height = avl.height(x)
- y.height = avl.height(y)
+ { … 12 line(s) … ⟦tj:bcae9ddabd0666126496fc679b7b1258⟧ }
return y
-}
+[omitted blocks are individually retrievable: call tinyjuice_retrieve with the token inside an omission marker to expand just that block]
+
+[PARTIAL view — full original (7718 bytes): call tinyjuice_retrieve with token "b8ff72f42c7f56dcce67519bb8cc458f"]
\ No newline at end of file