@@ -370,36 +370,25 @@ def black_height(self) -> int | None:
370370 """
371371 Calculate the black height of the tree and verify consistency
372372 - Black height = number of black nodes from current node to any leaf
373- - Count includes current node if black and all leaf nodes (None are black)
374373 - Returns None if any path has different black height
375-
374+
376375 Returns:
377376 Black height if consistent, None otherwise
378-
379- Examples:
380- >>> tree = RedBlackTree(10, color=0) # Black root
381- >>> tree.insert(5, color=1) # Red child
382- >>> tree.black_height() # Paths: [10(black), 5(red), None(black)] = 2
383- 2 [10(black), None(black)] = 2
384377 """
385- # Base case: Leaf node (None is always black)
386- if self is None :
387- return 1
388-
389378 # Leaf node case (both children are None)
390379 if self .left is None and self .right is None :
391380 # Count: current node (if black) + leaf (black)
392381 return 1 + (1 - self .color ) # 2 if black, 1 if red
393-
394- # Recursive case: Check both subtrees
382+
383+ # Get black heights from both subtrees
395384 left_bh = self .left .black_height () if self .left else 1
396385 right_bh = self .right .black_height () if self .right else 1
397-
386+
398387 # Validate consistency
399388 if left_bh is None or right_bh is None or left_bh != right_bh :
400- return None # Inconsistent black heights
401-
402- # Current node's contribution: add 1 if black
389+ return None
390+
391+ # Add current node's contribution ( 1 if black, 0 if red)
403392 return left_bh + (1 - self .color )
404393 def __contains__ (self , label : int ) -> bool :
405394 """Check if the tree contains a label.
0 commit comments