Download presentation
Presentation is loading. Please wait.
Published byRalf Chapman Modified over 9 years ago
1
CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com
2
CS-2852 Data Structures, Andrew J. Wozniewicz Agenda Heap – Tree Implementation – Array Implementation Priority Queue
3
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
4
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
5
CS-2852 Data Structures, Andrew J. Wozniewicz Full, Perfect, and Complete BT Full: – All nodes have either 0 or 2 children Perfect – Full, with 2 height -1 nodes Complete – Perfect through level height-1
6
Max-Heap Example
7
Min-Heap Example
8
CS-2852 Data Structures, Andrew J. Wozniewicz Heap Operations create findMax | findMin deleteMax | deleteMin insert merge
9
CS-2852 Data Structures, Andrew J. Wozniewicz Heap Applications Priority Queue – Maximally efficient implementation – java.util.PriorityQueue 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)
10
CS-2852 Data Structures, Andrew J. Wozniewicz Heap Implementations TREE ARRAY
11
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
12
CS-2852 Data Structures, Andrew J. Wozniewicz Heap Operations: Insert (Example)
13
CS-2852 Data Structures, Andrew J. Wozniewicz Heap Operations: Insert (Example)
14
CS-2852 Data Structures, Andrew J. Wozniewicz Heap Formulas Children of Node p: c 1 (p) = 2p+1 c 2 (p) = 2p+2 Parent of Node c: p(c) = (c-1) / 2
15
CS-2852 Data Structures, Andrew J. Wozniewicz Heap Operations: Insert (Example)
16
CS-2852 Data Structures, Andrew J. Wozniewicz Heap Operations: Insert (Example)
17
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
18
CS-2852 Data Structures, Andrew J. Wozniewicz Heap Operations: Remove
19
CS-2852 Data Structures, Andrew J. Wozniewicz Heap Operations: Remove
20
CS-2852 Data Structures, Andrew J. Wozniewicz Heap Operations: Remove
21
CS-2852 Data Structures, Andrew J. Wozniewicz Heap Operations: Remove
22
CS-2852 Data Structures, Andrew J. Wozniewicz Time-Complexity of Heap Operations Insertion O(log n) Removal O(log n)
23
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
24
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 IMPLEMENTATIONS DON’T EVER DO THIS! Priority Queue
25
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 IMPLEMENTED DO NOT RE-INVENT THE WHEEL!
26
CS-2852 Data Structures, Andrew J. Wozniewicz Summary Heap – Tree Implementation – Array Implementation Priority Queue
27
Questions? Image copyright © 2010 andyjphoto.com
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.