@@ -84,6 +84,7 @@ def rotate_right(self) -> RedBlackTree:
8484 parent .left = left
8585 left .parent = parent
8686 return left
87+
8788 def insert (self , label : int ) -> RedBlackTree :
8889 """Inserts label into the subtree rooted at self, performs any
8990 rotations necessary to maintain balance, and then returns the
@@ -317,6 +318,7 @@ def check_coloring(self) -> bool:
317318 if self .left and not self .left .check_coloring ():
318319 return False
319320 return not (self .right and not self .right .check_coloring ())
321+
320322 def black_height (self ) -> int | None :
321323 """Returns the number of black nodes from this node to the
322324 leaves of the tree, or None if there isn't one such value (the
@@ -435,6 +437,7 @@ def sibling(self) -> RedBlackTree | None:
435437 return self .parent .right
436438 else :
437439 return self .parent .left
440+
438441 def is_left (self ) -> bool :
439442 """Returns true iff this node is the left child of its parent."""
440443 if self .parent is None :
@@ -481,6 +484,7 @@ def postorder_traverse(self) -> Iterator[int | None]:
481484 if self .right :
482485 yield from self .right .postorder_traverse ()
483486 yield self .label
487+
484488 def __repr__ (self ) -> str :
485489 if self .left is None and self .right is None :
486490 return f"'{ self .label } { (self .color and 'red' ) or 'blk' } '"
@@ -513,6 +517,7 @@ def color(node: RedBlackTree | None) -> int:
513517 else :
514518 return node .color
515519
520+
516521"""
517522Code for testing the various
518523functions of the red-black tree.
@@ -552,6 +557,7 @@ def test_rotations() -> bool:
552557 right_rot .right .right .right = RedBlackTree (20 , parent = right_rot .right .right )
553558 return tree == right_rot
554559
560+
555561def test_insertion_speed () -> bool :
556562 """Test that the tree balances inserts to O(log(n)) by doing a lot
557563 of them.
@@ -619,6 +625,8 @@ def test_insert_delete() -> bool:
619625 if not tree .check_color_properties ():
620626 return False
621627 return list (tree .inorder_traverse ()) == [- 8 , 0 , 4 , 8 , 10 , 11 , 12 ]
628+
629+
622630def test_floor_ceil () -> bool :
623631 """Tests the floor and ceiling functions in the tree."""
624632 tree = RedBlackTree (0 )
@@ -661,6 +669,8 @@ def test_tree_traversal() -> bool:
661669 if list (tree .preorder_traverse ()) != [0 , - 16 , 16 , 8 , 22 , 20 , 24 ]:
662670 return False
663671 return list (tree .postorder_traverse ()) == [- 16 , 8 , 20 , 24 , 22 , 16 , 0 ]
672+
673+
664674def test_tree_chaining () -> bool :
665675 """Tests the three different tree chaining functions."""
666676 tree = RedBlackTree (0 )
0 commit comments