CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
-
Upload
ralf-chapman -
Category
Documents
-
view
212 -
download
0
Transcript of CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
CS-2852Data StructuresLECTURE 13A
Andrew J. Wozniewicz
Image copyright © 2010 andyjphoto.com
CS-2852 Data Structures, Andrew J. Wozniewicz
Agenda• Heap– Tree Implementation– Array Implementation
• Priority Queue
CS-2852 Data Structures, Andrew J. Wozniewicz
What is a Heap?
• Max-Heap: key(P) >= key(C)• Min-Heap: key(P) <= key(C)
• Not to be confused with memory “heap” (except in early LISP)
(Max-)Heap is a data structure that satisfies the “heap property”: if C is a child node of P, then key(P) >= key(C)
DEFINITION
CS-2852 Data Structures, Andrew J. Wozniewicz
What is a Heap?
A Heap is a complete binary tree with the following properties:• The value in the root is the
smallest (largest)• Every subtree is a heap.
DEFINITION
CS-2852 Data Structures, Andrew J. Wozniewicz
Full, Perfect, and Complete BT
• Full:– All nodes have either 0 or 2 children
• Perfect– Full, with 2height-1 nodes
• Complete– Perfect through level height-1
Max-Heap Example
19
17 3 25
100
36
1
2 7
Min-Heap Example
18
20 28 39
6
29
66
37 26 76 32 76
CS-2852 Data Structures, Andrew J. Wozniewicz
Heap Operations
• create• findMax | findMin• deleteMax | deleteMin• insert• merge
CS-2852 Data Structures, Andrew J. Wozniewicz
Heap Applications• Priority Queue– Maximally efficient implementation– java.util.PriorityQueue<E>
• Graph algorithms – Dijkstra’s; shortest path
• Heap Sort– Sort an array in-place
• Selection algorithms– Finding min/max in O(1)– Median; k-th largest element in O(n)
CS-2852 Data Structures, Andrew J. Wozniewicz
Heap Implementations
18
20 28 39
6
29
66
37 26 76 32 76
6 18 29 20 28 39 66 37 26 76 32 76
TREE
ARRAY
CS-2852 Data Structures, Andrew J. Wozniewicz
Heap Operations: Insert
• Insert the new item in the next available position at the bottom of the heap
• While new item not at root, AND smaller than its parent:– Swap the new item with its parent
CS-2852 Data Structures, Andrew J. Wozniewicz
Heap Operations: Insert (Example)
18
20 28 39
6
29
66
37 26 76 32 76
[0]
[1] [2]
[3] [4] [5] [6]
[7] [8] [9] [10] [11][12] [13] [14]
6 18 29 20 28 39 66 37 26 76 32 76[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]
CS-2852 Data Structures, Andrew J. Wozniewicz
Heap Operations: Insert (Example)
6 18 29 20 28 39 66 37 26 76 32 76 7[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]
18
20 28 39
6
29
37 26 76 32 76
[0]
[1] [2]
[3] [4] [5] [6]
[7] [8] [9] [10] [11] [12][13] [14]7
66
CS-2852 Data Structures, Andrew J. Wozniewicz
Heap Formulas
• Children of Node p:c1(p) = 2p+1
c2(p) = 2p+2
• Parent of Node c:p(c) = (c-1) / 2
CS-2852 Data Structures, Andrew J. Wozniewicz
Heap Operations: Insert (Example)
18
20 28
6
29
37 26 76 32 76
[0]
[1] [2]
[3] [4] [5] [6]
[7] [8] [9] [10] [11] [12][13] [14]
7 66
39
6 18 29 20 28 3966 37 26 76 32 767[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]
CS-2852 Data Structures, Andrew J. Wozniewicz
Heap Operations: Insert (Example)
18
20 28
6
37 26 76 32 76
[0]
[1] [2]
[3] [4] [5] [6]
[7] [8] [9] [10] [11] [12][13] [14]
66
39
29
7
6 18 2920 28 3966 37 26 76 32 767[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]
CS-2852 Data Structures, Andrew J. Wozniewicz
Heap Operations: Remove
• Remove root item by replacing with last item in the heap (LAST)
• While LAST has children, AND LAST > child:– Swap LAST with smaller child
CS-2852 Data Structures, Andrew J. Wozniewicz
Heap Operations: Remove
18
20 28
6
37 26 76 32 76
[0]
[1] [2]
[3] [4] [5] [6]
[7] [8] [9] [10] [11] [12]
[13] [14]
66
39
29
7
6 18 2920 28 3966 37 26 76 32 767
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]
CS-2852 Data Structures, Andrew J. Wozniewicz
Heap Operations: Remove
18
20 28
37 26 76 32 76
[0]
[1] [2]
[3] [4] [5] [6]
[7] [8] [9] [10] [11] [12]
[13] [14]
6629
7
639
39 18 2920 28 66 37 26 76 32 767
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]
CS-2852 Data Structures, Andrew J. Wozniewicz
Heap Operations: Remove
18
20 28
37 26 76 32 76
[0]
[1] [2]
[3] [4] [5] [6]
[7] [8] [9] [10] [11] [12]
[13] [14]
6629
7
39
7 18 2920 28 66 37 26 76 32 7639
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]
CS-2852 Data Structures, Andrew J. Wozniewicz
Heap Operations: Remove
18
20 28
37 26 76 32 76
[0]
[1] [2]
[3] [4] [5] [6]
[7] [8] [9] [10] [11] [12]
[13] [14]
66
7
39
29
7 18 3920 28 66 37 26 76 32 7629
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]
CS-2852 Data Structures, Andrew J. Wozniewicz
Time-Complexity of Heap Operations
• Insertion
O(log n)• Removal
O(log n)
CS-2852 Data Structures, Andrew J. Wozniewicz
Priority Queue
• An Abstract Data Type• Each element associated with a
“priority”• Elements are removed
Highest-Priority-First (VIP)• Operations:– insert_with_priority– pull_highest_priority
CS-2852 Data Structures, Andrew J. Wozniewicz
Inefficient (BAD) Implementations
• Unsorted “bag” (list)– Insertion: O(1)– Removal: O(n)
• Sorted list– Insertion: O(n)– Removal: O(1)
TERRIBLE IMPLEMENTATIONSDON’T EVER DO THIS!
Priority Queue
CS-2852 Data Structures, Andrew J. Wozniewicz
Typical (GOOD) Implementations
• Heap– Insertion: O(log n)– Removal: O(log n)
• Self-Balancing Tree– Insertion: O(log n)– Removal: O(log n)
Priority Queue
TYPICALLY ALREADY IMPLEMENTEDDO NOT RE-INVENT THE WHEEL!
CS-2852 Data Structures, Andrew J. Wozniewicz
Summary• Heap– Tree Implementation– Array Implementation
• Priority Queue
Questions?
Image copyright © 2010 andyjphoto.com