Ch. 12 Tables and Priority Queues

Slides:



Advertisements
Similar presentations
CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
Advertisements

1 HeapSort CS 3358 Data Structures. 2 Heapsort: Basic Idea Problem: Arrange an array of items into sorted order. 1) Transform the array of items into.
© 2006 Pearson Addison-Wesley. All rights reserved12 B-1 Chapter 12 (continued) Tables and Priority Queues.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Tables and Priority Queues.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
© 2006 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 Tables and Priority Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Heaps.
Chapter 12 B Priority Queues. © 2004 Pearson Addison-Wesley. All rights reserved 12 B-2 The ADT Priority Queue: A Variation of the ADT Table The ADT priority.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Tables and Priority Queues.
data ordered along paths from root to leaf
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
The ADT Table The ADT table, or dictionary Uses a search key to identify its items Its items are records that contain several pieces of data 2 Figure.
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Chapter 12 Heaps & HeapSort © John Urrutia 2014, All Rights Reserved1.
Chapter 13 Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-in-first-out The “smallest”
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
CSI 312 Dr. Yousef Qawqzeh Heaps and Priority Queue.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Spring 2016CS202 - Fundamental Structures of Computer Science II1 Tables Appropriate for problems that must manage data by value. Some important operations.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Tables and Priority Queues
Chapter 12 A Tables.
Partially Ordered Data ,Heap,Binary Heap
Tables and Priority Queues
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
Yuanming Yu CSCI2100B Data Structures Tutorial 7
Source: Muangsin / Weiss
Binary Heaps What is a Binary Heap?
CSCE 3100 Data Structures and Algorithm Analysis
Bohyung Han CSE, POSTECH
CS202 - Fundamental Structures of Computer Science II
Priority Queues Linked-list Insert Æ Æ head head
BST Review Jika terdapat urutan bilangan di atas, dimulai dari 10 dan diakhiri dengan 10. Buatkan BST nya Tampilkan secara inorder Preorder.
Binary Heaps What is a Binary Heap?
- Alan Perlis Heaps "You think you know when you can learn,
CSCI2100 Data Structures Tutorial 7
CS202 - Fundamental Structures of Computer Science II
Priority Queues.
CMSC 341 Lecture 14 Priority Queues & Heaps
Priority Queues.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
CSCE 3110 Data Structures and Algorithm Analysis
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Binary Heaps What if we’re mostly concerned with finding the most relevant data? A binary heap is a binary tree (2 or fewer subtrees for each node) A heap.
Tables and Priority Queues
Copyright ©2012 by Pearson Education, Inc. All rights reserved
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Priority Queues & Heaps
CSCE 3110 Data Structures and Algorithm Analysis
CSE 12 – Basic Data Structures
Sorting Dr. Yingwu Zhu.
CSCE 3110 Data Structures and Algorithm Analysis
CS 367 – Introduction to Data Structures
Priority Queues (Heaps)
Heaps By JJ Shepherd.
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 9 The Priority Queue ADT
Priority Queue and Heap
Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps
Tables and Priority Queues
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right.
EE 312 Software Design and Implementation I
Presentation transcript:

Ch. 12 Tables and Priority Queues Static data sets Data do not change once constructed Dynamic data sets Data sets can grow or shrink Dictionary (table) A dynamic data set that supports insertion, deletion, and membership test Priority queue A dynamic data sets that supports insertion, deletion, and retrieval of max element

Figure 1. An ordinary table of cities

Figure 2. The data fields for two sorted linear implementations of the ADT table for the data in Figure 1: a) array based; b) reference based

Figure 3. The data fields for a binary search tree implementation of the ADT table for the data in Figure 1

Figure 4 Insertion for unsorted linear implementations: a) array based; b) reference based

Figure 5 Insertion for sorted linear implementations: a) array based; b) reference based

Figure 6 The average-case order of the operations of the ADT table for various implementations

ADT Table Operations Create an empty table Determine whether the table is empty Determine the number of items in the table Add a new item to the table Remove the item w/ a given search key from the table Retrieve the item w/ a given search key from the table Traverse the items in the table in sorted search-key order

Sorted Array-Based Implementation public class TableArrayBased { final int MAX_TABLE = 100; protected Comparable[ ] items; private int size; public TableArrayBased( ) { items = new Comparable[MAX_TABLE]; size = 0; }

public boolean tableIsEmpty { return size == 0; } public int tableLength( ) { return size; public void tableInsert(Comparable newItem) { if (size < MAX_TABLE) { int spot = position(newItem); if ((spot < size) && items[spot].compareTo(newItem) == 0) { exception 처리 for “duplicated items”; } else { for (int i = size-1; i >= spot; --i) { // shift right items[i+1] = items[i]; items[spot] = newItem; // insert ++size; exception 처리 for “table full”;

public boolean tableDelete(Comparable searchKey) { int spot = position(searchKey); boolean success = (spot < size) && (items[spot].compareTo(searchKey) == 0); if (success) { size--; for (int i = spot; i < size; ++i) { // shift left items[i] = items[i+1]; } public Comparable tableRetrieve(Comparable searchKey) { … protected int position(Comparable searchKey) { int pos = 0; while ((pos < size) && (searchKey.compareTo(items[pos]) > 0) { pos++; return pos; } // end TableArrayBased

Binary Search Tree-Based Implementation public class TableBTBased { private int size; public TableBTBased( ) { bst = new BinarySearchTree( ); size = 0; }

public boolean tableIsEmpty { return size == 0; } public int tableLength( ) { return size; public void tableInsert(Comparable newItem) { if (bst.retrieve(newItem) == null) { bst.insert(newItem); size++; } else { exception 처리 for “duplicated items”; public Comparable tableRetrieve(Comparable searchKey) { return bst.retrieve(searchKey); public boolean tableDelete(Comparable searchKey) { if (bst.deleteItem(searchKey) is successful) { size--; return true; } else { return false; } } // TableBTBased

ADT Priority Queue Operations Create an empty priority queue Determine whether the priority queue is empty Add a new item to the priority queue Retrieve and then remove the item w/ the highest priority value

비교 Deletion Insertion Key 값 중복 Table은 search key 제공 Priority queue는 search key 사용 안함 Priority가 가장 높은 item만 delete 가능함 Insertion Table과 priority queue 둘 다 key 사용함 Key 값 중복 Table은 불허 Priority queue는 허용

Heap : A Representative Priority Queue A heap is a complete binary tree that is empty or the key of each node is greater than or equal to the keys of both children (if any) The root has the largest key Maxheap : minheap The root has max key : min key

A Heap w/ Array Representation 1 2 3 4 5 6 1 2 3 4 5 6 indices Node i’s children: 2i, 2i+1 Node i’s parent: i/2

Deletion Return the root item Remove the last node and move it to the root Percolate down until the heap is valid

1 2 3 4 5 6

1 2 3 4 5 6

Percolate down

Figure 7 Recursive calls to heapRebuild percolateDown percolateDown

heapDelete( ) { // Keys are in key[1]…key[size] if (!heapIsEmpty( )) { rootItem = key[1]; key[1] = key[size]; // move the last node size--; percolateDown(1); return rootItem; } else { return null; }

percolateDown (int i) { child = 2*i ; // left child rightChild = 2*i + 1; // right child if (child <= size) { if ((rightChild <= size) && (key[child] < key[rightChild])) { child = rightChild; // index of larger child } if (key[i] < key[child]) { Swap key[i] and key[child]; percolateDown(child);

Insertion Insert an item into the bottom of the complete tree Percolate up until the heap is valid

heapInsert(newItem) { i = size+1; key[i] = newItem; parent = i/2; while ((parent >= 1) && (key[i] > key[parent])) { Swap key[i] and key[parent]; i = parent; }

percolate

Figure 8-1 Some non-efficient implementations of the ADT priority queue: a) array based; b) reference based

Figure 8-2 c) A non-desirable implementation of the ADT priority queue: binary search tree

Heapsort Heap을 이용한 sorting 먼저 heap을 만든 다음, “Root node의 key를 빼내고, last node의 key를 root로 옮긴 다음 percolate down”하는 작업을 반복한다.

PercolateDown for Sorting percolateDown (Comparable key[ ], int i, int n) { child = 2*i ; // left child rightChild = 2*i + 1; // right child if (child <= n) { if ((rightChild <= n) && (key[child] < key[rightChild])) { child = rightChild; // index of larger child } if (key[i] < key[child]) { Swap key[i] and key[child]; percolateDown(key, child, n);

Heapsort heapsort(A, n) { // build initial heap: A[1…n] for i = n/2 downto 1 { percolateDown(A, i, n); } // delete one by one for size = n-1 downto 1 { Swap A[1] and A[size+1]; percolateDown(A, 1, size); // 이 지점에서 A[1…n]은 sorting되어 있다

Figure 9-1 A trace of heapsort size 1 size size percolateDown(anArray, 1, 5) size

Figure 9-2 A trace of heapsort, continued. size 1 size size size percolateDown(anArray, 1, 4) size 1 size size size

Figure 9-3 A trace of heapsort, continued. percolateDown(anArray, 1, 3) size 1 size size size percolateDown(anArray, 1, 2) size 1 size size size