unit 4 AVL trees
unit 4 AVL trees
UNIT-4
Topic : AVL Trees
Adelson, Velski & Landis, AVL
trees
Adelson, Velski & Landis, AVL trees
• It is observed that BST's worst-case performance is closest to linear
search algorithms, that is Ο(n). In real-time data, we cannot predict
data pattern and their frequencies. So, a need arises to balance out
the existing BST.
• Named after their inventor Adelson, Velski & Landis, AVL trees are
height balancing binary search 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.
Balance Factor
• 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 −
Right Rotation
• To implement the insert and delete operations, we need two basic algorithms:
reheap up and reheap down.
Reheap Up
• The reheap up operation reorders a “broken” heap by floating the
last element up the tree until it is in its correct location in the heap.
Heap Implementation
• Although a heap can be built in a dynamic tree structure, it is most often implemented in
an array. This implementation is possible because the heap is, by definition, complete or
nearly complete. Therefore, the relationship
• between a node and its children is fixed and can be calculated as shown below.
• 1. For a node located at index i, its children are found at:
• a. Left child: 2i + 1
• b. Right child: 2i + 2
• 2. The parent of a node located at index i is located at [(i – 1) / 2]
• 3. Given the index for a left child, j, its right sibling, if any, is found at j + 1.
• Conversely, given the index for a right child, k, its left sibling, which must exist, is found
at k – 1
• 4. Given the size, n, of a complete heap, the location of the first
leaf is [(n / 2)]
• Given the location of the first leaf element, the location of the
last non leaf
• element is one less.
parent= (i-1)/2
parent of 40, i=4
parent=3/2=1= 75
A heap can be implemented in an
array because it must be a
complete or nearly complete
binary tree, which allows a fixed
relationship between each node
and its children.
Max Heap Construction Algorithm
Example :max heap. Insert a new node with value 85.
• Step 1 - Insert the newNode with value 85
as last leaf from left to right. That means •Step 2 - Compare newNode value
newNode is added as a right child of node with (85) with its Parent node value (75).
value 75. After adding max heap is as follows... That means 85 > 75
• Step 3 - Here newNode value (85) is •Step 4 - Now, again compare new Node
greater than its parent value (75), value (85) with its parent node value
then swap both of them. After (89).
swapping, max heap is as follows...
• Here, newNode value (85) is smaller than its parent node value
(89). So, we stop insertion process. Finally, max heap after
insertion of a new node with value 85 is as follows...
Max Heap Deletion Algorithm
Problem Example:
• The value “7” at the root of the tree is less than both of its children,
the nodes containing the value “8” and “9”. We need to swap the
“7” with the largest child, the node containing the value “9”.
Heap sort
• Heaps can be used in sorting an array.
• In max-heaps, maximum element will always be at the root. Heap Sort
uses this property of heap to sort the array.
• Consider an array Arr which is to be sorted using Heap Sort.
• 1. Initially build a max heap of elements in Arr.
• 2. The root element, that is Arr[1], will contain maximum element
of Arr. After that, swap this element with the last element of Arr and
heapify the max heap excluding the last element which is already in its
correct position and then decrease the length of heap by one.
• 3. Repeat the step 2, until all the elements are in their correct position.
Heap sort-complexity