0% found this document useful (0 votes)
16 views

AVL Trees

A document that provides the explanation of how AVL TREES work in data structures

Uploaded by

niko ferns
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

AVL Trees

A document that provides the explanation of how AVL TREES work in data structures

Uploaded by

niko ferns
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

AVL Trees

Sayan Sikder
SCOPE, VIT University, Vellore
2023
Motivation

So far …
– Binary search trees store linearly ordered data
– Best case height: O(log(n))
– Worst case height: O(n)

Requirement:
– Define and maintain a balance to ensure O(log(n))
height
Binary Search Tree review

• Each node has at most 2 children (left, right)


– Most commonly used form of search tree
– Some examples: Heaps, expression trees, branch-bound
– Input: a set of keys
• Any set with an ordering, but assume numeric keys for simplicity
• For simplicity, also assume all keys are distinct (no duplicates)

• A BST is a tree with the following “ordering” property


– For any node X:
• All keys in the left subtrees < key(X)
• All keys in the right subtree > key(X)
Balanced Binary Search Tree review

• Worst case height of binary search tree: N-1


– Insertion, deletion can be O(N) in the worst case
• We want a tree with small height
• Height of a binary tree with N node is at least (log N)
• Goal: keep the height of a binary search tree O(log N)
• Balanced binary search trees
– Examples: AVL tree, red-black tree
For example for 3 key values you can get 3! BST.
But our search should be minimum search with minimum
height BST so that search is easy.
Balanced Tree?

• Suggestion 1::the left and right subtrees of root have the same
height
– Doesn’t force the tree to be shallow
• Suggestion 2: every node must have left and right subtrees of
the same height
– Only complete binary trees satisfy
– Too rigid to be useful
• Our choice: for each node, the height of the left and right
subtrees can differ at most 1.
• Balance factor: Left subtree-Right subtree = {-1,0,1} should
be maintained at each node.
AVL Trees

• AVL Trees are a form of balanced binary


search trees
• Named after the initials of their inventors
– Adelson-Velskii and Landis
– One of the first to achieve provable guarantee of O(log n)
worst-case for any sequence of insert and delete
operations.
– A binary tree cannot do better
Properties of AVL Trees

• Maximum possible number of nodes in AVL tree of height H = 2H+1 – 1


• Minimum number of nodes in AVL Tree of height H is given by a
recursive relation-
N(H) = N(H-1) + N(H-2) + 1
• Minimum possible height of AVL Tree using N nodes = ⌊log2N⌋
• Maximum height of AVL Tree using N nodes is calculated using recursive
relation-
N(H) = N(H-1) + N(H-2) + 1
Base conditions for this recursive relation are- N(0) = 1, N(1) = 2
• If there are n nodes in AVL Tree, its maximum height can not exceed
1.44log2n.
• In other words, Worst case height of AVL Tree with n nodes = 1.44log2n.
Imbalances and rotations
Insertion

• Basically follows insertion strategy of binary search tree


– But may cause violation of AVL tree property
• Restore the destroyed balance condition if needed

6 8

6
Insert 6 Restore AVL property
Original AVL tree
Property violated
Pros and Cons

Advantages

• The height of the AVL tree is always balanced. The height never grows beyond
log N, where N is the total number of nodes in the tree.
• It gives better search time complexity when compared to simple Binary
Search trees.
• AVL trees have self-balancing capabilities

Disadvantages

• Slow Inserts and Deletes.


• The largest disadvantage to using an AVL tree is the fact that in the event that it
is slightly unbalanced it can severely impact the amount of time that it takes to
perform inserts and deletes.
• Fast Updating Systems.

You might also like