SlideShare une entreprise Scribd logo
1  sur  38
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Heap Data Structure
Zahoor Jan
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Heaps
A (binary) heap data structure is an array object that can be
viewed as a complete binary tree
Each node of the tree corresponds to an element of the array that
stores the value in the node
A complete binary tree is one where all the internal nodes have
exactly 2 children, and all the leaves are on the same level.
The tree is completely filled on all levels except possibly the
lowest, which is filled from left to right up to a point
Leaf nodes are nodes without children.
 Leaf node
 Interior node
Edge

Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Heaps
Height of a node: The number of edges starting at that node, and going
down to the furthest leaf.
Height of the heap: The maximum number of edges from the root to a leaf.
 Root
Height of blue
node = 1
Height of root = 3
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Heaps
Complete binary tree– if not full, then the only
unfilled level is filled in from left to right.
2 3
4 5 6 7
8 9 10 11 12 13
1
2 3
4 5 6
127 8 9 10 11
1
Parent(i)
return i/2
Left(i)
return 2i
Right(i)
return 2i + 1
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Heaps
The heap property of a tree is a condition that must be true for
the tree to be considered a heap.
Min-heap property: for min-heaps, requires
A[parent(i)] ≤ A[i]
So, the root of any sub-tree holds the least value in that sub-tree.
Max-heap property: for max-heaps, requires
A[parent(i)] ≥ A[i]
The root of any sub-tree holds the greatest value in the sub-tree.
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Heaps
Looks like a max heap, but max-heap property is violated at
indices 11 and 12. Why?
because those nodes’ parents have smaller values than their
children.
2
8
3
8
4
8
5
7
6
8
7
1
8
6
9
7
10
5
11
8
12
9
1
9
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Heaps
Assume we are given a tree represented by a linear array A, and
such that i ≤ length[A], and the trees rooted at Left(i) and
Right(i) are heaps. Then, to create a heap rooted at A[i], use
procedure Max-Heapify(A,i):
Max-Heapify(A,i)
1. l  Left(i)
2. r  Right(i)
3. if l ≤ heap-size[A] and A[l] > A[i]
4. then largest  l
5. else largest  i
6. if r ≤ heap-size[A] and A[r] > A[largest]
7. then largest  r
8. if largest ≠ i
9. then swap( A[i], A[largest])
Max-Heapify(A, largest)
20
9
19 18
6 7
17 16 17 16 1
10
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Heaps
If either child of A[i] is greater than A[i], the greatest child
is exchanged with A[i]. So, A[i] moves down in the heap.
The move of A[i] may have caused a violation of the max-
heap property at it’s new location.
So, we must recursively call Max-Heapify(A,i) at the
location i where the node “lands”.
The above is the top-down approach of Max-Heapify(A,i)
20
9
19 18
6 7
17 16 17 16 1
10
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Max-Heapify(A,1)
20 9
19 18 6 7
17 16 17 16 1
10
10 9
19 18 6 7
17 16 17 16 1
20
10 9
19 18 6 7
17 16 17 16 1
20
19 9
10 18 6 7
17 16 17 16 1
20
19 9
10 18 6 7
17 16 17 16 1
20
19 9
17 18 6 7
10 16 17 16 1
20
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Run Time of Max-Heapify()
Run time of Max-Heapify().
How many nodes, in terms of n, is in the sub-tree with the most nodes?
2 n / 3
So, worst case would follow a path that included the most nodes
T(n) = T(2n/3) + Θ(1)
Why Θ(1)? Note that all the work in lines 1-7 is simple assignments or
comparisons, so they are constant time, or Θ(1).
2
1
2 3
4 5 6 7
8 9 10 11
1
2 3
4 5
1
2(2) / 3 = 2
1 ≤ 2 2(5) / 3 = 3
3 ≤ 3 2(11) / 3 = 8
7 ≤ 8
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Heaps
So, what must we do to build a heap?
We call Max-Heapify(A,i) for every i starting at last node
and going to the root.
Why Bottom-up?
Because Max-Heapify() moves the larger node upward
into the root. If we start at the top and go to larger
node indices, the highest a node can move is to the root
of that sub-tree. But what if there is a violation between
the sub-tree’s root and its parent?
So, we must start from the bottom and go towards the
root to fix the problems caused by moving larger nodes
into the root of sub-trees.
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Heaps
Bad-Build-Max-Heap(A)
1. heap-size[A]  length[A]
2. for i  length[A] downto 1
3. do Max-Heapify(A,i)
Can this implementation be improved?
Sure can!
2
3
4 5
6 7
8 9 10 11
1
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Heaps
Without going through a formal proof, notice that there is no
need to call Max-Heapify() on leaf nodes. At most, the
internal node with the largest index is at most equal to
length[A]/2.
Since this may be odd, should we use the floor or the ceiling?
In the case below, it is clear that we really need the floor
of length[A]/2 which is 5, and not the ceiling, which is 6.
Build-Max-Heap(A)
1. heap-size[A]  length[A]
2. for i  length[A]/2 downto 1
3. do Max-Heapify(A,i)
2 3
4 5 6 7
8 9 10 11
1
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Example: building a heap (1)
4 1 3 2 16 9 10 14 8 7
1 2 3 4 5 6 7 8 9 10
4
1
2 16
7814
3
9 10
1
2 3
4 5 6 7
8 9 10
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Example: building a heap (2)
4
1
2 16
7814
3
9 10
1
2 3
4 5 6 7
8 9 10
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Example: building a heap (4)
4
1
14 16
782
3
9 10
1
2 3
4 5 6 7
8 9 10
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Example: building a heap (5)
4
1
14 16
782
10
9 3
1
2 3
4 5 6 7
8 9 10
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Example: building a heap (6)
4
16
14 7
182
10
9 3
1
2 3
4 5 6 7
8 9 10
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Example: building a heap (7)
16
14
8 7
142
10
9 3
1
2 3
4 5 6 7
8 9 10
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Priority Queues
A priority queue is a data structure for maintaining a set S of elements, each with
an associated value called a key.
We will only consider a max-priority queue.
If we give the key a meaning, such as priority, so that elements with the highest
priority have the highest value of key, then we can use the heap structure to
extract the element with the highest priority.
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Priority Queues
Max priority queue supports the following operations:
Max-Heap-Insert(A,key): insert key into heap, maintaining heap property
Heap-Maximum(A): returns the heap element with the largest key
Heap-Extract-Max(A): returns and removes the heap element with the largest key,
and maintains the heap property
Heap-Increase-Key(A,i,key): used (at least) by
Max-Heap-Insert() to set A[i]  A[key], and then maintaining the heap property.
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Priority Queues
Heap-Maximum(A)
1. return A[1]
Heap-Extract-Max(A)
1. if heap-size[A] < 1
2. then error “heap underflow”
3. max  A[1]
4. A[1]  A[heap-size[A] ]
5. heap-size[A]  heap-size[A] - 1
6. Max-Heapify(A,1)
7. return max
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Priority Queues
Heap-Increase-Key(A,i,key)
1. if key < A[i]
2. then error “new key is smaller than current key”
3. A[i]  key
4. while i > 1 and A[Parent(i)] < A[i]
5. do exchange A[i]  A[Parent(i)]
6. i  Parent(i)
Max-Heap-Insert(A,key)
1. heap-size[A]  heap-size[A]+1
2. A[heap-size[A]]  – ∞
3. Heap-Increase-Key(A, heap-size[A], key)
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Example: increase key (1)
16
14
8 7
142
10
9 3
1
2 3
4 5 6 7
8 9 10
increase 4 to 15
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Example: increase key (2)
16
14
8 7
1152
10
9 3
1
2 3
4 5 6 7
8 9 10
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Example: increase key (3)
16
14
15 7
182
10
9 3
1
2 3
4 5 6 7
8 9 10
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Example: increase key (4)
16
15
14 7
182
10
9 3
1
2 3
4 5 6 7
8 9 10
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Heap Sort
Finally, how can we get the keys sorted in an array A? We know that a heap is
not necessarily sorted as the following sequence illustrates:
A = < 100, 50, 25, 40, 30 >
Use the algorithm Heapsort():
Heapsort(A)
1. Build-Max-Heap(A)
2. for i  length[A] downto 2
3. do exchange A[1] ↔ A[i]
4. heap-size[A]  heap-size[A]-1
5. Max-Heapify(A,1)
Build-Max-Heap() is O(n), Max-Heapify() is O(lg n), but is executed n times,
so runtime for Heapsort(A) is O(nlgn).
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Heapsort
Heapsort(A)
1. Build-Max-Heap(A)
2. for i  length[A] downto 2
3. do exchange A[1] ↔ A[i]
4. heap-size[A]  heap-size[A]-1
5. Max-Heapify(A,1)
On line 1, we create the heap. Keep in mind that the largest element is at the
root. So, why not put the element that is currently in the root at the last index
of A[]?
We will exchange the last element in A, based on the value of heap-size[A],
with A[1], as per line 3.
Then we will reduce heap-size[A] by one so that we can make sure that
putting A[heap-size] into A[1] from line 3 doesn’t violate the heap property. But
we don’t want to touch the max element, so that’s why heap-size is reduced.
Continue in this fashion until i=2. Why don’t we care about i=1? It’s already done
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Example: Heap-Sort
16
14
8 7
142
10
9 3
1
2 3
4 5 6 7
8 9 10
16 14 10 8 7 9 3 2 4 1
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Example: Heap-Sort (2)
14
8
4 7
1612
10
9 3
1
2 3
4 5 6 7
8 9 10
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Example: Heap-Sort (3)
10
8
4 7
16142
9
1 3
1
2 3
4 5 6 7
8 9 10
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Example: Heap-Sort (4)
9
8
4 7
161410
3
1 2
1
2 3
4 5 6 7
8 9 10
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Example: Heap-Sort (4)
8
7
4 2
161410
1
2 3
4 5 6 7
8 9 10
9
3
1
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Example: Heap-Sort (5)
7
4
1 2
161410
3
8 9
1
2 3
4 5 6 7
8 9 10
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Example: Heap-Sort (6)
4
2
1 7
161410
3
8 9
1
2 3
4 5 6 7
8 9 10
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Example: Heap-Sort (7)
3
2
4 7
161410
1
8 9
1
2 3
4 5 6 7
8 9 10
Department of Computer Science University of Peshawar
Heap Data Structure Advance Algorithms (Spring 2009)
Example: Heap-Sort (8)
2
1
4 7
161410
3
8 9
1
2 3
4 5 6 7
8 9 10
1 2 3 4 7 8 9 10 14 16

Contenu connexe

Tendances

Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursionAbdullah Al-hazmy
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - GraphMadhu Bala
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & DeletionAfaq Mansoor Khan
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm03446940736
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applicationssomendra kumar
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTSoumen Santra
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureBalwant Gorad
 
trees in data structure
trees in data structure trees in data structure
trees in data structure shameen khan
 

Tendances (20)

Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Heaps
HeapsHeaps
Heaps
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Linked list
Linked listLinked list
Linked list
 
Arrays
ArraysArrays
Arrays
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
stack & queue
stack & queuestack & queue
stack & queue
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
 
single linked list
single linked listsingle linked list
single linked list
 
Heap tree
Heap treeHeap tree
Heap tree
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 
Expression trees
Expression treesExpression trees
Expression trees
 
Linear Search Presentation
Linear Search PresentationLinear Search Presentation
Linear Search Presentation
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 

Similaire à Lec 17 heap data structure

Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).pptSudhaPatel11
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).pptAmitShou
 
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingAlgorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingTraian Rebedea
 
Computer notes - Josephus Problem
Computer notes - Josephus ProblemComputer notes - Josephus Problem
Computer notes - Josephus Problemecomputernotes
 
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structurefaran nawaz
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5ecomputernotes
 
heap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsheap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsssuser7319f8
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heapschidabdu
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeksJinTaek Seo
 
lecture 5
lecture 5lecture 5
lecture 5sajinsc
 
Frequency and similarity aware partitioning for cloud storage based on space ...
Frequency and similarity aware partitioning for cloud storage based on space ...Frequency and similarity aware partitioning for cloud storage based on space ...
Frequency and similarity aware partitioning for cloud storage based on space ...redpel dot com
 
L1 - Recap.pdf
L1 - Recap.pdfL1 - Recap.pdf
L1 - Recap.pdfIfat Nix
 

Similaire à Lec 17 heap data structure (20)

4 heapsort pq
4 heapsort pq4 heapsort pq
4 heapsort pq
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
Cis435 week05
Cis435 week05Cis435 week05
Cis435 week05
 
Algorithms - "heap sort"
Algorithms - "heap sort"Algorithms - "heap sort"
Algorithms - "heap sort"
 
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingAlgorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
 
Chapter 10 ds
Chapter 10 dsChapter 10 ds
Chapter 10 ds
 
Computer notes - Josephus Problem
Computer notes - Josephus ProblemComputer notes - Josephus Problem
Computer notes - Josephus Problem
 
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structure
 
Heapsort quick sort
Heapsort quick sortHeapsort quick sort
Heapsort quick sort
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5
 
heap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsheap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithms
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heaps
 
stack.ppt
stack.pptstack.ppt
stack.ppt
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeks
 
lecture 5
lecture 5lecture 5
lecture 5
 
Heap
HeapHeap
Heap
 
Frequency and similarity aware partitioning for cloud storage based on space ...
Frequency and similarity aware partitioning for cloud storage based on space ...Frequency and similarity aware partitioning for cloud storage based on space ...
Frequency and similarity aware partitioning for cloud storage based on space ...
 
L1 - Recap.pdf
L1 - Recap.pdfL1 - Recap.pdf
L1 - Recap.pdf
 

Plus de Sajid Marwat

Trusted computing introduction and technical overview
Trusted computing introduction and technical overviewTrusted computing introduction and technical overview
Trusted computing introduction and technical overviewSajid Marwat
 
Digital Rights Management and Trusted Computing Base
Digital Rights Management and Trusted Computing BaseDigital Rights Management and Trusted Computing Base
Digital Rights Management and Trusted Computing BaseSajid Marwat
 
Automata definitions
Automata definitionsAutomata definitions
Automata definitionsSajid Marwat
 
Thr cellular concept
Thr cellular conceptThr cellular concept
Thr cellular conceptSajid Marwat
 
Computer System Overview,
Computer System Overview, Computer System Overview,
Computer System Overview, Sajid Marwat
 
4 g LTE, LTE Advance
4 g LTE, LTE Advance 4 g LTE, LTE Advance
4 g LTE, LTE Advance Sajid Marwat
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec IISajid Marwat
 
Advance algorithm hashing lec I
Advance algorithm hashing lec IAdvance algorithm hashing lec I
Advance algorithm hashing lec ISajid Marwat
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihmSajid Marwat
 
GSM Network 3G Technologies
GSM Network 3G TechnologiesGSM Network 3G Technologies
GSM Network 3G TechnologiesSajid Marwat
 
WiMAX (IEEE 802.16)
WiMAX (IEEE 802.16)WiMAX (IEEE 802.16)
WiMAX (IEEE 802.16)Sajid Marwat
 
Radio over Fiber Technology for WiMAX Systems
 Radio over Fiber Technology for WiMAX Systems Radio over Fiber Technology for WiMAX Systems
Radio over Fiber Technology for WiMAX Systems Sajid Marwat
 
Knowledge Representation & Reasoning
Knowledge Representation & ReasoningKnowledge Representation & Reasoning
Knowledge Representation & ReasoningSajid Marwat
 
top level view of computer function and interconnection
top level view of computer function and interconnectiontop level view of computer function and interconnection
top level view of computer function and interconnectionSajid Marwat
 

Plus de Sajid Marwat (15)

Trusted computing introduction and technical overview
Trusted computing introduction and technical overviewTrusted computing introduction and technical overview
Trusted computing introduction and technical overview
 
Digital Rights Management and Trusted Computing Base
Digital Rights Management and Trusted Computing BaseDigital Rights Management and Trusted Computing Base
Digital Rights Management and Trusted Computing Base
 
Manet
ManetManet
Manet
 
Automata definitions
Automata definitionsAutomata definitions
Automata definitions
 
Thr cellular concept
Thr cellular conceptThr cellular concept
Thr cellular concept
 
Computer System Overview,
Computer System Overview, Computer System Overview,
Computer System Overview,
 
4 g LTE, LTE Advance
4 g LTE, LTE Advance 4 g LTE, LTE Advance
4 g LTE, LTE Advance
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec II
 
Advance algorithm hashing lec I
Advance algorithm hashing lec IAdvance algorithm hashing lec I
Advance algorithm hashing lec I
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
GSM Network 3G Technologies
GSM Network 3G TechnologiesGSM Network 3G Technologies
GSM Network 3G Technologies
 
WiMAX (IEEE 802.16)
WiMAX (IEEE 802.16)WiMAX (IEEE 802.16)
WiMAX (IEEE 802.16)
 
Radio over Fiber Technology for WiMAX Systems
 Radio over Fiber Technology for WiMAX Systems Radio over Fiber Technology for WiMAX Systems
Radio over Fiber Technology for WiMAX Systems
 
Knowledge Representation & Reasoning
Knowledge Representation & ReasoningKnowledge Representation & Reasoning
Knowledge Representation & Reasoning
 
top level view of computer function and interconnection
top level view of computer function and interconnectiontop level view of computer function and interconnection
top level view of computer function and interconnection
 

Dernier

Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 

Dernier (20)

Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 

Lec 17 heap data structure

  • 1. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Heap Data Structure Zahoor Jan
  • 2. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Heaps A (binary) heap data structure is an array object that can be viewed as a complete binary tree Each node of the tree corresponds to an element of the array that stores the value in the node A complete binary tree is one where all the internal nodes have exactly 2 children, and all the leaves are on the same level. The tree is completely filled on all levels except possibly the lowest, which is filled from left to right up to a point Leaf nodes are nodes without children.  Leaf node  Interior node Edge 
  • 3. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Heaps Height of a node: The number of edges starting at that node, and going down to the furthest leaf. Height of the heap: The maximum number of edges from the root to a leaf.  Root Height of blue node = 1 Height of root = 3
  • 4. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Heaps Complete binary tree– if not full, then the only unfilled level is filled in from left to right. 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 127 8 9 10 11 1 Parent(i) return i/2 Left(i) return 2i Right(i) return 2i + 1
  • 5. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Heaps The heap property of a tree is a condition that must be true for the tree to be considered a heap. Min-heap property: for min-heaps, requires A[parent(i)] ≤ A[i] So, the root of any sub-tree holds the least value in that sub-tree. Max-heap property: for max-heaps, requires A[parent(i)] ≥ A[i] The root of any sub-tree holds the greatest value in the sub-tree.
  • 6. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Heaps Looks like a max heap, but max-heap property is violated at indices 11 and 12. Why? because those nodes’ parents have smaller values than their children. 2 8 3 8 4 8 5 7 6 8 7 1 8 6 9 7 10 5 11 8 12 9 1 9
  • 7. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Heaps Assume we are given a tree represented by a linear array A, and such that i ≤ length[A], and the trees rooted at Left(i) and Right(i) are heaps. Then, to create a heap rooted at A[i], use procedure Max-Heapify(A,i): Max-Heapify(A,i) 1. l  Left(i) 2. r  Right(i) 3. if l ≤ heap-size[A] and A[l] > A[i] 4. then largest  l 5. else largest  i 6. if r ≤ heap-size[A] and A[r] > A[largest] 7. then largest  r 8. if largest ≠ i 9. then swap( A[i], A[largest]) Max-Heapify(A, largest) 20 9 19 18 6 7 17 16 17 16 1 10
  • 8. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Heaps If either child of A[i] is greater than A[i], the greatest child is exchanged with A[i]. So, A[i] moves down in the heap. The move of A[i] may have caused a violation of the max- heap property at it’s new location. So, we must recursively call Max-Heapify(A,i) at the location i where the node “lands”. The above is the top-down approach of Max-Heapify(A,i) 20 9 19 18 6 7 17 16 17 16 1 10
  • 9. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Max-Heapify(A,1) 20 9 19 18 6 7 17 16 17 16 1 10 10 9 19 18 6 7 17 16 17 16 1 20 10 9 19 18 6 7 17 16 17 16 1 20 19 9 10 18 6 7 17 16 17 16 1 20 19 9 10 18 6 7 17 16 17 16 1 20 19 9 17 18 6 7 10 16 17 16 1 20
  • 10. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Run Time of Max-Heapify() Run time of Max-Heapify(). How many nodes, in terms of n, is in the sub-tree with the most nodes? 2 n / 3 So, worst case would follow a path that included the most nodes T(n) = T(2n/3) + Θ(1) Why Θ(1)? Note that all the work in lines 1-7 is simple assignments or comparisons, so they are constant time, or Θ(1). 2 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 1 2(2) / 3 = 2 1 ≤ 2 2(5) / 3 = 3 3 ≤ 3 2(11) / 3 = 8 7 ≤ 8
  • 11. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Heaps So, what must we do to build a heap? We call Max-Heapify(A,i) for every i starting at last node and going to the root. Why Bottom-up? Because Max-Heapify() moves the larger node upward into the root. If we start at the top and go to larger node indices, the highest a node can move is to the root of that sub-tree. But what if there is a violation between the sub-tree’s root and its parent? So, we must start from the bottom and go towards the root to fix the problems caused by moving larger nodes into the root of sub-trees.
  • 12. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Heaps Bad-Build-Max-Heap(A) 1. heap-size[A]  length[A] 2. for i  length[A] downto 1 3. do Max-Heapify(A,i) Can this implementation be improved? Sure can! 2 3 4 5 6 7 8 9 10 11 1
  • 13. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Heaps Without going through a formal proof, notice that there is no need to call Max-Heapify() on leaf nodes. At most, the internal node with the largest index is at most equal to length[A]/2. Since this may be odd, should we use the floor or the ceiling? In the case below, it is clear that we really need the floor of length[A]/2 which is 5, and not the ceiling, which is 6. Build-Max-Heap(A) 1. heap-size[A]  length[A] 2. for i  length[A]/2 downto 1 3. do Max-Heapify(A,i) 2 3 4 5 6 7 8 9 10 11 1
  • 14. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Example: building a heap (1) 4 1 3 2 16 9 10 14 8 7 1 2 3 4 5 6 7 8 9 10 4 1 2 16 7814 3 9 10 1 2 3 4 5 6 7 8 9 10
  • 15. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Example: building a heap (2) 4 1 2 16 7814 3 9 10 1 2 3 4 5 6 7 8 9 10
  • 16. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Example: building a heap (4) 4 1 14 16 782 3 9 10 1 2 3 4 5 6 7 8 9 10
  • 17. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Example: building a heap (5) 4 1 14 16 782 10 9 3 1 2 3 4 5 6 7 8 9 10
  • 18. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Example: building a heap (6) 4 16 14 7 182 10 9 3 1 2 3 4 5 6 7 8 9 10
  • 19. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Example: building a heap (7) 16 14 8 7 142 10 9 3 1 2 3 4 5 6 7 8 9 10
  • 20. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Priority Queues A priority queue is a data structure for maintaining a set S of elements, each with an associated value called a key. We will only consider a max-priority queue. If we give the key a meaning, such as priority, so that elements with the highest priority have the highest value of key, then we can use the heap structure to extract the element with the highest priority.
  • 21. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Priority Queues Max priority queue supports the following operations: Max-Heap-Insert(A,key): insert key into heap, maintaining heap property Heap-Maximum(A): returns the heap element with the largest key Heap-Extract-Max(A): returns and removes the heap element with the largest key, and maintains the heap property Heap-Increase-Key(A,i,key): used (at least) by Max-Heap-Insert() to set A[i]  A[key], and then maintaining the heap property.
  • 22. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Priority Queues Heap-Maximum(A) 1. return A[1] Heap-Extract-Max(A) 1. if heap-size[A] < 1 2. then error “heap underflow” 3. max  A[1] 4. A[1]  A[heap-size[A] ] 5. heap-size[A]  heap-size[A] - 1 6. Max-Heapify(A,1) 7. return max
  • 23. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Priority Queues Heap-Increase-Key(A,i,key) 1. if key < A[i] 2. then error “new key is smaller than current key” 3. A[i]  key 4. while i > 1 and A[Parent(i)] < A[i] 5. do exchange A[i]  A[Parent(i)] 6. i  Parent(i) Max-Heap-Insert(A,key) 1. heap-size[A]  heap-size[A]+1 2. A[heap-size[A]]  – ∞ 3. Heap-Increase-Key(A, heap-size[A], key)
  • 24. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Example: increase key (1) 16 14 8 7 142 10 9 3 1 2 3 4 5 6 7 8 9 10 increase 4 to 15
  • 25. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Example: increase key (2) 16 14 8 7 1152 10 9 3 1 2 3 4 5 6 7 8 9 10
  • 26. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Example: increase key (3) 16 14 15 7 182 10 9 3 1 2 3 4 5 6 7 8 9 10
  • 27. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Example: increase key (4) 16 15 14 7 182 10 9 3 1 2 3 4 5 6 7 8 9 10
  • 28. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Heap Sort Finally, how can we get the keys sorted in an array A? We know that a heap is not necessarily sorted as the following sequence illustrates: A = < 100, 50, 25, 40, 30 > Use the algorithm Heapsort(): Heapsort(A) 1. Build-Max-Heap(A) 2. for i  length[A] downto 2 3. do exchange A[1] ↔ A[i] 4. heap-size[A]  heap-size[A]-1 5. Max-Heapify(A,1) Build-Max-Heap() is O(n), Max-Heapify() is O(lg n), but is executed n times, so runtime for Heapsort(A) is O(nlgn).
  • 29. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Heapsort Heapsort(A) 1. Build-Max-Heap(A) 2. for i  length[A] downto 2 3. do exchange A[1] ↔ A[i] 4. heap-size[A]  heap-size[A]-1 5. Max-Heapify(A,1) On line 1, we create the heap. Keep in mind that the largest element is at the root. So, why not put the element that is currently in the root at the last index of A[]? We will exchange the last element in A, based on the value of heap-size[A], with A[1], as per line 3. Then we will reduce heap-size[A] by one so that we can make sure that putting A[heap-size] into A[1] from line 3 doesn’t violate the heap property. But we don’t want to touch the max element, so that’s why heap-size is reduced. Continue in this fashion until i=2. Why don’t we care about i=1? It’s already done
  • 30. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Example: Heap-Sort 16 14 8 7 142 10 9 3 1 2 3 4 5 6 7 8 9 10 16 14 10 8 7 9 3 2 4 1
  • 31. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Example: Heap-Sort (2) 14 8 4 7 1612 10 9 3 1 2 3 4 5 6 7 8 9 10
  • 32. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Example: Heap-Sort (3) 10 8 4 7 16142 9 1 3 1 2 3 4 5 6 7 8 9 10
  • 33. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Example: Heap-Sort (4) 9 8 4 7 161410 3 1 2 1 2 3 4 5 6 7 8 9 10
  • 34. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Example: Heap-Sort (4) 8 7 4 2 161410 1 2 3 4 5 6 7 8 9 10 9 3 1
  • 35. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Example: Heap-Sort (5) 7 4 1 2 161410 3 8 9 1 2 3 4 5 6 7 8 9 10
  • 36. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Example: Heap-Sort (6) 4 2 1 7 161410 3 8 9 1 2 3 4 5 6 7 8 9 10
  • 37. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Example: Heap-Sort (7) 3 2 4 7 161410 1 8 9 1 2 3 4 5 6 7 8 9 10
  • 38. Department of Computer Science University of Peshawar Heap Data Structure Advance Algorithms (Spring 2009) Example: Heap-Sort (8) 2 1 4 7 161410 3 8 9 1 2 3 4 5 6 7 8 9 10 1 2 3 4 7 8 9 10 14 16