Skip to content

Commit 03d7920

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 2762dbc commit 03d7920

1 file changed

Lines changed: 8 additions & 9 deletions

File tree

data_structures/binary_tree/red_black_tree.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -335,29 +335,27 @@ def _remove_repair(self) -> None:
335335
self.parent.color = 0
336336
self.parent.sibling.color = 0
337337

338-
339338
def check_color_properties(self) -> bool:
340339
"""
341340
Verify that all Red-Black Tree properties are satisfied:
342341
1. Root node is black
343342
2. No two consecutive red nodes
344343
3. All paths have same black height
345-
344+
346345
Returns:
347346
True if all properties are satisfied, False otherwise
348347
"""
349348
# Property 1: Root must be black
350349
if self.parent is None and self.color != 0:
351350
return False
352-
351+
353352
# Property 2: No two consecutive red nodes
354353
if not self.check_coloring():
355354
return False
356-
355+
357356
# Property 3: All paths have same black height
358357
return self.black_height() is not None
359358

360-
361359
def check_coloring(self) -> bool:
362360
"""Check if the tree satisfies Red-Black property 4."""
363361
if self.color == 1 and 1 in (color(self.left), color(self.right)):
@@ -371,25 +369,26 @@ def black_height(self) -> int | None:
371369
Calculate the black height of the tree and verify consistency
372370
- Black height = number of black nodes from current node to any leaf
373371
- Returns None if any path has different black height
374-
372+
375373
Returns:
376374
Black height if consistent, None otherwise
377375
"""
378376
# Leaf node case (both children are None)
379377
if self.left is None and self.right is None:
380378
# Count: current node (if black) + leaf (black)
381379
return 1 + (1 - self.color) # 2 if black, 1 if red
382-
380+
383381
# Get black heights from both subtrees
384382
left_bh = self.left.black_height() if self.left else 1
385383
right_bh = self.right.black_height() if self.right else 1
386-
384+
387385
# Validate consistency
388386
if left_bh is None or right_bh is None or left_bh != right_bh:
389387
return None
390-
388+
391389
# Add current node's contribution (1 if black, 0 if red)
392390
return left_bh + (1 - self.color)
391+
393392
def __contains__(self, label: int) -> bool:
394393
"""Check if the tree contains a label.
395394

0 commit comments

Comments
 (0)