OccupyMars2025 / Introduction-to-Algorithms-4th-Edition

Time is Money, Efficiency is Life
MIT License
1 stars 0 forks source link

[solved][bug][ch13, red-black tree] After inserting 10 nodes, I get only 4 nodes #12

Closed OccupyMars2025 closed 5 months ago

OccupyMars2025 commented 5 months ago

buggy code: https://github.com/OccupyMars2025/Introduction-to-Algorithms-4th-Edition/commit/45bfb7e1a9c95c86d9ed23b17965b2a91fd49ec5


tion_generated_by_AI/Chapter_13_Red_Black_Trees/rotation_insertion_deletion$ make python 
rm -rf ./graphviz
# mypy --check-untyped-defs rotation_insertion_deletion.py
python rotation_insertion_deletion.py
Inorder Traversal of the Red-Black Tree:
(20, BLACK)
(63, BLACK)
(67, BLACK)
(91, RED)
OccupyMars2025 commented 5 months ago

solution: https://github.com/OccupyMars2025/Introduction-to-Algorithms-4th-Edition/commit/3eef932a62a0360d240ea415127757f8b313f314

step 1: draw all the "child -> parent" edges , from the generated pictures, I see sth is wrong. (Well, I forget to save the generated pictures)

image

step 2: Based on the above information, check your code step by step

image

step 3: Finally, I find that I didn't replace None with self.nil in the right_rotate method:

        # if y.p is None:  # y is root
        if y.p is self.nil:  # y is root