Skip to content

Commit e56009f

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 4c3fa76 commit e56009f

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

other/lru_cache.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def rotate_left(self) -> RedBlackTree:
6363
parent.right = right
6464
right.parent = parent
6565
return right
66+
6667
def rotate_right(self) -> RedBlackTree:
6768
"""Rotate the subtree rooted at this node to the right and
6869
returns the new root to this subtree.
@@ -313,6 +314,7 @@ def check_color_properties(self) -> bool:
313314
return False
314315
# All properties were met
315316
return True
317+
316318
def check_coloring(self) -> bool:
317319
"""A helper function to recursively check Property 4 of a
318320
Red-Black Tree. See check_color_properties for more info.
@@ -328,17 +330,17 @@ def black_height(self) -> int | None:
328330
# 叶子节点(None)被视为黑色,高度为1
329331
if self is None:
330332
return 1
331-
333+
332334
# 递归计算左右子树高度
333335
left_bh = RedBlackTree.black_height(self.left)
334336
right_bh = RedBlackTree.black_height(self.right)
335-
337+
336338
# 检查高度是否有效且一致
337339
if left_bh is None or right_bh is None:
338340
return None
339341
if left_bh != right_bh:
340342
return None
341-
343+
342344
# 返回当前节点高度(黑色节点+1)
343345
return left_bh + (1 - self.color)
344346

@@ -367,6 +369,7 @@ def search(self, label: int) -> RedBlackTree | None:
367369
return None
368370
else:
369371
return self.left.search(label)
372+
370373
def floor(self, label: int) -> int | None:
371374
"""Returns the largest element in this tree which is at most label.
372375
This method is guaranteed to run in O(log(n)) time."""
@@ -439,6 +442,7 @@ def sibling(self) -> RedBlackTree | None:
439442
return self.parent.right
440443
else:
441444
return self.parent.left
445+
442446
def is_left(self) -> bool:
443447
"""Returns true iff this node is the left child of its parent."""
444448
if self.parent is None:
@@ -486,6 +490,7 @@ def postorder_traverse(self) -> Iterator[int | None]:
486490
if self.right:
487491
yield from self.right.postorder_traverse()
488492
yield self.label
493+
489494
def __repr__(self) -> str:
490495
if self.left is None and self.right is None:
491496
return f"'{self.label} {(self.color and 'red') or 'blk'}'"
@@ -503,13 +508,13 @@ def __eq__(self, other: object) -> bool:
503508
"""Test if two trees are equal."""
504509
if not isinstance(other, RedBlackTree):
505510
return NotImplemented
506-
511+
507512
# 处理空树比较
508513
if self.label is None and other.label is None:
509514
return True
510515
if self.label != other.label:
511516
return False
512-
517+
513518
# 递归比较子树
514519
return (self.left == other.left) and (self.right == other.right)
515520

@@ -561,6 +566,7 @@ def test_rotations() -> bool:
561566
right_rot.right.right.right = RedBlackTree(20, parent=right_rot.right.right)
562567
return tree == right_rot
563568

569+
564570
def test_insertion_speed() -> bool:
565571
"""Test that the tree balances inserts to O(log(n)) by doing a lot
566572
of them.
@@ -644,6 +650,8 @@ def test_floor_ceil() -> bool:
644650
if tree.floor(val) != floor or tree.ceil(val) != ceil:
645651
return False
646652
return True
653+
654+
647655
def test_min_max() -> bool:
648656
"""Tests the min and max functions in the tree."""
649657
tree = RedBlackTree(0)
@@ -671,6 +679,7 @@ def test_tree_traversal() -> bool:
671679
return False
672680
return list(tree.postorder_traverse()) == [-16, 8, 20, 24, 22, 16, 0]
673681

682+
674683
def test_tree_chaining() -> bool:
675684
"""Tests the three different tree chaining functions."""
676685
tree = RedBlackTree(0)
@@ -685,34 +694,36 @@ def test_tree_chaining() -> bool:
685694
def test_empty_tree() -> bool:
686695
"""Tests behavior with empty trees."""
687696
tree = RedBlackTree(None)
688-
697+
689698
# 测试空树属性
690699
if tree.label is not None or tree.left or tree.right:
691700
return False
692-
701+
693702
# 测试空树长度
694703
if len(tree) != 0:
695704
return False
696-
705+
697706
# 测试空树布尔值
698707
if tree:
699708
return False
700-
709+
701710
# 测试空树搜索
702711
if 0 in tree or tree.search(0):
703712
return False
704-
713+
705714
# 测试空树删除
706715
try:
707716
tree.remove(0)
708717
except Exception:
709718
return False
710-
719+
711720
return True
712721

713722

714723
def print_results(msg: str, passes: bool) -> None:
715724
print(str(msg), "works!" if passes else "doesn't work :(")
725+
726+
716727
def pytests() -> None:
717728
assert test_rotations()
718729
assert test_insert()

0 commit comments

Comments
 (0)