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.

Slides:



Advertisements
Similar presentations
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
Advertisements

Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
1 Heaps & Priority Queues (Walls & Mirrors - Remainder of Chapter 11)
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
© 2006 Pearson Addison-Wesley. All rights reserved12 B-1 Chapter 12 (continued) Tables and Priority Queues.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
© 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 reserved11 A-1 Chapter 11 Trees.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Heaps.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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.
Fundamentals of Python: From First Programs Through Data Structures
Week 10: Heap and Priority queue. Any feature here?
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
Marc Smith and Jim Ten Eyck
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Tables and Priority Queues.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Data Structures Using C++ 2E Chapter 10 Sorting Algorithms.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues I.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
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.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Fall 2015CS202 - Fundamental Structures of Computer Science II1 Heaps Appropriate for problems that must manage data by value. Some important operations.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
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”
Heaps Chapter 17 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
CSI 312 Dr. Yousef Qawqzeh Heaps and Priority Queue.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
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.
FALL 2005CENG 213 Data Structures1 Review. FALL 2005CENG 213 Data Structures2 Collection of items Problems that must manage data by value. Some important.
Spring 2016CS202 - Fundamental Structures of Computer Science II1 Tables Appropriate for problems that must manage data by value. Some important operations.
Tables and Priority Queues
Chapter 12 A Tables.
Tables and Priority Queues
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
CS202 - Fundamental Structures of Computer Science II
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
CS202 - Fundamental Structures of Computer Science II
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Ch. 12 Tables and Priority Queues
Tables and Priority Queues
Sorting And Searching CSE116A,B 4/7/2019 B.Ramamurthy.
Sorting Dr. Yingwu Zhu.
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
Tables and Priority Queues
EE 312 Software Design and Implementation I
Presentation transcript:

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 11-1 An ordinary table of cities

The ADT Table Various sets of table operations are possible 3 Figure 11-2 UML diagram for class Table

The ADT Table Our table assumes distinct search keys Other tables could allow duplicate search keys The traverseTable operation visits table items in a specified order One common order is by sorted search key A client-defined visit function is supplied as an argument to the traversal Called once for each item in the table 4

The ADT Table KeyedItem class Same as for the ADT binary search tree Contains an item’s search key and a method for accessing the search-key data field Prevents modification of the search-key value once an item is created Has only a constructor for initializing the search key Also true of a class that extends KeyedItem 5

Selecting an Implementation Linear implementations: Four categories Unsorted: array based or pointer based Sorted (by search key): array based or pointer based 6 Figure 11-3 The data members for two sorted linear implementations of the ADT table for the data in Figure 11-1: (a) array based; (b) pointer based

Selecting an Implementation Nonlinear implementations Binary search tree implementation Offers several advantages over linear implementations 7 Figure 11-4 The data members for a binary search tree implementation of the ADT table for the data in Figure 11-1

Selecting an Implementation The requirements of a particular application influence the selection of an implementation Questions to be considered about an application before choosing an implementation What operations are needed? How often is each operation required? Are frequently used operations efficient given a particular implementation? 8

Comparing Linear Implementations Unsorted array-based implementation Insertion is made efficiently after the last table item in an array Deletion usually requires shifting data Retrieval requires a sequential search 9 Figure 11-5a Insertion for unsorted linear implementations: array based

Comparing Linear Implementations Sorted array-based implementation Both insertions and deletions require shifting data Retrieval can use an efficient binary search 10 Figure 11-6a Insertion for sorted linear implementations: array based

Comparing Linear Implementations Unsorted pointer-based implementation No data shifts Insertion is made efficiently at the beginning of a linked list Deletion requires a sequential search Retrieval requires a sequential search 11 Figure 11-5b Insertion for unsorted linear implementations: pointer based

Comparing Linear Implementations Sorted pointer-based implementation No data shifts Insertions, deletions, and retrievals each require a sequential search 12 Figure 11-6b Insertion for sorted linear implementations: pointer based TableA.cpp

Selecting an Implementation Linear Easy to understand conceptually May be appropriate for small tables or unsorted tables with few deletions Nonlinear Is usually a better choice than a linear implementation A balanced binary search tree Increases the efficiency of the table operations 13

Selecting an Implementation 14 Figure 11-7 The average-case order of the ADT table operations for various implementations

Selecting an Implementation for a Particular Application 15 A.Frequent insertions and infrequent traversals in no particular order Unsorted linear implementation B.Frequent retrievals Sorted array-based implementation –Binary search Balanced binary search tree C.Frequent retrievals, insertions, deletions, traversals Binary search tree (preferably balanced)

A Sorted Array-Based Implementation of the ADT Table Default constructor and virtual destructor Copy constructor supplied by the compiler Has a typedef declaration for a “visit” function Public methods are virtual Protected methods: setSize, setItem, and position 16

A Binary Search Tree Implementation of the ADT Table Reuses BinarySearchTree An instance is a private data member Default constructor and virtual destructor Copy constructor supplied by the compiler Public methods are virtual Protected method: setSize 17 TableB.cpp

The ADT Priority Queue: A Variation of the ADT Table Orders items by a priority value The deletion operation for a priority queue is different from the one for a table The item removed is the one having the highest priority value Priority queues do not have retrieval and traversal operations 18

The ADT Priority Queue: A Variation of the ADT Table 19 Figure 11-8 UML diagram for the class PriorityQueue

The ADT Priority Queue: Possible Implementations Sorted linear implementations Appropriate if the number of items in the priority queue is small Array-based implementation Maintains the items sorted in ascending order of priority value items[size - 1] has the highest priority 20 Figure 11-9a An array-based implementation of the ADT priority queue

The ADT Priority Queue: Possible Implementations Sorted linear implementations (continued) Pointer-based implementation Maintains the items sorted in descending order of priority value Item having the highest priority is at beginning of linked list 21 Figure 11-9b A pointer-based implementation of the ADT priority queue

The ADT Priority Queue: A Variation of the ADT Table Binary search tree implementation Appropriate for any priority queue Largest item is rightmost and has at most one child 22 Figure 11-9c A binary search tree implementation of the ADT priority queue

Heaps A heap is a complete binary tree That is empty or Whose root contains a search key >= the search key in each of its children, and Whose root has heaps as its subtrees 23

Heaps Note: The search key in each heap node is >= the search keys in each of the node’s children The search keys of a node’s children have no required relationship 24

Heaps The heap is a complete binary tree An array-based representation is attractive But need to know the heap’s maximum size 25 Figure A heap with its array representation

Heaps 26 Figure UML diagram for the class Heap

Heaps Maxheap A heap in which the root contains the item with the largest search key Minheap A heap in which the root contains the item with the smallest search key 27

Heaps: An Array-based Implementation of a Heap Constant MAX_HEAP Data members items : an array of heap items size : an integer equal to the current number of items in the heap 28

Heaps: heapDelete Strategy Step 1: Return the item in the root rootItem = items[0] Results in disjoint heaps 29 Figure 11-12a Disjoint heaps

Heaps: heapDelete Step 2: Copy the item from the last node into the root: items[0]= items[size-1] Step 3: Remove the last node: --size Results in a semiheap 30 Figure 11-12b A semiheap

Heaps: heapDelete Step 3: Transform the semiheap back into a heap Use the recursive algorithm heapRebuild The root value trickles down the tree until it is not out of place If the root has a smaller search key than the larger of the search keys of its children, swap the item in the root with that of the larger child 31

Heaps: heapDelete Efficiency heapDelete is O(log n) 32 Figure Deletion from a heap

Heaps: heapInsert Strategy Insert newItem into the bottom of the tree newItem trickles up to an appropriate spot in the tree 33 Figure Insertion into a heap Efficiency: O(log n) Heap.cpp

A Heap Implementation of the ADT Priority Queue Priority-queue operations and heap operations are analogous The priority value in a priority-queue corresponds to a heap item’s search key One implementation Has an instance of the Heap class as a private data member Methods call analogous heap operations 34

A Heap Implementation of the ADT Priority Queue Disadvantage Requires the knowledge of the priority queue’s maximum size Advantage A heap is always balanced Another implementation A heap of queues Useful when a finite number of distinct priority values are used, which can result in many items having the same priority value 35 PQ.cpp

Heapsort Strategy Transform the array into a heap Remove the heap's root (the largest element) by exchanging it with the heap’s last element Transforms the resulting semiheap back into a heap 36

Heapsort 37 Figure Transforming the array anArray into a heap Heapsort.cpp

Heapsort Compared to mergesort Both heapsort and mergesort are O(n * log n) in both the worst and average cases However, heapsort does not require a second array Compared to quicksort Quicksort is O(n * log n) in the average case It is generally the preferred sorting method, even though it has poor worst-case efficiency : O(n 2 ) 38

The STL Associative Containers Act as tables Elements are retrieved using a key that need not be an integral type The interfaces to all associative containers are similar Bidirectional iterators are provided Comparison is determined by either The default comparison operator <, or A comparison function object as a parameter 39

The STL Associative Containers map A sorted collection of (key, value) pairs Unique keys Provides the operator [ ] using keys as subscripts Month[“December”] = 31; set A sorted collection of objects, which represent both the key and value multimap and multiset Like map and set, except that they allow duplicate keys 40

The STL priority_queue The STL class priority_queue is an adaptor container The default underlying container is a vector Allows for insertion, but not iteration through its elements Only the top element can be accessed Priority is determined by either The default comparison operator <, or A comparison function object 41

The STL Heap Algorithms Heap algorithms for containers make_heap Turns an existing container into a heap sort_heap Turns the heap back into the original container push_heap Adds an item to a heap pop_heap Removes the first item from a heap Has the highest priority 42

Summary The ADT table supports value-oriented operations The linear implementations (array based and pointer based) of a table are adequate only in limited situations When the table is small For certain operations A nonlinear pointer based (binary search tree) implementation of the ADT table provides the best aspects of the two linear implementations Dynamic growth Insertions/deletions without extensive data movement Efficient searches 43

Summary A priority queue is a variation of the ADT table Its operations allow you to retrieve and remove the item with the largest priority value A heap that uses an array-based representation of a complete binary tree is a good implementation of a priority queue when you know the maximum number of items that will be stored at any one time 44

Summary Heapsort, like mergesort, has good worst-case and average-case behaviors, but neither sort is as good as quicksort in the average case Heapsort has an advantage over mergesort in that it does not require a second array 45