COSC2007 Data Structures II Chapter 12 Tables & Priority Queues I.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II Chapter 11 Tables & Priority Queues I.
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Dictionaries Chapter Chapter Contents Specifications for the ADT Dictionary Entries and methods Using the ADT Dictionary English Dictionary Telephone.
1 Heaps & Priority Queues (Walls & Mirrors - Remainder of Chapter 11)
CS 171: Introduction to Computer Science II
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
© 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.
© 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.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Heaps.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
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
Marc Smith and Jim Ten Eyck
COSC 2007 Data Structures II
Copyright © Wondershare Software Introduction to Data Structures Prepared by: Eng. Ahmed & Mohamed Taha.
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
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.
Dictionaries CS 105. L11: Dictionaries Slide 2 Definition The Dictionary Data Structure structure that facilitates searching objects are stored with search.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
ADTs and C++ Ch 2,3,4 May 12, 2015 Abstract Data Type (ADT) An abstract data type is: 1.A collection of data items (storage structures) 2.Basic operations.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
AVL Trees Amanuel Lemma CS252 Algoithms Dec. 14, 2000.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Tables and Priority Queues.
COSC 1030 Lecture 9 Binary Trees. Topics Basic Concept and Terminology Applications of Binary Tree Complete Tree Representation Traversing Binary Trees.
“Enthusiasm releases the drive to carry you over obstacles and adds significance to all you do.” – Norman Vincent Peale Thought for the Day.
Chapter 13 A Advanced Implementations of Tables. © 2004 Pearson Addison-Wesley. All rights reserved 13 A-2 Balanced Search Trees The efficiency of the.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
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.
COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
Fall 2015CS202 - Fundamental Structures of Computer Science II1 Heaps Appropriate for problems that must manage data by value. Some important operations.
Dictionaries CS /02/05 L7: Dictionaries Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
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.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Dictionaries. Reading Weiss Chap. 5, Sec
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
April 27, 2017 COSC Data Structures I Review & Final Exam
CSI 312 Dr. Yousef Qawqzeh Heaps and Priority Queue.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Dictionaries CS 110: Data Structures and Algorithms First Semester,
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
FALL 2005CENG 213 Data Structures1 Review. FALL 2005CENG 213 Data Structures2 Collection of items Problems that must manage data by value. Some important.
Chapter 12 A Tables.
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
CSCI 210 Data Structures and Algorithms
Binary Search Tree Chapter 10.
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}
Tables and Priority Queues
Presentation transcript:

COSC2007 Data Structures II Chapter 12 Tables & Priority Queues I

S. Xu 2 Topics Tables Operations Implementation

S. Xu 3 ADT Table Table example: City population City (key)CountryPopulation (sorted) AthensGreece 2,500,000 BarcelonaSpain 1,800,000 CairoEgypt16,500,000 LondonEngland 9,400,000 New YorkU.S.A 7,300,000 ParisFrance 2,200,000 TorontoCanada 3,200,000 VeniceItaly 300,000 Questions (if we use BST or other ADT to store) What is the population of London? Which city is in Italy?

S. Xu 4 ADT Table Table example: City population City (key)CountryPopulation (sorted) AthensGreece 2,500,000 BarcelonaSpain 1,800,000 CairoEgypt16,500,000 LondonEngland 9,400,000 New YorkU.S.A 7,300,000 ParisFrance 2,200,000 TorontoCanada 3,200,000 VeniceItaly 300,000 Questions (if we use BST or other ADT to store) What is the population of London? Which city is in Italy? Search can not be always done by using binary search

S. Xu 5 ADT Table Dictionary Member Record keystudent namehw Stan Smith Sue Margolin Billie King Roy Miller39...

S. Xu 6 ADT Table ADT Table (Dictionary) Elements are records containing several pieces of information Value oriented – Data are arranged to facilitate search Implementation Needs rapid retrieval of items Assumption: All items in a table have distinct search keys Some tables allow duplicate search keys Items should not be inserted if they already exist in the table

S. Xu 7 ADT Table

S. Xu 8 ADT Table Example: Populations Which implementation will be best suited for each of the following operations? Display in alphabetical order, the name of each city & its population Increase the population of each city by 10 Delete all cities with population < 1,000,000

S. Xu 9 ADT Table Table example: City population City (key)CountryPopulation (sorted) AthensGreece 2,500,000 BarcelonaSpain 1,800,000 CairoEgypt16,500,000 LondonEngland 9,400,000 New YorkU.S.A 7,300,000 ParisFrance 2,200,000 TorontoCanada 3,200,000 VeniceItaly 300,000

S. Xu 10 ADT Table Operations: KeyedItem Class public abstract class KeyedItem { private Comparable searchKey; public KeyedItem(Comparable key ) { searchKey = key; } public Comparable getKey() { return searchKey; } // end getKey } // end KeyedItem class Only one constructor is available, and not setKey () is defined --- Why?

S. Xu 11 ADT Table Example: Populations public class City extends KeyedItem { private string country; //city name will be the searchKey private int Pop; public City (String theCity, String theCountry, int newPop ) { super (theCity); country = theCountry; pop=newPop; }// constructors public string toString () { return getKey()+”, “ +country+” “ + pop; } // other methods }// end City class

S. Xu 12 ADT Table Example: Display in alphabetical order, the name of each city & its population The order of the visit is important Method to display an item should be passed as the Iterator +displayItem(anItem) Display anItem.cityName( ) Display anItem.getPopulation ( ) Increase the population of each city by 10 The order of the visit is not important Method to update the population should be passed as the Iterator +updatePopulation(anItem) anItem.setPopulation(1.1 * anItem.getPopulation( ))

S. Xu 13 ADT Table Example: Delete all cities with population < 1,000,000 The order of the visit is not important method to delete populations less than the desired should be passed as the iterator +deleteSmall(Table, anItem) if (anItem.getPopulation( ) < 1,000,000) t.tableDelete(anItem) Problem: Table changes (item is deleted) during traversal

S. Xu 14 ADT Table Implementation Linear implementation of tables could be: Unsorted Array based Reference based Sorted (by search key) Array based Reference based Both implementations should maintain a count of the items in the table More?

S. Xu 15 ADT Table Implementation ADT Table can also be implemented using ADT list Sorted List BST

S. Xu 16 ADT Table Implementation Example Sorted (by search key) Array based reference based Athens Barcelona... Venice... 9 size 01 size-1 MAX_TABLE -1 AthensBarcelonaVenice 9 size head

S. Xu 17 ADT Table Implementation Using ADT BST New York Cairo Venice ParisLondon Barcelona RomeAthens Toronto 9 size

S. Xu 18 ADT Table How to choose an implementation?

S. Xu 19 ADT Table How to choose an implementation? Factors affecting the selection of an implementation: Necessary operations Expected frequency of occurrence of each operation Required response times for the operations

S. Xu 20 Scenario A: Insert & Traverse (no Particular Order) Example: Raise money for local Charity Insert fund-raiser ideas into a table and later print a report Items can be sorted or unsorted No operation requires a specific order  Maintaining items in a specific order has no advantage For array based implementation, insert items after the last item in the array (Big O??) For reference based implementation, insert items at the beginning of the linked list (Big O??) Others? Why?

S. Xu 21 Scenario A: Insert & Traverse (no Particular Order) Insertion for unsorted linear implementation Array-based Reference-based Athens Barcelona... Venice... K+1 New item K-1 K K+1... AthensBarcelonaVenice K+1 New item

S. Xu 22 Scenario B: Retrieval Example: Word Processor's Thesaurus Frequent retrievals require table implementation that allows efficient searching for items No deletions or insertions are necessary Which implementation? Reference-based implementation? Binary search performs fast retrieval? What is the problem? Sorted array-based implementation? A Balanced BST would also be suitable, why?

S. Xu 23 Scenario B: Retrieval Example: Word Processor's Thesaurus Frequent retrievals require table implementation that allows efficient searching for items No deletions or insertions are necessary Which implementation? Reference-based implementation?,must traverse whole the LL Binary search performs fast retrieval? What is the problem? Impractical for reference-based Sorted array-based implementation? good A Balanced BST would also be suitable, why? Since the thesaurus does not change

S. Xu 24 Scenario C: Insertion, Deletion, Retrieval, & Traversal - Sorted Order Example: Computerized Library Catalog Elements are sorted Retrieval is the most frequent operation Both insertion & deletion perform two steps: 1. Find the appropriate position in the table 2. Insert into (or delete from) this position We need both steps together. How about efficiency? If the table is sorted, both tableInsert & tableDelete will require amount of time ~ (O(N)) in either array or reference-based implementations More?

S. Xu 25 Scenario C: Insertion, Deletion, Retrieval, & Traversal - Sorted Order Example: Computerized Library Catalog Elements are sorted Retrieval is the most frequent operation Both insertion & deletion perform two steps: 1. Find the appropriate position in the table 2. Insert into (or delete from) this position We need both steps together. How about efficiency? If the table is sorted, both tableInsert & tableDelete will require amount of time ~ (O(N)) in either array or reference-based implementations More? BST implementation combines the feature of the two linear implementations

S. Xu 26 Scenario C: Insertion, Deletion, Retrieval, & Traversal - Sorted Order Insertion for sorted linear implementation Array-based Reference-based New item Data Data... Data K+1 items 0i-1i i+1 K... Data New item Data head

S. Xu 27 Conclusion Linear implementations Less sophisticated & generally require more time than BST implementation Appropriate for tables containing small number of items BST implementation Better if they have minimum height We can keep the height of the tree near Log 2 (N) by using the tree balancing algorithms Reference-based implementation of BST Permits dynamic allocation and can handle tables whose maximum size is unknown Efficiently perform insertions & deletions ADT Table is appropriate when you have a data base to maintain and search by value

S. Xu 28 Comparison of Time Complexity (average) Operation Insertion Deletion Retrieval Traversal Unsorted ArrayO(1)O(n) O(n)O(n) Unsorted reference O(1)O(n) O(n)O(n) Sorted Array O(n)O(n) O(logn)O(n) Sorted reference O(n)O(n) O(n)O(n) BST O(logn)O(logn) O(logn)O(n)

29 Review The ADT table is also known as a ______. map queue Heap dictionary

30 Review An array based implementation of an ADT is a ______ implementation. vertical linear nonlinear compound

31 Review Which of the following is true about a linear implementation of a table? the unsorted implementations must insert a new item into its proper position as determined by the value of its search key the sorted implementations can insert a new item into any convenient location the sorted implementations maintain a count of the current number of items in the table the unsorted implementations do not maintain a count of the current number of items in the table

32 Review A(n) ______ implementation of a table is nonlinear. list linked list binary search tree array

33 Review In an unsorted array-based implementation of a table, a new item is inserted at location ______. items[size] items[size-1] items[size+1] items[size+2]

34 Review In an unsorted array-based implementation of the ADT table, the insertion operation is ______. O(1) O(n) O(n 2 ) O(log n)

35 Review In an unsorted array-based implementation of the ADT table, the retrieval operation is ______. O(1) O(n) O(n 2 ) O(log n)

36 Review The sorted reference-based implementation of the tableDelete operation is ______. O(1) O(log n) O(n) O(n 2 )