1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Java methods: Object-oriented programming and data structures (2nd AP edition): Chapter 26 - Maria Litvin, Gary Litvin

16 21 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 16
Dung lượng 337,14 KB

Nội dung

Chapter 26 - Heaps and priority queues. This chapter completes our tour of data structures. After you have mastered the material in this chapter, you will be able to: Learn about heaps, review the java.util.PriorityQueue class, learn about heapsort.

Java Methods Object-Oriented Programming and Data Structures 2nd AP edition with GridWorld Maria Litvin ● Gary Litvin A C E H P T R Heaps and Priority Queues Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing All rights reserved Objectives: • Learn about heaps • Review the java.util.PriorityQueue class • Learn about Heapsort 26­2 Priority Queues • A priority queue is a data structure for temporary storage that delivers the stored items in order of their priority: an item with higher priority is delivered first • The objects in a priority queue are Comparable (or a comparator is provided) • According to a convention, the smaller item has higher priority 26­3 Possible Implementations • A sorted list: items are stored in order of priority remove and peek are O(1), but add is O(n) • An unsorted list: items are stored in order of their arrival add is O(1) but remove and peek are O(n) Either way, one of the methods creates a bottleneck 26­4 Heaps • A heap is a particular kind of a binary tree • Heaps provide a way to implement priority queues in such a way that both add and remove take O(log n) time • A heap can be stored in an array (or in an ArrayList) 26­5 Full and Complete Binary Trees Full tree: all levels are filled; a full tree with h levels holds 2h - nodes Complete tree: all levels are filled, except perhaps the bottom one                            26­6 Complete Trees • Nodes can be numbered in level-by-level order:                 8        9    10      11    12 The parent of the i-th node is the node i /  The left child of the i-th node is the node 2*i and the right child is the node 2*i + 26­7 Complete Trees (cont’d) • It is convenient to store a complete binary tree in an array in the order of nodes, starting at index 1:   Argentina  Brazil  Egypt  Haiti  Dominica  9  3  Italy  Greece  5  France  8  Chile  2  4  1  6  items[0]: items[1]: items[2]: items[3]: items[4]: items[5]: 7  items[6]: items[7]: items[8]: items[9]: Argentina Brazil Chile Egypt Dominica Greece Italy Haiti France  26­8 Heaps (cont’d) • A (min) heap is a complete binary tree • The value in each node does not exceed any of the values in that node’s left and right subtrees • In a heap, the root holds the smallest value A heap: Not a heap: 3 / \ 12 / \ 12 20 / \ 12 20 / \ 12 26­9 Heaps (cont’d) • Either adding or removing an item takes O(log n) time • The algorithm for add uses the reheap-up procedure • The algorithm for remove uses the reheap-down procedure Add a leaf Starting at the last leaf, swap the node with its parent as many times as needed to repair the heap Remove the root and place the last leaf at the root Starting at the root, swap the node with its smaller child, as many times as needed to repair the heap 26­10 The add Algorithm Step 1: the new value is added as the rightmost leaf at the bottom level, keeping the tree complete   Brazil  Dominica  Egypt  Chile  2  France  China  8  3  Italy  Greece  5  4  Haiti  1  6  7  9  Step 2: “reheap up”: the new value keeps swapping places with its parent until it falls into place   Brazil  Dominica  4  Egypt  Haiti  8  9  China  3  Italy  Greece  5  Brazil  1  Chile  2  France  China    6  Dominica  7  France  Egypt  Haiti  8  Chile  2  3  Italy  Greece  5  4  1  6  9  26­11 7  The remove Algorithm Step 1: the root is removed   Argentina  Brazil  Egypt    Haiti      2  Dominica  5  4  Step 2: the rightmost leaf from the bottom level replaces the root France  1  Chile      Greece  6  Brazil  3  Italy  Egypt  7  4  France    9  8  1  Chile  2  Dominica    5  3  Italy  Greece  6  7  Haiti  8  Step 3: “reheap down”: the new root value keeps swapping places with its smaller child until it falls into place   Brazil  France  Egypt  Dominica  3  Italy  Greece  5  Brazil  1  Chile  2  Dominica  4    6  Egypt  7  4  Chile  2  France  1  Italy  Greece  5  3  6  7  Haiti  Haiti  8  8  26­12 java.util.PriorityQueue • Implements java.util.Queue with methods: boolean isEmpty (); void add (E obj); E remove (); E peek (); • The implementation is a heap • add and remove are O(log n); peek is O(1) 26­13 Heapsort • A relatively fast algorithm for sorting   Place all values into a heap Remove values one by one (they come out in ascending order) and add them to the output list • Heapsort can be implemented in the same array, without a temporary heap: Rearrange the values to make a max-heap Rearrange the values again to get a sorted array • The running time is O(n log n) 26­14 Review: • What is a priority queue? • What is wrong with implementing a priority queue as a sorted list? • What is a complete binary tree? • If a complete tree is stored in an array with the first node in items [1], where can we find the parent of the 5-th node? Its left and right children? 26­15 Review (cont’d): • What is a heap? • Describe the main steps in the algorithm for removing the smallest value from a heap • Describe the main steps in the algorithm for adding a value to a heap • What is the main idea of Heapsort? 26­16 ... smallest value A heap: Not a heap: 3 / 12 / 12 20 / 12 20 / 12 26? ?9 Heaps (cont’d) • Either adding or removing an item takes O(log n) time • The algorithm for add uses the reheap-up procedure... 8  8  26? ?12 java. util.PriorityQueue • Implements java. util.Queue with methods: boolean isEmpty (); void add (E obj); E remove (); E peek (); • The implementation is a heap • add and remove... 2h - nodes Complete tree: all levels are filled, except perhaps the bottom one                            26? ?6 Complete Trees • Nodes can be numbered in level-by-level

Ngày đăng: 04/11/2020, 23:20

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN