You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: AVL Tree/README.markdown
+28-4Lines changed: 28 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -43,20 +43,44 @@ The difference between the heights of the left and right subtrees is called the
43
43
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.
44
44
45
45
## Rotations
46
-
47
46
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.
48
47
49
-
TODO: describe with pictures how these rotations work
48
+

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:
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
+
```
50
73
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.
52
76
53
77
## The code
54
78
55
79
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.
56
80
57
81
> **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.
58
82
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.
0 commit comments