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

Heaps_Revised

The document discusses heaps, a type of binary tree that maintains specific relational and structural properties, including the organization of keys and the complete filling of levels. It covers heap algorithms, including insertion, deletion, and heap sort, as well as the implementation of priority queues using heaps. The overall time complexity for heap operations is O(log n), making it an efficient data structure for various applications such as Dijkstra's algorithm and job scheduling.

Uploaded by

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

Heaps_Revised

The document discusses heaps, a type of binary tree that maintains specific relational and structural properties, including the organization of keys and the complete filling of levels. It covers heap algorithms, including insertion, deletion, and heap sort, as well as the implementation of priority queues using heaps. The overall time complexity for heap operations is O(log n), making it an efficient data structure for various applications such as Dijkstra's algorithm and job scheduling.

Uploaded by

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

11.

Trees

Dr. Swati Agarwal


Agenda

1 Heaps

2 Array representation of a heap

3 Heap Algorithms

4 Heap Sort
Priority Queue Implementation

December 24, 2020 Dr. Swati Agarwal 11. Trees 2/23


1. Heaps

A binary tree T that stores a collection of keys at its internal


nodes and satisfies the two additional properties:
1. A relational property defined in terms of the way keys are
stored in T .
2. A structural property that defines the T itself
We assume that a total order relation on the keys is given
(Antisymmetry, Transitivity, Connexity).
External nodes do not contain any key or element.

December 24, 2020 Dr. Swati Agarwal 11. Trees 3/23


1.1 Relational Property

In Heap T , the key stored at any intermediate node v (other


than the ROOT node) is always greater than or equal to its
parent (or always lesser than or equal to its parent).
As a consequence, a path from the root to a leaf node always
has keys in non-decreasing order (or decreasing).
Root has the minimum key (or maximum key).
We further want Tree to have as small height as possible.

December 24, 2020 Dr. Swati Agarwal 11. Trees 4/23


1.2 Structural Property

Heap is a complete binary tree in structure.


A binary tree is a complete binary tree if in every level, except
possibly the deepest, is completely filled. At depth n, the
height of the tree, all nodes must be as far left as possible.
There is a special node called last node- right most deepest
node
A heap storing n keys has height dlog (n + 1)e

We assume that a total order relation on the keys is given.

December 24, 2020 Dr. Swati Agarwal 11. Trees 5/23


Example

December 24, 2020 Dr. Swati Agarwal 11. Trees 6/23


Array Implementation of Heap

In binary heap, each node may have up to two children.


Since heaps are forming complete binary tree there will not be
any wastage of memory space/locations.
For a node at i th location, it’s parent is at 2i location.
 

Example: parent of 3 is at 32 = 1 position.


For a node at i th location, it’s children will be at 2 ∗ i and
2 ∗ i + 1 locations. Example: children of 3 are at 3 ∗ 2 = 6
and 3 ∗ 2 + 1 = 7 locations.

1 2 3 4 5 6 7

December 24, 2020 Dr. Swati Agarwal 11. Trees 7/23


Insertion Operation

1. Add the element to the bottom level of the heap at the most
left.
2. Compare the added element with its parent; if they are in the
correct order, stop.
3. If not, swap the element with its parent and return to the
previous step.

December 24, 2020 Dr. Swati Agarwal 11. Trees 8/23


Deletion Operation aka Extract

1. Always starts from root node


2. Replace the root of the heap with the last element on the last
level.
3. Compare the new root with its children; if they are in the
correct order, stop.
4. If not, swap the element with one of its children and return to
the previous step. (Swap with its smaller child in a min-heap
and its larger child in a max-heap.)

December 24, 2020 Dr. Swati Agarwal 11. Trees 9/23


Heap Algorithms
Heap Algorithms: Basic
Parent (A , i )
1. if i == 1
2. return NULL
2. return 2i
 

Left (A , i )
1. if 2 * i ≤ heap - size [ A ]
2. return 2 * i
3. else
4. return NULL

Right (A , i )
1. if 2 * i + 1 ≤ heap - size [ A ]
2. return 2 * i + 1
3. else
4. return NULL

December 24, 2020 Dr. Swati Agarwal 11. Trees 11/23


Heapifying an Element
Max - Heapify (A , i )
1. l ← Left (A , i )
2. r ← Right (A , i )
3. if l ≤ heap - size [ A ] and A [ l ] > A [ i ]
4. max ← l
5. else
6. max ← i
7. if r ≤ heap - size [ A ] and A [ r ] < A [ max ]
8. max ← r
9. if max 6= i
10. exchange A [ i ] and A [ max ]
11. Max - Heapify (A , max )

December 24, 2020 Dr. Swati Agarwal 11. Trees 12/23


MAX HEAP
Build - Max - Heap ( A )
1. heap - size [ A ] ← length [ A ]
j k
2. for i ← length[A] 2
downto 1
3. Max - Heapify (A , i )

Heap - Extract - Max ( A )


1. max ← A [1]
2. A [1] ← A [ heap - size [ A ]]
3. heap - size [ A ] ← heap - size [ A ] - 1
4. Max - Heapify (A , 1)
5. return max

December 24, 2020 Dr. Swati Agarwal 11. Trees 13/23


removeMin in a heap

opposite of remove max in heap


First copy the key in the last node to root node
Now change the last node as external node
Reassign the last node
It might violate the heap-order property
Down-heap bubbling to resolve this issue
removeMin method takes O(logn) time

December 24, 2020 Dr. Swati Agarwal 11. Trees 14/23


Heap-sort

First we have to insert n items and it will take O(nlogn) time


Then we have to removeMin/removeMax (based on the
required output ordering) n times and it will take O(nlogn)
time
So overall running time for heap-sort is O(nlogn)

Heap - Sort ( A )
1. Build - Max - Heap ( A )
2. for i = length [ A ] down to 2
3. exchange A [1] and A [ i ]
4. heap - size [ A ] ← heap - size [ A ] - 1
5. Max - Heapify (A , 1)

December 24, 2020 Dr. Swati Agarwal 11. Trees 15/23


Heap Sort

8 3 6
6 4 6 4 3 4
5 1 3 5 1 5 1
6 1 5
5 4 5 4 1 4
3 1 3 3
5 1 4 and so on...

3 4 3 4 3 1
1
Output: 8, 6, 5, 4, 3, 1

December 24, 2020 Dr. Swati Agarwal 11. Trees 16/23


Priority Queue ADT
Priority Queue ADT

A priority queue is a data structure that supports two basic


operations: Insert and Remove Max or Remove Min which
returns the element with maximum and minimum key
(priority) respectively.

Key
item
Data

December 24, 2020 Dr. Swati Agarwal 11. Trees 18/23


Priority Queue Applications

Shortest Path computation (Dijkstra’s algorithm)


Minimum Spanning Tree (Prim’s algorithm)
Event Driven Simulation: (customer’s in a queue, job
scheduling)
Selection problem: finding k th smallest/greatest element

December 24, 2020 Dr. Swati Agarwal 11. Trees 19/23


Priority Queue Implementation

Unordered Array/List
• Insert operation = O(1)
• Remove Max or Remove Min operation: O(n)
Ordered Array/List
• Insert operation = O(n)
• Remove Max or Remove Min operation: O(1)

We, however, do not need any operation to cost O(n) and rather
need more balanced approach where both insertion and
remove max (or min) operation takes O(log n) time.

December 24, 2020 Dr. Swati Agarwal 11. Trees 20/23


Priority Queue Implementation: Heap Tree

Both the above implementations use linear data structure


A nonlinear data structure to implement the priority queue in
an efficient way?
In heap implementation insert and removeMin methods take
O(log n) time
From a given collection C insert element into the Priority
Queue Q
Use removeMin on Q and store it in C

December 24, 2020 Dr. Swati Agarwal 11. Trees 21/23


Priority Queue using Heap
Input: 8 6 4 5 1 2
Insert 10: 8 6 4 5 1 2 10
j k
loc[10]
Parent of 10: 2 = data[3] = 4
Heapify: 8 6 10 5 1 2 4
j k
loc[10]
Parent of 10: 2 = data[1] = 8
Heapify: 10 6 8 5 1 2 4
Swaps take constant time. Total number of swaps = O(log n).
Equal to the height of the heap tree.

December 24, 2020 Dr. Swati Agarwal 11. Trees 22/23


Priority Queue using Heap
10 6 8 5 1 2 4

December 24, 2020 Dr. Swati Agarwal 11. Trees 23/23


Priority Queue using Heap
10 6 8 5 1 2 4
Remove Max and replace with last entry:
4 6 8 5 1 2

December 24, 2020 Dr. Swati Agarwal 11. Trees 23/23


Priority Queue using Heap
10 6 8 5 1 2 4
Remove Max and replace with last entry:
4 6 8 5 1 2 Children of node 4 (data[1]): data[2],
data [3] i.e. 6 and 8
right[root] > left[root]
swap root with right child. 8 6 4 5 1 2

December 24, 2020 Dr. Swati Agarwal 11. Trees 23/23


Priority Queue using Heap
10 6 8 5 1 2 4
Remove Max and replace with last entry:
4 6 8 5 1 2 Children of node 4 (data[1]): data[2],
data [3] i.e. 6 and 8
right[root] > left[root]
swap root with right child. 8 6 4 5 1 2
Remove Max and replace with last entry
2 6 4 5 1

December 24, 2020 Dr. Swati Agarwal 11. Trees 23/23


Priority Queue using Heap
10 6 8 5 1 2 4
Remove Max and replace with last entry:
4 6 8 5 1 2 Children of node 4 (data[1]): data[2],
data [3] i.e. 6 and 8
right[root] > left[root]
swap root with right child. 8 6 4 5 1 2
Remove Max and replace with last entry
2 6 4 5 1
Children of node 2 (data[1]): data[2], data [3] i.e. 6 and 4
left[root] > right[root]
swap root with left child: 6 2 4 5 1

December 24, 2020 Dr. Swati Agarwal 11. Trees 23/23


Priority Queue using Heap
10 6 8 5 1 2 4
Remove Max and replace with last entry:
4 6 8 5 1 2 Children of node 4 (data[1]): data[2],
data [3] i.e. 6 and 8
right[root] > left[root]
swap root with right child. 8 6 4 5 1 2
Remove Max and replace with last entry
2 6 4 5 1
Children of node 2 (data[1]): data[2], data [3] i.e. 6 and 4
left[root] > right[root]
swap root with left child: 6 2 4 5 1
Repeat the step for new position of 2

December 24, 2020 Dr. Swati Agarwal 11. Trees 23/23

You might also like