Skip to content

Commit 5a1c1ae

Browse files
committed
Remove blank lines
1 parent ec6b94d commit 5a1c1ae

File tree

1 file changed

+0
-37
lines changed

1 file changed

+0
-37
lines changed

data_structures/binary_tree/red_black_tree.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,13 @@ def rotate_left(self) -> RedBlackTree:
5151
"""
5252
parent = self.parent
5353
right = self.right
54-
5554
if right is None:
5655
return self
57-
5856
self.right = right.left
59-
6057
if self.right:
6158
self.right.parent = self
6259
self.parent = right
63-
6460
right.left = self
65-
6661
if parent is not None:
6762
if parent.left == self:
6863
parent.left = right
@@ -80,16 +75,11 @@ def rotate_right(self) -> RedBlackTree:
8075
return self
8176
parent = self.parent
8277
left = self.left
83-
8478
self.left = left.right
85-
8679
if self.left:
8780
self.left.parent = self
88-
8981
self.parent = left
90-
9182
left.right = self
92-
9383
if parent is not None:
9484
if parent.right is self:
9585
parent.right = left
@@ -143,24 +133,20 @@ def _insert_repair(self) -> None:
143133
self.parent.rotate_left()
144134
if self.left:
145135
self.left._insert_repair()
146-
147136
elif self.is_left():
148137
if self.grandparent:
149138
self.grandparent.rotate_right()
150139
self.parent.color = 0
151-
152140
if self.parent.right:
153141
self.parent.right.color = 1
154142
else:
155143
if self.grandparent:
156144
self.grandparent.rotate_left()
157145
self.parent.color = 0
158-
159146
if self.parent.left:
160147
self.parent.left.color = 1
161148
else:
162149
self.parent.color = 0
163-
164150
if uncle and self.grandparent:
165151
uncle.color = 0
166152
self.grandparent.color = 1
@@ -186,7 +172,6 @@ def remove(self, label: int) -> RedBlackTree:
186172
# is if both children are None leaves.
187173
# We can just remove this node and call it a day.
188174
if self.parent:
189-
190175
if self.is_left():
191176
self.parent.left = None
192177
else:
@@ -232,7 +217,6 @@ def _remove_repair(self) -> None:
232217
or self.grandparent is None
233218
):
234219
return
235-
236220
if color(self.sibling) == 1:
237221
self.sibling.color = 0
238222
self.parent.color = 1
@@ -266,7 +250,6 @@ def _remove_repair(self) -> None:
266250
):
267251
self.sibling.rotate_right()
268252
self.sibling.color = 0
269-
270253
if self.sibling.right:
271254
self.sibling.right.color = 1
272255
if (
@@ -277,7 +260,6 @@ def _remove_repair(self) -> None:
277260
):
278261
self.sibling.rotate_left()
279262
self.sibling.color = 0
280-
281263
if self.sibling.left:
282264
self.sibling.left.color = 1
283265
if (
@@ -314,21 +296,17 @@ def check_color_properties(self) -> bool:
314296
"""
315297
# I assume property 1 to hold because there is nothing that can
316298
# make the color be anything other than 0 or 1.
317-
318299
# Property 2
319300
if self.color:
320301
# The root was red
321302
print("Property 2")
322303
return False
323-
324304
# Property 3 does not need to be checked, because None is assumed
325305
# to be black and is all the leaves.
326-
327306
# Property 4
328307
if not self.check_coloring():
329308
print("Property 4")
330309
return False
331-
332310
# Property 5
333311
if self.black_height() is None:
334312
print("Property 5")
@@ -385,7 +363,6 @@ def search(self, label: int) -> RedBlackTree | None:
385363
"""
386364
if self.label == label:
387365
return self
388-
389366
elif self.label is not None and label > self.label:
390367
if self.right is None:
391368
return None
@@ -474,7 +451,6 @@ def is_left(self) -> bool:
474451
"""Returns true iff this node is the left child of its parent."""
475452
if self.parent is None:
476453
return False
477-
478454
return self.parent.left is self.parent.left is self
479455

480456
def is_right(self) -> bool:
@@ -491,7 +467,6 @@ def __len__(self) -> int:
491467
Return the number of nodes in this tree.
492468
"""
493469
ln = 1
494-
495470
if self.left:
496471
ln += len(self.left)
497472
if self.right:
@@ -538,7 +513,6 @@ def __eq__(self, other: object) -> bool:
538513
"""Test if two trees are equal."""
539514
if not isinstance(other, RedBlackTree):
540515
return NotImplemented
541-
542516
if self.label == other.label:
543517
return self.left == other.left and self.right == other.right
544518
else:
@@ -563,9 +537,7 @@ def test_rotations() -> bool:
563537
"""Test that the rotate_left and rotate_right functions work."""
564538
# Make a tree to test on
565539
tree = RedBlackTree(0)
566-
567540
tree.left = RedBlackTree(-10, parent=tree)
568-
569541
tree.right = RedBlackTree(10, parent=tree)
570542
tree.left.left = RedBlackTree(-20, parent=tree.left)
571543
tree.left.right = RedBlackTree(-5, parent=tree.left)
@@ -580,10 +552,8 @@ def test_rotations() -> bool:
580552
left_rot.left.left.right = RedBlackTree(-5, parent=left_rot.left.left)
581553
left_rot.right = RedBlackTree(20, parent=left_rot)
582554
tree = tree.rotate_left()
583-
584555
if tree != left_rot:
585556
return False
586-
587557
tree = tree.rotate_right()
588558
tree = tree.rotate_right()
589559
# Make the left rotation
@@ -752,19 +722,12 @@ def main() -> None:
752722
>>> pytests()
753723
"""
754724
print_results("Rotating right and left", test_rotations())
755-
756725
print_results("Inserting", test_insert())
757-
758726
print_results("Searching", test_insert_and_search())
759-
760727
print_results("Deleting", test_insert_delete())
761-
762728
print_results("Floor and ceil", test_floor_ceil())
763-
764729
print_results("Tree traversal", test_tree_traversal())
765-
766730
print_results("Tree traversal", test_tree_chaining())
767-
768731
print("Testing tree balancing...")
769732
print("This should only be a few seconds.")
770733
test_insertion_speed()

0 commit comments

Comments
 (0)