42 lines
1.5 KiB
Markdown
42 lines
1.5 KiB
Markdown
## 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.
|
|
|
|

|
|
|
|

|
|
|
|
## 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](https://gitea.bchen.dev/ethan/dsa-homework),
|
|
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.
|
|
|