Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add AVL tree rotation example image; #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added AVL Tree/Images/RotationStep0.jpg
View file Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[フレーム]
Binary file added AVL Tree/Images/RotationStep1.jpg
View file Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[フレーム]
Binary file added AVL Tree/Images/RotationStep2.jpg
View file Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[フレーム]
Binary file added AVL Tree/Images/RotationStep3.jpg
View file Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[フレーム]
32 changes: 28 additions & 4 deletions AVL Tree/README.markdown
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,44 @@ 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;
Copy link
Member

@kelvinlauKL kelvinlauKL Dec 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo here - "parent not" should be "parent node"

Antondomashnev reacted with thumbs up emoji
* *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

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.

> **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

Expand Down

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