Skip to content

Commit 17ea820

Browse files
authored
Update red_black_tree.py
1 parent 4bb794d commit 17ea820

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

data_structures/binary_tree/red_black_tree.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -336,36 +336,28 @@ def _remove_repair(self) -> None:
336336
self.parent.sibling.color = 0
337337

338338

339-
def check_color_properties(self) -> bool:
339+
def check_color_properties(self) -> bool:
340340
"""
341341
Verify that all Red-Black Tree properties are satisfied:
342-
# Root node is black
343-
# No two consecutive red nodes (red node cannot have red children)
344-
# All paths from any node to its leaf descendants have
345-
# the same number of black nodes
346-
342+
1. Root node is black
343+
2. No two consecutive red nodes
344+
3. All paths have same black height
345+
347346
Returns:
348347
True if all properties are satisfied, False otherwise
349-
350-
Examples:
351-
>>> tree = RedBlackTree(10).insert(5).insert(15)
352-
>>> tree.check_color_properties()
353-
True
354-
>>> tree.left.color = 1 # Force invalid state: two consecutive reds
355-
>>> tree.check_color_properties()
356-
False
357348
"""
358349
# Property 1: Root must be black
359350
if self.parent is None and self.color != 0:
360351
return False
361-
352+
362353
# Property 2: No two consecutive red nodes
363354
if not self.check_coloring():
364355
return False
365-
356+
366357
# Property 3: All paths have same black height
367358
return self.black_height() is not None
368359

360+
369361
def check_coloring(self) -> bool:
370362
"""Check if the tree satisfies Red-Black property 4."""
371363
if self.color == 1 and 1 in (color(self.left), color(self.right)):

0 commit comments

Comments
 (0)