主观题:Top-down Insertion for RB Tree
这是一个主观题模板。请在这里写题目描述。
答案:As we move from top down, whenever we see a black node X with two red children, simply flip their colors.
If X has a red parent P, then it turns into Case 2 or 3 of the bottom-up solution, which can be solved by a simple rotation.
Why is Case 1 (X's parent's sibling is also red) impossible? Because if so, X's grandparent must have two red children, and must have been re-colored.
答案:As we move from top down, whenever we see a black node X with two red children, simply flip their colors.
If X has a red parent P, then it turns into Case 2 or 3 of the bottom-up solution, which can be solved by a simple rotation.
Why is Case 1 (X's parent's sibling is also red) impossible? Because if so, X's grandparent must have two red children, and must have been re-colored.