diff --git a/AVL Tree/Images/RotationStep0.jpg b/AVL Tree/Images/RotationStep0.jpg new file mode 100644 index 000000000..f61f8804a Binary files /dev/null and b/AVL Tree/Images/RotationStep0.jpg differ diff --git a/AVL Tree/Images/RotationStep1.jpg b/AVL Tree/Images/RotationStep1.jpg new file mode 100644 index 000000000..953d35929 Binary files /dev/null and b/AVL Tree/Images/RotationStep1.jpg differ diff --git a/AVL Tree/Images/RotationStep2.jpg b/AVL Tree/Images/RotationStep2.jpg new file mode 100644 index 000000000..410c89536 Binary files /dev/null and b/AVL Tree/Images/RotationStep2.jpg differ diff --git a/AVL Tree/Images/RotationStep3.jpg b/AVL Tree/Images/RotationStep3.jpg new file mode 100644 index 000000000..921c0c4ba Binary files /dev/null and b/AVL Tree/Images/RotationStep3.jpg differ diff --git a/AVL Tree/README.markdown b/AVL Tree/README.markdown index 159f9e1dc..355204587 100644 --- a/AVL Tree/README.markdown +++ b/AVL Tree/README.markdown @@ -43,12 +43,36 @@ The difference between the heights of the left and right subtrees is called the If after an insertion or deletion the balance factor becomes greater than 1, then we need to re-balance this part of the AVL tree. And that is done with rotations. ## Rotations - Each tree node keeps track of its current balance factor in a variable. After inserting a new node, we need to update the balance factor of its parent node. If that balance factor becomes greater than 1, we "rotate" part of that tree to restore the balance. -TODO: describe with pictures how these rotations work +![Rotation0](Images/RotationStep0.jpg) + +For the rotation we're using the terminology: +* *Root* - the parent not of the subtrees that will be rotated; +* *Pivot* - the node that will become parent (basically will be on the *Root*'s position) after rotation; +* *RotationSubtree* - subtree of the *Pivot* upon the side of rotation +* *OppositeSubtree* - subtree of the *Pivot* opposite the side of rotation + +Let take an example of balancing the unbalanced tree using *Right* (clockwise direction) rotation: + +![Rotation1](Images/RotationStep1.jpg) ![Rotation2](Images/RotationStep2.jpg) ![Rotation3](Images/RotationStep3.jpg) + +The steps of rotation could be described by following: + +1. Assign the *RotationSubtree* as a new *OppositeSubtree* for the *Root*; +2. Assign the *Root* as a new *RotationSubtree* for the *Pivot*; +3. Check the final result + + +In pseudocode the algorithm above could be written as follows: +``` +Root.OS = Pivot.RS +Pivot.RS = Root +Root = Pivot +``` -Insertion never needs more than 2 rotations. Removal might require up to *log(n)* rotations. +This is a constant time operation - __O(1)__ +Insertion never needs more than 2 rotations. Removal might require up to __log(n)__ rotations. ## The code @@ -56,7 +80,7 @@ Most of the code in [AVLTree.swift](AVLTree.swift) is just regular [binary searc > **Note:** If you're a bit fuzzy on the regular operations of a binary search tree, I suggest you [catch up on those first](../Binary Search Tree/). It will make the rest of the AVL tree easier to understand. -The interesting bits are in the `balance()` method which is called after inserting or deleting a node. +The interesting bits are in the `balance()` method which is called after inserting or deleting a node. ## See also

AltStyle によって変換されたページ (->オリジナル) /