-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompression.diff
More file actions
641 lines (599 loc) · 24.7 KB
/
Copy pathcompression.diff
File metadata and controls
641 lines (599 loc) · 24.7 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
--- input/input.py
+++ output/output.py
@@ -41,301 +41,54 @@
# Here are functions which are specific to red-black trees
def rotate_left(self) -> RedBlackTree:
- """Rotate the subtree rooted at this node to the left and
- returns the new root to this subtree.
- Performing one rotation can be done in O(1).
- """
- parent = self.parent
- right = self.right
- if right is None:
- return self
- self.right = right.left
- if self.right:
- self.right.parent = self
- self.parent = right
- right.left = self
- if parent is not None:
- if parent.left == self:
- parent.left = right
- else:
- parent.right = right
- right.parent = parent
+ """Rotate the subtree rooted at this node to the left and
+ returns the new root to this subtree.
+... # 17 line(s) collapsed ⟦tj:2d75ce0d2b19ff14671b16d3e6a80c71⟧
return right
def rotate_right(self) -> RedBlackTree:
- """Rotate the subtree rooted at this node to the right and
- returns the new root to this subtree.
- Performing one rotation can be done in O(1).
- """
- if self.left is None:
- return self
- parent = self.parent
- left = self.left
- self.left = left.right
- if self.left:
- self.left.parent = self
- self.parent = left
- left.right = self
- if parent is not None:
- if parent.right is self:
- parent.right = left
- else:
- parent.left = left
- left.parent = parent
+ """Rotate the subtree rooted at this node to the right and
+ returns the new root to this subtree.
+... # 17 line(s) collapsed ⟦tj:57b1ba48ed62415ddd706f934f108f5c⟧
return left
def insert(self, label: int) -> RedBlackTree:
- """Inserts label into the subtree rooted at self, performs any
- rotations necessary to maintain balance, and then returns the
- new root to this subtree (likely self).
- This is guaranteed to run in O(log(n)) time.
- """
- if self.label is None:
- # Only possible with an empty tree
- self.label = label
- return self
- if self.label == label:
- return self
- elif self.label > label:
- if self.left:
- self.left.insert(label)
- else:
- self.left = RedBlackTree(label, 1, self)
- self.left._insert_repair()
- elif self.right:
- self.right.insert(label)
- else:
- self.right = RedBlackTree(label, 1, self)
- self.right._insert_repair()
+ """Inserts label into the subtree rooted at self, performs any
+ rotations necessary to maintain balance, and then returns the
+... # 20 line(s) collapsed ⟦tj:befc51b2ab78f3e7262e26b070551572⟧
return self.parent or self
def _insert_repair(self) -> None:
- """Repair the coloring from inserting into a tree."""
- if self.parent is None:
- # This node is the root, so it just needs to be black
- self.color = 0
- elif color(self.parent) == 0:
- # If the parent is black, then it just needs to be red
- self.color = 1
- else:
- uncle = self.parent.sibling
- if color(uncle) == 0:
- if self.is_left() and self.parent.is_right():
- self.parent.rotate_right()
- if self.right:
- self.right._insert_repair()
- elif self.is_right() and self.parent.is_left():
- self.parent.rotate_left()
- if self.left:
- self.left._insert_repair()
- elif self.is_left():
- if self.grandparent:
- self.grandparent.rotate_right()
- self.parent.color = 0
- if self.parent.right:
- self.parent.right.color = 1
- else:
- if self.grandparent:
- self.grandparent.rotate_left()
- self.parent.color = 0
- if self.parent.left:
- self.parent.left.color = 1
- else:
- self.parent.color = 0
- if uncle and self.grandparent:
- uncle.color = 0
- self.grandparent.color = 1
+ """Repair the coloring from inserting into a tree."""
+ if self.parent is None:
+... # 33 line(s) collapsed ⟦tj:eb8901b7247bd361503e31762ef5946d⟧
self.grandparent._insert_repair()
def remove(self, label: int) -> RedBlackTree:
- """Remove label from this tree."""
- if self.label == label:
- if self.left and self.right:
- # It's easier to balance a node with at most one child,
- # so we replace this node with the greatest one less than
- # it and remove that.
- value = self.left.get_max()
- if value is not None:
- self.label = value
- self.left.remove(value)
- else:
- # This node has at most one non-None child, so we don't
- # need to replace
- child = self.left or self.right
- if self.color == 1:
- # This node is red, and its child is black
- # The only way this happens to a node with one child
- # is if both children are None leaves.
- # We can just remove this node and call it a day.
- if self.parent:
- if self.is_left():
- self.parent.left = None
- else:
- self.parent.right = None
- # The node is black
- elif child is None:
- # This node and its child are black
- if self.parent is None:
- # The tree is now empty
- return RedBlackTree(None)
- else:
- self._remove_repair()
- if self.is_left():
- self.parent.left = None
- else:
- self.parent.right = None
- self.parent = None
- else:
- # This node is black and its child is red
- # Move the child node here and make it black
- self.label = child.label
- self.left = child.left
- self.right = child.right
- if self.left:
- self.left.parent = self
- if self.right:
- self.right.parent = self
- elif self.label is not None and self.label > label:
- if self.left:
- self.left.remove(label)
- elif self.right:
- self.right.remove(label)
+ """Remove label from this tree."""
+ if self.label == label:
+... # 50 line(s) collapsed ⟦tj:ca0594ef386d04b2c3cd01c176d85a0d⟧
return self.parent or self
def _remove_repair(self) -> None:
- """Repair the coloring of the tree that may have been messed up."""
- if (
- self.parent is None
- or self.sibling is None
- or self.parent.sibling is None
- or self.grandparent is None
- ):
- return
- if color(self.sibling) == 1:
- self.sibling.color = 0
- self.parent.color = 1
- if self.is_left():
- self.parent.rotate_left()
- else:
- self.parent.rotate_right()
- if (
- color(self.parent) == 0
- and color(self.sibling) == 0
- and color(self.sibling.left) == 0
- and color(self.sibling.right) == 0
- ):
- self.sibling.color = 1
- self.parent._remove_repair()
- return
- if (
- color(self.parent) == 1
- and color(self.sibling) == 0
- and color(self.sibling.left) == 0
- and color(self.sibling.right) == 0
- ):
- self.sibling.color = 1
- self.parent.color = 0
- return
- if (
- self.is_left()
- and color(self.sibling) == 0
- and color(self.sibling.right) == 0
- and color(self.sibling.left) == 1
- ):
- self.sibling.rotate_right()
- self.sibling.color = 0
- if self.sibling.right:
- self.sibling.right.color = 1
- if (
- self.is_right()
- and color(self.sibling) == 0
- and color(self.sibling.right) == 1
- and color(self.sibling.left) == 0
- ):
- self.sibling.rotate_left()
- self.sibling.color = 0
- if self.sibling.left:
- self.sibling.left.color = 1
- if (
- self.is_left()
- and color(self.sibling) == 0
- and color(self.sibling.right) == 1
- ):
- self.parent.rotate_left()
- self.grandparent.color = self.parent.color
- self.parent.color = 0
+ """Repair the coloring of the tree that may have been messed up."""
+ if (
+... # 68 line(s) collapsed ⟦tj:8b779663a824d8051f2d6ceb1cafdae2⟧
self.parent.sibling.color = 0
- if (
- self.is_right()
- and color(self.sibling) == 0
- and color(self.sibling.left) == 1
- ):
- self.parent.rotate_right()
- self.grandparent.color = self.parent.color
- self.parent.color = 0
- self.parent.sibling.color = 0
def check_color_properties(self) -> bool:
- """Check the coloring of the tree, and return True iff the tree
- is colored in a way which matches these five properties:
- (wording stolen from wikipedia article)
- 1. Each node is either red or black.
- 2. The root node is black.
- 3. All leaves are black.
- 4. If a node is red, then both its children are black.
- 5. Every path from any node to all of its descendent NIL nodes
- has the same number of black nodes.
- This function runs in O(n) time, because properties 4 and 5 take
- that long to check.
- """
- # I assume property 1 to hold because there is nothing that can
- # make the color be anything other than 0 or 1.
- # Property 2
- if self.color:
- # The root was red
- print("Property 2")
- return False
- # Property 3 does not need to be checked, because None is assumed
- # to be black and is all the leaves.
- # Property 4
- if not self.check_coloring():
- print("Property 4")
- return False
- # Property 5
- if self.black_height() is None:
- print("Property 5")
- return False
- # All properties were met
+ """Check the coloring of the tree, and return True iff the tree
+ is colored in a way which matches these five properties:
+... # 28 line(s) collapsed ⟦tj:6ae263c8b4785c8ff038ca7fe5fc2532⟧
return True
def check_coloring(self) -> bool:
- """A helper function to recursively check Property 4 of a
- Red-Black Tree. See check_color_properties for more info.
- """
- if self.color == 1 and 1 in (color(self.left), color(self.right)):
- return False
- if self.left and not self.left.check_coloring():
- return False
- return not (self.right and not self.right.check_coloring())
+ ... # 8 line(s) collapsed ⟦tj:6f222bb54b2d9cd83443aeb145866922⟧
def black_height(self) -> int | None:
- """Returns the number of black nodes from this node to the
- leaves of the tree, or None if there isn't one such value (the
- tree is color incorrectly).
- """
- if self is None or self.left is None or self.right is None:
- # If we're already at a leaf, there is no path
- return 1
- left = RedBlackTree.black_height(self.left)
- right = RedBlackTree.black_height(self.right)
- if left is None or right is None:
- # There are issues with coloring below children nodes
- return None
- if left != right:
- # The two children have unequal depths
- return None
- # Return the black depth of children, plus one if this node is
- # black
+ """Returns the number of black nodes from this node to the
+ leaves of the tree, or None if there isn't one such value (the
+... # 15 line(s) collapsed ⟦tj:a94beb9afa94e459836439dca902e2a7⟧
return left + (1 - self.color)
# Here are functions which are general to all binary search trees
@@ -348,76 +101,28 @@
return self.search(label) is not None
def search(self, label: int) -> RedBlackTree | None:
- """Search through the tree for label, returning its node if
- it's found, and None otherwise.
- This method is guaranteed to run in O(log(n)) time.
- """
- if self.label == label:
- return self
- elif self.label is not None and label > self.label:
- if self.right is None:
- return None
- else:
- return self.right.search(label)
- elif self.left is None:
- return None
- else:
+ """Search through the tree for label, returning its node if
+ it's found, and None otherwise.
+... # 12 line(s) collapsed ⟦tj:80cb2d2ac7ee2d0164a8757f921d7f5d⟧
return self.left.search(label)
def floor(self, label: int) -> int | None:
- """Returns the largest element in this tree which is at most label.
- This method is guaranteed to run in O(log(n)) time."""
- if self.label == label:
+ """Returns the largest element in this tree which is at most label.
+ This method is guaranteed to run in O(log(n)) time."""
+... # 12 line(s) collapsed ⟦tj:6dec1252e934e8b07966f01932645985⟧
return self.label
- elif self.label is not None and self.label > label:
- if self.left:
- return self.left.floor(label)
- else:
- return None
- else:
- if self.right:
- attempt = self.right.floor(label)
- if attempt is not None:
- return attempt
- return self.label
def ceil(self, label: int) -> int | None:
- """Returns the smallest element in this tree which is at least label.
- This method is guaranteed to run in O(log(n)) time.
- """
- if self.label == label:
+ """Returns the smallest element in this tree which is at least label.
+ This method is guaranteed to run in O(log(n)) time.
+... # 13 line(s) collapsed ⟦tj:8de4eb1f4a101aeeb1194d74e8e752f6⟧
return self.label
- elif self.label is not None and self.label < label:
- if self.right:
- return self.right.ceil(label)
- else:
- return None
- else:
- if self.left:
- attempt = self.left.ceil(label)
- if attempt is not None:
- return attempt
- return self.label
def get_max(self) -> int | None:
- """Returns the largest element in this tree.
- This method is guaranteed to run in O(log(n)) time.
- """
- if self.right:
- # Go as far right as possible
- return self.right.get_max()
- else:
- return self.label
+ ... # 8 line(s) collapsed ⟦tj:8bdfb21b11fe5bbefc6b8febba4f8615⟧
def get_min(self) -> int | None:
- """Returns the smallest element in this tree.
- This method is guaranteed to run in O(log(n)) time.
- """
- if self.left:
- # Go as far left as possible
- return self.left.get_min()
- else:
- return self.label
+ ... # 8 line(s) collapsed ⟦tj:adbc3c85206b25c59ea3627f2120a5cc⟧
@property
def grandparent(self) -> RedBlackTree | None:
@@ -453,15 +158,7 @@
return True
def __len__(self) -> int:
- """
- Return the number of nodes in this tree.
- """
- ln = 1
- if self.left:
- ln += len(self.left)
- if self.right:
- ln += len(self.right)
- return ln
+ ... # 9 line(s) collapsed ⟦tj:ebd8f15925c8f11b71d6cd845452af0b⟧
def preorder_traverse(self) -> Iterator[int | None]:
yield self.label
@@ -485,18 +182,9 @@
yield self.label
def __repr__(self) -> str:
- from pprint import pformat
-
- if self.left is None and self.right is None:
- return f"'{self.label} {(self.color and 'red') or 'blk'}'"
- return pformat(
- {
- f"{self.label} {(self.color and 'red') or 'blk'}": (
- self.left,
- self.right,
- )
- },
- indent=1,
+ from pprint import pformat
+
+... # 10 line(s) collapsed ⟦tj:d44c4d9bcc3f958d1c37644a7d3588f1⟧
)
def __eq__(self, other: object) -> bool:
@@ -524,36 +212,9 @@
def test_rotations() -> bool:
- """Test that the rotate_left and rotate_right functions work."""
- # Make a tree to test on
- tree = RedBlackTree(0)
- tree.left = RedBlackTree(-10, parent=tree)
- tree.right = RedBlackTree(10, parent=tree)
- tree.left.left = RedBlackTree(-20, parent=tree.left)
- tree.left.right = RedBlackTree(-5, parent=tree.left)
- tree.right.left = RedBlackTree(5, parent=tree.right)
- tree.right.right = RedBlackTree(20, parent=tree.right)
- # Make the right rotation
- left_rot = RedBlackTree(10)
- left_rot.left = RedBlackTree(0, parent=left_rot)
- left_rot.left.left = RedBlackTree(-10, parent=left_rot.left)
- left_rot.left.right = RedBlackTree(5, parent=left_rot.left)
- left_rot.left.left.left = RedBlackTree(-20, parent=left_rot.left.left)
- left_rot.left.left.right = RedBlackTree(-5, parent=left_rot.left.left)
- left_rot.right = RedBlackTree(20, parent=left_rot)
- tree = tree.rotate_left()
- if tree != left_rot:
- return False
- tree = tree.rotate_right()
- tree = tree.rotate_right()
- # Make the left rotation
- right_rot = RedBlackTree(-10)
- right_rot.left = RedBlackTree(-20, parent=right_rot)
- right_rot.right = RedBlackTree(0, parent=right_rot)
- right_rot.right.left = RedBlackTree(-5, parent=right_rot.right)
- right_rot.right.right = RedBlackTree(10, parent=right_rot.right)
- right_rot.right.right.left = RedBlackTree(5, parent=right_rot.right.right)
- right_rot.right.right.right = RedBlackTree(20, parent=right_rot.right.right)
+ """Test that the rotate_left and rotate_right functions work."""
+ # Make a tree to test on
+... # 28 line(s) collapsed ⟦tj:7d1ff0bd5bbe190bad63ee4d195099ff⟧
return tree == right_rot
@@ -568,117 +229,46 @@
def test_insert() -> bool:
- """Test the insert() method of the tree correctly balances, colors,
- and inserts.
- """
- tree = RedBlackTree(0)
- tree.insert(8)
- tree.insert(-8)
- tree.insert(4)
- tree.insert(12)
- tree.insert(10)
- tree.insert(11)
- ans = RedBlackTree(0, 0)
- ans.left = RedBlackTree(-8, 0, ans)
- ans.right = RedBlackTree(8, 1, ans)
- ans.right.left = RedBlackTree(4, 0, ans.right)
- ans.right.right = RedBlackTree(11, 0, ans.right)
- ans.right.right.left = RedBlackTree(10, 1, ans.right.right)
- ans.right.right.right = RedBlackTree(12, 1, ans.right.right)
+ """Test the insert() method of the tree correctly balances, colors,
+ and inserts.
+... # 15 line(s) collapsed ⟦tj:f4efcfb66393e30deadc97a9f0818ecd⟧
return tree == ans
def test_insert_and_search() -> bool:
- """Tests searching through the tree for values."""
- tree = RedBlackTree(0)
- tree.insert(8)
- tree.insert(-8)
- tree.insert(4)
- tree.insert(12)
- tree.insert(10)
- tree.insert(11)
- if any(i in tree for i in (5, -6, -10, 13)):
- # Found something not in there
- return False
- # Find all these things in there
+ """Tests searching through the tree for values."""
+ tree = RedBlackTree(0)
+... # 10 line(s) collapsed ⟦tj:ff4ad8b630ad119a070eb5bce9ca26e7⟧
return all(i in tree for i in (11, 12, -8, 0))
def test_insert_delete() -> bool:
- """Test the insert() and delete() method of the tree, verifying the
- insertion and removal of elements, and the balancing of the tree.
- """
- tree = RedBlackTree(0)
- tree = tree.insert(-12)
- tree = tree.insert(8)
- tree = tree.insert(-8)
- tree = tree.insert(15)
- tree = tree.insert(4)
- tree = tree.insert(12)
- tree = tree.insert(10)
- tree = tree.insert(9)
- tree = tree.insert(11)
- tree = tree.remove(15)
- tree = tree.remove(-12)
- tree = tree.remove(9)
- if not tree.check_color_properties():
- return False
+ """Test the insert() and delete() method of the tree, verifying the
+ insertion and removal of elements, and the balancing of the tree.
+... # 16 line(s) collapsed ⟦tj:b6ce061d7d7792a5dab40659ac1bc13c⟧
return list(tree.inorder_traverse()) == [-8, 0, 4, 8, 10, 11, 12]
def test_floor_ceil() -> bool:
- """Tests the floor and ceiling functions in the tree."""
- tree = RedBlackTree(0)
- tree.insert(-16)
- tree.insert(16)
- tree.insert(8)
- tree.insert(24)
- tree.insert(20)
- tree.insert(22)
- tuples = [(-20, None, -16), (-10, -16, 0), (8, 8, 8), (50, 24, None)]
- for val, floor, ceil in tuples:
- if tree.floor(val) != floor or tree.ceil(val) != ceil:
- return False
+ """Tests the floor and ceiling functions in the tree."""
+ tree = RedBlackTree(0)
+... # 10 line(s) collapsed ⟦tj:2ca8cb739269cbe6d9d866cc990c6fca⟧
return True
def test_min_max() -> bool:
- """Tests the min and max functions in the tree."""
- tree = RedBlackTree(0)
- tree.insert(-16)
- tree.insert(16)
- tree.insert(8)
- tree.insert(24)
- tree.insert(20)
- tree.insert(22)
- return not (tree.get_max() != 22 or tree.get_min() != -16)
+ ... # 9 line(s) collapsed ⟦tj:e8e7a6939a49692824f75633d610bdcf⟧
def test_tree_traversal() -> bool:
- """Tests the three different tree traversal functions."""
- tree = RedBlackTree(0)
- tree = tree.insert(-16)
- tree.insert(16)
- tree.insert(8)
- tree.insert(24)
- tree.insert(20)
- tree.insert(22)
- if list(tree.inorder_traverse()) != [-16, 0, 8, 16, 20, 22, 24]:
- return False
- if list(tree.preorder_traverse()) != [0, -16, 16, 8, 22, 20, 24]:
- return False
+ """Tests the three different tree traversal functions."""
+ tree = RedBlackTree(0)
+... # 10 line(s) collapsed ⟦tj:a55d5a0ea8e2fe9a8518b8c6a7489dcb⟧
return list(tree.postorder_traverse()) == [-16, 8, 20, 24, 22, 16, 0]
def test_tree_chaining() -> bool:
- """Tests the three different tree chaining functions."""
- tree = RedBlackTree(0)
- tree = tree.insert(-16).insert(16).insert(8).insert(24).insert(20).insert(22)
- if list(tree.inorder_traverse()) != [-16, 0, 8, 16, 20, 22, 24]:
- return False
- if list(tree.preorder_traverse()) != [0, -16, 16, 8, 22, 20, 24]:
- return False
- return list(tree.postorder_traverse()) == [-16, 8, 20, 24, 22, 16, 0]
+ ... # 8 line(s) collapsed ⟦tj:42389fa4fea178af6462cede193209a5⟧
def print_results(msg: str, passes: bool) -> None:
@@ -713,4 +303,7 @@
if __name__ == "__main__":
- main()
+ main()
+[omitted blocks are individually retrievable: call tinyjuice_retrieve with the token inside an omission marker to expand just that block]
+
+[PARTIAL view — full original (25467 bytes): call tinyjuice_retrieve with token "bd143cc1e37121407c74ca03595ccd59"]
\ No newline at end of file