feat: cleaned up formatting, spelling, grammar (#36087)

This commit is contained in:
Christopher McCormack
2019-06-28 00:25:49 -07:00
committed by Randell Dawson
parent 882a4b5263
commit 49dba25225

View File

@ -1,35 +1,43 @@
--- ---
title: AVL Trees title: AVL Trees
--- ---
## AVL Trees # AVL Trees
An AVL tree is a subtype of binary search tree. Named after its inventors Adelson, Velskii and Landis, AVL trees have the property of dynamic self-balancing in addition to all the properties exhibited by Binary Search Trees (BSTs).
An AVL tree is a subtype of binary search tree. Named after it's inventors Adelson, Velskii and Landis, AVL trees have the property of dynamic self-balancing in addition to all the properties exhibited by binary search trees.
A BST is a data structure composed of nodes. It has the following guarantees: A BST is a data structure composed of nodes. It has the following guarantees:
- Each tree has a root node (at the top).
- The root node has zero, one or two child nodes.
- Each child node has zero, one or two child nodes, and so on.
- For each node, the value of its left descendants are less than the current node, which is less than the value of the right descendants.
1. Each tree has a root node (at the top). AVL trees have some additional guarantees:
2. The root node has zero, one or two child nodes. - The difference between the depth of right and left sub-trees cannot be more than one. In order to maintain this guarantee, an implementation of an AVL will include an algorithm to rebalance the tree when adding an additional element would upset this guarantee.
3. Each child node has zero, one or two child nodes, and so on. - The height of an AVL tree is always `log(n)` where `n` is the number of nodes in the tree.
4. Each node has up to two children.
5. For each node, its left descendents are less than the current node, which is less than the right descendents.
AVL trees have an additional guarantee: AVL trees have a worst case lookup, insert, and delete time of `O(log(n))`.
6. The difference between the depth of right and left subtrees cannot be more than one. In order to maintain this guarantee, an implementation of an AVL will include an algorithm to rebalance the tree when adding an additional element would upset this guarantee.
AVL trees have a worst case lookup, insert and delete time of O(log n). ## AVL Tree Rotations
In an AVL tree, after performing an operation like insertion or deletion, we need to check the balance factor of every node in the tree. If every node satisfies the balance factor condition, then we conclude the operation; otherwise, we must make it balanced.
### Right Rotation ### Single Right Rotation (RR Rotation)
In RR Rotation every node moves one position to right from the current position.
![AVL Tree Right Rotation](https://raw.githubusercontent.com/HebleV/valet_parking/master/images/avl_right_rotation.jpg) ![AVL Tree Right Rotation](https://raw.githubusercontent.com/HebleV/valet_parking/master/images/avl_right_rotation.jpg)
### Left Rotation ### Single Left Rotation (LL Rotation)
In LL Rotation every node moves one position to left from the current position.
![AVL Tree Left Rotation](https://raw.githubusercontent.com/HebleV/valet_parking/master/images/avl_left_rotation.jpg) ![AVL Tree Left Rotation](https://raw.githubusercontent.com/HebleV/valet_parking/master/images/avl_left_rotation.jpg)
### AVL Insertion Process ### Left Right Rotation (LR 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.
You will do an insertion similar to a normal Binary Search Tree insertion. After inserting, you fix the AVL property using left or right rotations. ### Right Left Rotation (RL 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.
## AVL Insertion Process
Insertion in an AVL Tree are similar to a normal Binary Search Tree insertion. After inserting, you fix the AVL property using left or right rotations.
- If there is an imbalance in left child of right subtree, then you perform a left-right rotation. - If there is an imbalance in left child of right subtree, then you perform a left-right rotation.
- If there is an imbalance in left child of left subtree, then you perform a right rotation. - If there is an imbalance in left child of left subtree, then you perform a right rotation.
@ -37,42 +45,19 @@ You will do an insertion similar to a normal Binary Search Tree insertion. After
- If there is an imbalance in right child of left subtree, then you perform a right-left rotation. - If there is an imbalance in right child of left subtree, then you perform a right-left rotation.
#### More Information: ## Time Analysis Of AVL Tree
[YouTube - AVL Tree](https://www.youtube.com/watch?v=7m94k2Qhg68)
[AVL Tree Wiki](https://en.wikipedia.org/wiki/AVL_tree)
An AVL tree is a self-balancing binary search tree.
An AVL tree is a binary search tree which has the following properties:
->The sub-trees of every node differ in height by at most one.
->Every sub-tree is an AVL tree.
AVL tree checks the height of the left and the right sub-trees and assures that the difference is not more than 1. This difference is called the Balance Factor.
The height of an AVL tree is always O(Logn) where n is the number of nodes in the tree.
AVL Tree Rotations:-
In AVL tree, after performing every operation like insertion and deletion we need to check the balance factor of every node in the tree. If every node satisfies the balance factor condition then we conclude the operation otherwise we must make it balanced. We use rotation operations to make the tree balanced whenever the tree is becoming imbalanced due to any operation.
Rotation operations are used to make a tree balanced.There are four rotations and they are classified into two types:
->Single Left Rotation (LL Rotation)
In LL Rotation every node moves one position to left from the current position.
->Single Right Rotation (RR Rotation)
In RR Rotation every node moves one position to right from the current position.
->Left Right Rotation (LR 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.
->Right Left Rotation (RL 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.
Time Analysis Of AVL Tree:
AVL tree is binary search tree with additional property that difference between height of left sub-tree and right sub-tree of any node cant be more than 1. AVL tree is binary search tree with additional property that difference between height of left sub-tree and right sub-tree of any node cant be more than 1.
Algorithm Average Worst case | Algorithm | Average | Worst case |
Space O ( n ) {\displaystyle O(n)} O(n) O ( n ) {\displaystyle O(n)} O(n) | --------- | :-----: | :--------: |
Search O ( log n ) {\displaystyle O(\log n)} O(\log n) O ( log n ) {\displaystyle O(\log n)} O(\log n) | Space | `O(n)` | `O(n)` |
Insert O ( log n ) {\displaystyle O(\log n)} O(\log n) O ( log n ) {\displaystyle O(\log n)} O(\log n) | Search | `O(log(n))` | `O(log(n))` |
Delete O ( log n ) {\displaystyle O(\log n)} O(\log n) O ( log n ) {\displaystyle O(\log n)} O(\log n) | Insert | `O(log(n))` | `O(log(n))` |
| Delete | `O(log(n))` | `O(log(n))` |
Application of AVL Trees ## Application of AVL Trees
One beneficial use case for an AVL Tree is when you are designing a database where insertions and deletions are not performed frequently but often require look-ups
AVL trees are beneficial in the cases where you are designing some database where insertions and deletions are not that frequent but you have to frequently look-up for the items present in there. ## Additional Resources
- [YouTube - AVL Tree](https://www.youtube.com/watch?v=7m94k2Qhg68)
- [Wikipedia - AVL Tree](https://en.wikipedia.org/wiki/AVL_tree)