0% found this document useful (0 votes)
37 views47 pages

unit 4 AVL trees

Uploaded by

pk6048
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views47 pages

unit 4 AVL trees

Uploaded by

pk6048
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

21CSC201J

DATA STRUCTURES AND ALGORITHMS

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

BalanceFactor = height(left-sutree) − height(right-sutree)


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 −
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.
Left-Right Rotation
A left-right rotation is a
combination of left
rotation followed by
right rotation.
Right-Left Rotation
The second type of double
rotation is Right-Left Rotation.
It is a combination of right
rotation followed by left
rotation.
heaps
Heaps
• A third type of tree is a heap.
• A heap is a binary tree whose left and right subtrees have values less
than their parents.
• The root of a maxheap is guaranteed to hold the largest node in the
tree; its subtrees contain data that have lesser values.
• Unlike the binary search tree, however, the lesser-valued nodes of a
heap can be placed on either the right or the left subtree.
• Heaps have another interesting facet: they are often implemented in
an array rather than a linked list.
Definition

• A heap, as shown in Figure


9-1, is a binary tree
structure with the following
• properties:
• 1. The tree is complete or
nearly complete.
• 2. The key value of each
node is greater than or A heap is a complete or nearly complete
equal to the key value in binary tree in which the key value in a node
is greater than or
each of its descendants. equal to the key values in all of its subtrees,
and the subtrees are in turn heaps.
Heap Operations
• Two basic maintenance operations are performed on a heap:
• insert a node
• and delete a node.

• 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:

Step 0: Change the Array Data Structure into a Tree


In order to change an array structure into the tree version of the binary heap, we start from the
left to the right of the array, and then insert values into the binary heap from top to bottom
and left to right.
• Step 1: Delete the node that contains the
value you want deleted in the heap. Step 2: Replace the deleted node with the
farthest right node.
The value that we want to delete is the
maximum value or element in the array
which is at the root of the tree. This is the
node that contains the value “10”.
Step 3: Heapify (Fix the heap):

• 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

• max_heapify has complexity O(logN),


• build_maxheap has complexity O(N) and we run
max_heapify N−1 times in heap_sort function, therefore complexity
of heap_sort function is O(NlogN).
Heap Applications
Three common applications of heaps
• Three common applications of heaps are
• selection algorithms,
• Priority queues, and
• sorting.
Selection Algorithms
• There are two solutions to the problem of determining the kth element in
an
• unsorted list.
• We could first sort the list and select the element at location k, or we could
create a heap and delete k – 1 elements from it, leaving the desired
element at the root.
• Rather than simply discard the elements at the top of the heap, a better
solution is to place the deleted element at the end of the heap and reduce
the heap size by 1.
• After the kth element has been processed, the temporarily removed
elements can then be reinserted into the heap.
For example, if we want to know the fourth-largest
element in a list, we can create the heap shown in Figure
9-14. After deleting three times, we have the fourth-largest
element, 21, at the top of the heap.
After selecting 21 we re-heap to restore the heap so that it
is complete and we are ready for another selection.
Priority Queues
• The heap is an excellent structure to use for a priority queue.
• As an event enters the queue, it is assigned a priority number that
determines its position relative to the other events already in the
queue.
• It is assigned a priority number even though the new event can enter
the heap in only one place at any given time, the first empty leaf.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy