Skip to content
This repository has been archived by the owner on Aug 28, 2019. It is now read-only.

Algorithm: Add AVL Tree Article #868

Closed
wants to merge 1 commit into from
Closed
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
36 changes: 31 additions & 5 deletions src/pages/algorithms/avl-trees/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,39 @@ title: Avl Trees
---
## Avl Trees

This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/algorithms/avl-trees/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.
In computer science, an AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of
left and right subtrees cannot be more than one for all nodes. This difference is called the Balance Factor.

<a href='https://github.com/freecodecamp/guides/blob/master/README.md' target='_blank' rel='nofollow'>This quick style guide will help ensure your pull request gets accepted</a>.
Example of AVL tree:
![Example of AVL tree](https://upload.wikimedia.org/wikipedia/commons/thumb/a/ad/AVL-tree-wBalance_K.svg/1054px-AVL-tree-wBalance_K.svg.png)

<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
The above tree is AVL because differences between heights of left and right subtrees for every node is less than or equal to 1.
###Why AVL Trees?
Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take O(h) time where h is the height of the BST.
If we make sure that height of the tree remains O(Logn) after every insertion and deletion,
then we can guarantee an upper bound of O(Logn) for all these operations. The height of an AVL tree is always O(Logn)
where n is the number of nodes in the tree.

#### More Information:
<!-- Please add any articles you think might be helpful to read before writing the article -->
###AVL Rotations
To balance itself, an AVL tree may perform the following four kinds of rotations:
* Left rotation
* Right rotation
* Left-Right rotation
* Right-Left rotation

The first two rotations are single rotations and the next two rotations are double rotations.
####Left Rotation
If a tree becomes unbalanced, when a node is inserted into the right subtree of the right subtree, then we perform a single left rotation
![Left Rotation](https://www.tutorialspoint.com/data_structures_algorithms/images/avl_left_rotation.jpg)
####Right Rotation
AVL tree may become unbalanced, if a node is inserted in the left subtree of the left subtree. The tree then needs a right rotation.
![Right Rotation](https://www.tutorialspoint.com/data_structures_algorithms/images/avl_right_rotation.jpg)
####Left-Right Rotation
The LR Rotation is combination of single left rotation followed by single right rotation.
In LR Rotation, first every node moves one position to left then one position to right from the current position.
![Left-Right-Rotation](http://btechsmartclass.com/DS/images/LR%20Rotation.png)

####Right-Left Rotation
The RL Rotation is combination of single right rotation followed by single left rotation.
In RL Rotation, first every node moves one position to right then one position to left from the current position.
![Right-Left-Rotation](http://btechsmartclass.com/DS/images/RL%20Rotation.png)