EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019

Slides:



Advertisements
Similar presentations
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Advertisements

The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
1 Hash Tables Gordon College CS Hash Tables Recall order of magnitude of searches –Linear search O(n) –Binary search O(log 2 n) –Balanced binary.
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 20 / 2009 Instructor: Michael Eckmann.
Sorting Chapter 13 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
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.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Searching:
Hashing Dr. Yingwu Zhu.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
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.
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.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
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”
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.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
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.
Lecture on Data Structures(Trees). Prepared by, Jesmin Akhter, Lecturer, IIT,JU 2 Properties of Heaps ◈ Heaps are binary trees that are ordered.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Hashing.
Heap Chapter 9 Objectives Define and implement heap structures
Priority Queues and Heaps
October 30th – Priority QUeues
March 31 – Priority Queues and the heap
Bohyung Han CSE, POSTECH
Hash functions Open addressing
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
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.
Description Given a linear collection of items x1, x2, x3,….,xn
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Part-D1 Priority Queues
Instructor: Dr. Michael Geiger Spring 2017 Lecture 34: Inheritance
B-Tree Insertions, Intro to Heaps
Binary Tree Application Operations in Heaps
Tree Representation Heap.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Hash Tables Chapter 12.7 Wherein we throw all the data into random array slots and somehow obtain O(1) retrieval time Nyhoff, ADTs, Data Structures and.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
CS202 - Fundamental Structures of Computer Science II
Advanced Implementation of Tables
Ch. 12 Tables and Priority Queues
Priority Queues & Heaps
Final Review Dr. Yingwu Zhu.
Adapted from instructor resource slides
Sorting Dr. Yingwu Zhu.
CS 367 – Introduction to Data Structures
Heaps By JJ Shepherd.
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
Data Structures for Shaping and Scheduling
Heaps and priority queues
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
Instructor: Dr. Michael Geiger Spring 2017 Lecture 36: Exam 3 Preview
Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps
Instructor: Dr. Michael Geiger Spring 2017 Lecture 33: Hash tables
Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019 Lecture 33: Priority queues Hash tables

Data Structures: Lecture 33 Lecture outline Announcements/reminders Program 4 due today Program 5 due 5/9 (extra credit) Course evals to be posted online; return at final Final exam: Wednesday, 5/8, 3-6 PM, Ball 208 Today’s lecture Priority queues Hash tables 7/1/2019 Data Structures: Lecture 33

Data Structures: Lecture 33 Review: Trees A data structure which consists of a finite set of elements called nodes or vertices a finite set of directed arcs which connect the nodes If the tree is nonempty one of the nodes (the root) has no incoming arc every other node can be reached by following a unique sequence of consecutive arcs Binary tree: tree in which each node has at most two children Binary search tree: binary tree in which each node follows property value in left child of node ≤ value in node ≤ value in right child of node 7/1/2019 Data Structures: Lecture 33

Data Structures: Lecture 31 Review: Heaps A heap is a binary tree with properties: It is complete Each level of tree completely filled (except possibly bottom) It satisfies heap-order property Data in each node >= data in children Also called a maxheap If data in node <= data in children, it’s a minheap Can be efficiently stored in array Store all nodes at given level from left to right in consecutive locations If first element @ index 1, then, given index n Children are at indexes 2*n and 2*n + 1 Parent is at index n/2 7/1/2019 Data Structures: Lecture 31

Data Structures: Exam 3 Preview Review: Heapsort Given a list of numbers in an array Stored in a complete binary tree Convert to a heap Begin at last node not a leaf Apply percolate down to this subtree Continue 7/1/2019 Data Structures: Exam 3 Preview

Data Structures: Exam 3 Preview Review: Heapsort Now swap element 1 (root of tree) with last element This puts largest element in correct location Use percolate down on remaining sublist Converts from semi-heap to heap 7/1/2019 Data Structures: Exam 3 Preview

Data Structures: Exam 3 Preview Review: Heapsort Again swap root with rightmost leaf Continue this process with shrinking sublist 7/1/2019 Data Structures: Exam 3 Preview

Data Structures: Lecture 31 Priority Queue A collection of data elements Items stored in order by priority Higher priority items removed ahead of lower Operations Constructor Insert Find, remove smallest/largest (priority) element Replace Change priority Delete an item Join two priority queues into a larger one 7/1/2019 Data Structures: Lecture 31

Data Structures: Lecture 31 Priority Queue Implementation possibilities As a list (array, vector, linked list) As an ordered list Best is to use a heap Basic operations have O(log2n) time Priority queue as heap: use priority for ordering Highest priority item will always be first All priority queue operations can be written in terms of heap operations 7/1/2019 Data Structures: Lecture 31

Data Structures: Lecture 33 Hash Tables Recall order of magnitude of searches Linear search O(n) Binary search O(log2n) Balanced binary tree search O(log2n) Unbalanced binary tree can degrade to O(n) 7/1/2019 Data Structures: Lecture 33

Data Structures: Lecture 33 Hash Tables In some situations faster search is needed Solution is to use a hash function Value of key field given to hash function Location in a hash table is calculated 7/1/2019 Data Structures: Lecture 33

Data Structures: Lecture 33 Hash Functions Simple function could be to mod the value of the key by some arbitrary integer int h(int i) { return i % someInt; } Note the max number of locations in the table will be same as someInt Note that we have traded speed for wasted space Table must be considerably larger than number of items anticipated 7/1/2019 Data Structures: Lecture 33

Data Structures: Lecture 33 Hash Functions Observe the problem with same value returned by h(i) for different values of i Called collisions A simple solution is linear probing Linear search begins at collision location Continues until empty slot found for insertion 7/1/2019 Data Structures: Lecture 33

Data Structures: Lecture 33 Hash Functions When retrieving a value linear probe until found If empty slot encountered then value is not in table If deletions permitted Slot can be marked so it will not be empty and cause an invalid linear probe 7/1/2019 Data Structures: Lecture 33

Data Structures: Lecture 33 Hash Functions Strategies for improved performance Increase table capacity (less collisions) Use different collision resolution technique Devise different hash function Hash table capacity Size of table must be 1.5 to 2 times the size of the number of items to be stored Otherwise probability of collisions is too high 7/1/2019 Data Structures: Lecture 33

Data Structures: Lecture 33 Collision Strategies Linear probing can result in primary clustering Consider quadratic probing Probe sequence from location i is i + 1, i – 1, i + 4, i – 4, i + 9, i – 9, … Secondary clusters can still form Double hashing Use a second hash function to determine probe sequence 7/1/2019 Data Structures: Lecture 33

Data Structures: Lecture 33 Collision Strategies Chaining Table is a list or vector of head nodes to linked lists When item hashes to location, it is added to that linked list 7/1/2019 Data Structures: Lecture 33

Improving the Hash Function Ideal hash function Simple to evaluate Scatters items uniformly throughout table Modulo arithmetic not so good for strings Possible to manipulate numeric (ASCII) value of first and last characters of a name 7/1/2019 Data Structures: Lecture 33

Data Structures: Lecture 33 Final notes Next time: Exam 3 Preview Reminders: Program 4 due today Program 5 due 5/9 (extra credit) Course evals to be posted online; return at final Final exam: Wednesday, 5/8, 3-6 PM, Ball 208 7/1/2019 Data Structures: Lecture 33

Data Structures: Lecture 33 Acknowledgements These slides are adapted from slides provided with the course textbook: Larry Nyhoff, ADTs, Data Structures, and Problem Solving with C++, 2nd edition, 2005, Pearson/Prentice Hall. ISBN: 0-13-140909-3 7/1/2019 Data Structures: Lecture 33