It is quite easy to fully understand standard Binary Search Tree and its operations. Because of that understanding, I even don't need to remember the implementations of those insert, delete, search operations.
I am learning Red-Black Tree now and I understand its properties for keeping the tree balanced. However I feel very hard to understand its insert and delete procedures.
I understand when inserting a new node, we mark the node as red (because red is the best we can do to avoid breaking less Red-Black tree laws). The new red node may still break the "no continuous red nodes law". Then we fix it via:
check its uncle's colour, if red, then mark its parent and uncle as black, and go to grandparent.
if it is right child, left rotate its parent
mark its parent as black and its grandparent as red, then right rotate its grandparent.
done (basically like above).
Many places describes Red-Black tree's insert like above. They just tell you how to do it. But why those steps can fix the tree? Why first left rotate, and then right rotate?
Can anyone explains why to me more clearly, even more clear than CLRS? What's the magic of rotation?
I really wish to understand so after 1 year, I can implement Red-Black tree by myself without review a book.
Thanks
question from:
https://stackoverflow.com/questions/9469858/how-to-easily-remember-red-black-tree-insert-and-delete 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…