Files
dsa-tutoring/notes-and-examples/2026.07.01/overview.md
2026-07-01 23:13:22 -04:00

1.5 KiB

Outline

  • Review tree rotations and red-black trees
  • Properties of a red-black tree
    • Top (root) node is black
    • The children and parent of a red node are black
    • Null nodes are colored black
    • The path from any particular node to a null node must contain the same number of black nodes
  • Dive into the steps for rebalancing a red-black tree

Also see the accompanying whiteboard pictures.

Rotations

Red-black trees

Assignment

Clarifications on the red-black tree example:

  • In your assignment, you should first color a node red when inserting it.
    • Then, check for red-red violations (the second rule). Then, rebalance if necessary.
  • If the uncle of the inserted node (parent → parent → right child) is red, you'll want to do two things:
    • Color the parent, uncle, and grandparent in a way which preserves all the rules (which ones are colored which, are an exercise left to you)
    • Recursively run the rebalancing algorithm on the grandparent
  • Otherwise, your task is to figure out the correct conditions in which each of the rebalancing algorithms apply.

Copy 2026.07.01/homework.py into your own Git repository, with a new folder for the date. Implement the rebalance_from_just_inserted function.

Don't change the test cases, but use them to inform how you implement your code. Try to pass all the test cases.

Follow the instructions from last session (2026.06.12) to commit and push your changes to Gitea.