Skip to content

Commit e61e743

Browse files
authored
Merge pull request kodecocodes#319 from Antondomashnev/antondomashnev/avl-tree/rotation
Add AVL tree rotation example image;
2 parents b621f1e + bd645f3 commit e61e743

File tree

5 files changed

+28
-4
lines changed

5 files changed

+28
-4
lines changed

AVL Tree/Images/RotationStep0.jpg

21.7 KB
Loading

AVL Tree/Images/RotationStep1.jpg

12.8 KB
Loading

AVL Tree/Images/RotationStep2.jpg

13 KB
Loading

AVL Tree/Images/RotationStep3.jpg

12.7 KB
Loading

AVL Tree/README.markdown

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,44 @@ The difference between the heights of the left and right subtrees is called the
4343
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.
4444

4545
## Rotations
46-
4746
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.
4847

49-
TODO: describe with pictures how these rotations work
48+
![Rotation0](Images/RotationStep0.jpg)
49+
50+
For the rotation we're using the terminology:
51+
* *Root* - the parent not of the subtrees that will be rotated;
52+
* *Pivot* - the node that will become parent (basically will be on the *Root*'s position) after rotation;
53+
* *RotationSubtree* - subtree of the *Pivot* upon the side of rotation
54+
* *OppositeSubtree* - subtree of the *Pivot* opposite the side of rotation
55+
56+
Let take an example of balancing the unbalanced tree using *Right* (clockwise direction) rotation:
57+
58+
![Rotation1](Images/RotationStep1.jpg) ![Rotation2](Images/RotationStep2.jpg) ![Rotation3](Images/RotationStep3.jpg)
59+
60+
The steps of rotation could be described by following:
61+
62+
1. Assign the *RotationSubtree* as a new *OppositeSubtree* for the *Root*;
63+
2. Assign the *Root* as a new *RotationSubtree* for the *Pivot*;
64+
3. Check the final result
65+
66+
67+
In pseudocode the algorithm above could be written as follows:
68+
```
69+
Root.OS = Pivot.RS
70+
Pivot.RS = Root
71+
Root = Pivot
72+
```
5073

51-
Insertion never needs more than 2 rotations. Removal might require up to *log(n)* rotations.
74+
This is a constant time operation - __O(1)__
75+
Insertion never needs more than 2 rotations. Removal might require up to __log(n)__ rotations.
5276

5377
## The code
5478

5579
Most of the code in [AVLTree.swift](AVLTree.swift) is just regular [binary search tree](../Binary Search Tree/) stuff. You'll find this in any implementation of a binary search tree. For example, searching the tree is exactly the same. The only things that an AVL tree does slightly differently are inserting and deleting the nodes.
5680

5781
> **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.
5882
59-
The interesting bits are in the `balance()` method which is called after inserting or deleting a node.
83+
The interesting bits are in the `balance()` method which is called after inserting or deleting a node.
6084

6185
## See also
6286

0 commit comments

Comments
 (0)