I am reading about AVL trees in Data structures and algorithms by Mark Allen Wesis
Suppose the node to be rebalanced is X. There are 4 cases that we might have to fix (two are the mirror images of the other two): An insertion in the left subtree of the left child of X, An insertion in the开发者_开发问答 right subtree of the left child of X, An insertion in the left subtree of the right child of X, or An insertion in the right subtree of the right child of X.
Balance is restored by tree rotations.
Following are questions i have on above text snippet.
- What does author mean by mirror images of other two?
- What is symmetric case in single rotation and double rotation?
Thanks
Suppose the node being inserted is I. The book says there are 4 cases. Let's take the one where I is the left child of the left child of X:
X
/ \
? ?
/ \ / \
I ? ? ?
The "mirror" of this is when I is the right child of the right child of X:
X
/ \
? ?
/ \ / \
? ? ? I
The reason this is a "mirror" is that the rotations you have to do for both cases are the same, just with left and right reversed. The same goes for the other two cases where I is the left child of the right child of X and where I is the right child of the left child of X.
As for your second question, the idea is the same. In the symmetric case, (ie the mirror case), you do the same rotations, just with left and right reversed.
精彩评论