DataStruct 9. Hash Tables, Maps, Skip Lists, and Dictionaries

Similar documents
Dictionaries-Hashing. Textbook: Dictionaries ( 8.1) Hash Tables ( 8.2)

CH 9 : MAPS AND DICTIONARIES

Maps, Hash Tables and Dictionaries. Chapter 10.1, 10.2, 10.3, 10.5

9.2 Hash Tables. When implementing a map with a hash table, the goal is to store item (k, e) at index i = h(k)

DataStruct 8. Heaps and Priority Queues

Data Structures Lecture 12

Priority Queue Sorting

CSED233: Data Structures (2017F) Lecture10:Hash Tables, Maps, and Skip Lists

Chapter 9: Maps, Dictionaries, Hashing

DataStruct 7. Trees. Michael T. Goodrich, et. al, Data Structures and Algorithms in C++, 2 nd Ed., John Wiley & Sons, Inc., 2011.

This lecture. Iterators ( 5.4) Maps. Maps. The Map ADT ( 8.1) Comparison to java.util.map

CSCE 110 PROGRAMMING FUNDAMENTALS

CSI33 Data Structures

Ch 8. Operator Overloading, Friends, and References

CSCI-1200 Data Structures Fall 2018 Lecture 21 Hash Tables, part 1

CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

DataStruct 5. Stacks, Queues, and Deques

Iterators. node UML diagram implementing a double linked list the need for a deep copy. nested classes for iterator function objects

19.1 The Standard Template Library

Structuur van Computerprogramma s 2

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

CSCI-1200 Data Structures Fall 2009 Lecture 20 Hash Tables, Part II

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

Dictionaries. 2/17/2006 Dictionaries 1

CSCI-1200 Data Structures Fall 2017 Lecture 17 Trees, Part I

A linear structure is an ordered (e.g., sequenced) arrangement of elements.

EECE.3220: Data Structures Spring 2017

Ch 5-2. Arrays Part 2

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

COEN244: Class & function templates

CSCI-1200 Data Structures Spring 2015 Lecture 18 Trees, Part I

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Intermediate Programming, Spring 2017*

Linked List using a Sentinel

Due Date: See Blackboard

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CSCI-1200 Data Structures Fall 2017 Lecture 18 Trees, Part II

SETS AND MAPS. Chapter 9

Ch 7. Constructors and Other Tools

CSCI-1200 Data Structures Spring 2018 Lecture 16 Trees, Part I

A linear structure is an ordered (e.g., sequenced) arrangement of elements.

CSCI-1200 Data Structures Fall 2015 Lecture 24 Hash Tables

CSCI-1200 Data Structures Spring 2014 Lecture 19 Hash Tables

STL: C++ Standard Library

What is a List? elements, with a linear relationship. first) has a unique predecessor, and. unique successor.

1. The term STL stands for?

CE221 Programming in C++ Part 1 Introduction

Lab 2: ADT Design & Implementation

CSCI-1200 Data Structures Fall 2017 Lecture 23 Functors & Hash Tables, part II

CPSC 427a: Object-Oriented Programming

Ch 6-1. Structures. March 30, Prof. Young-Tak Kim

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCI-1200 Data Structures Fall 2018 Lecture 19 Trees, Part III

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

C++_ MARKS 40 MIN

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

CSC 427: Data Structures and Algorithm Analysis. Fall 2004

Set Implementation Version 1

CSC212 Data Structure - Section FG

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Ch 6. Structures and Classes

CS302 - Data Structures using C++

Container Notes. Di erent Kinds of Containers. Types Defined by Containers. C++11 Container Notes C++11

Chapter 17: Linked Lists

CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists!

Data Structures and Algorithms

CSI33 Data Structures

CSCI-1200 Data Structures Spring 2018 Lecture 18 Trees, Part III

class for simulating a die (object "rolled" to generate a random number) Dice(int sides) -- constructor, sides specifies number of "sides" for the die

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

STL Containers, Part II

void insert( Type const & ) void push_front( Type const & )

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Exercise 6.2 A generic container class

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Comp 11 - Summer Session Hashmap

ICOM 4035 Data Structures. Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Design Project - Object-oriented Programming for Tetris Game

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid

the pointer range [first, last) into the tree

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid. Fall 2018

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

Do not write in this area A.1 A.2 A.3 A.4 A.5 A.6 B.1 B.2 B.3 TOTAL. Maximum possible points:

W3101: Programming Languages C++ Ramana Isukapalli

Ch 4. Parameters and Function Overloading

Example Final Questions Instructions

Lab 4 - Lazy Deletion in BSTs

Hashing. mapping data to tables hashing integers and strings. aclasshash_table inserting and locating strings. inserting and locating strings

Outline and Reading. The Stack ADT ( 4.2.1) Applications of Stacks ( 4.2.3) Array-based implementation ( 4.2.2) Growable array-based stack.

Tutorial AVL TREES. arra[5] = {1,2,3,4,5} arrb[8] = {20,30,80,40,10,60,50,70} FIGURE 1 Equivalent Binary Search and AVL Trees. arra = {1, 2, 3, 4, 5}

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

List, Stack, and Queues

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

2. (5 Points) When calculating a combination (n choose k) the recursive definition is n k =1 if

Transcription:

2013-2 DataStruct 9. Hash Tables, Maps, Skip Lists, and Dictionaries Michael T. Goodrich, et. al, Data Structures and Algorithms in C++, 2 nd Ed., John Wiley & Sons, Inc., 2011. November 22, 2013 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, College of Engineering, Yeungnam University, KOREA (Tel : +82-53-810-2497; Fax : +82-53-810-4742 http://antl.yu.ac.kr/; E-mail : ytkim@yu.ac.kr)

Outline Maps Hash Tables Ordered Maps Skip Lists Dictionaries DS 9-2

9.1 Maps A map models a searchable collection of keyvalue entries The main operations of a map are for searching, inserting, and deleting items Multiple entries with the same key are not allowed Applications: address book student-record database (labeled folder) (folder) (file of cabinet) DS 9-3

Entry ADT An entry stores a key-value pair (k,v) Methods: key(): return the associated key value(): return the associated value setkey(k): set the key to k setvalue(v): set the value to v template <typename K, typename V> class Entry { // a (key, value) pair public: // public functions Entry(const K& k = K(), const V& v = V()) // constructor : _key(k), _value(v) { } const K& key() const { return _key; } // get key const V& value() const { return _value; } // get value void setkey(const K& k) { _key = k; } // set key void setvalue(const V& v) { _value = v; } // set value private: // private data K _key; // key V _value; // value }; DS 9-4

The Map ADT find(k): if the map M has an entry with key k, return and iterator to it; else, return special iterator end put(k, v): if there is no entry with key k, insert entry (k, v), and otherwise set its value to v. Return an iterator to the new/modified entry erase(k): if the map M has an entry with key k, remove it from M size(), empty() begin(), end(): return iterators to beginning and end of M DS 9-5

Example Operation Output Map empty() true Ø put(5,a) p1: [(5,A)] (5,A) put(7,b) p2: [(7,B)] (5,A),(7,B) put(2,c) p3: [(2,C)] (5,A),(7,B),(2,C) put(2,e) p3: [(2,E)] (5,A),(7,B),(2,E) find(7) p2: [(7,B)] (5,A),(7,B),(2,E) find(4) end (5,A),(7,B),(2,E) find(2) p3: [(2,E)] (5,A),(7,B),(2,E) size() 3 (5,A),(7,B),(2,E) erase(5) (7,B),(2,E) erase(2) (7,B) find(2) end (7,B) DS 9-6

C++ Map Interface template <typename K, typename V> class Map { // map interface public: class Entry; // a (key,value) pair class Iterator; // an iterator (and position) int size() const; // number of entries in the map bool empty() const; // is the map empty? Iterator find(const K& k) const; // find entry with key k Iterator put(const K& k, const V& v); // insert/replace pair (k,v) void erase(const K& k) // remove entry with key k throw(nonexistentelement); void erase(const Iterator& p); // erase entry at p Iterator begin(); // iterator to first entry Iterator end(); // iterator to end entry }; DS 9-7

Class Iterator for Map<K, V> class Iterator { // an iterator for the list public: Iterator(Entry* u); // create from node Iterator(); // default constructor Elem& operator*(); // reference to the element bool operator==(const Iterator& p) const; // compare positions bool operator!=(const Iterator& p) const; Iterator& operator++(); // move to next position Iterator& operator--(); // move to previous position friend class EntryList; // give EntryList access private: Entry* v; // pointer to the node }; DS 9-8

STL map class size() : return the number of elements in the map empty() : return true if the map is empty, and false otherwise find(k) : find the entry with key k and return an iterator to it; if no such key exists, return end operator[k] : produce a reference to the value of key k; if no such key exists, create a new entry for key k insert(pair(k, v)) : insert pair (k, v), returning an iterator to its position erase(k) : remove the element with key k erase(p) : remove the element referenced by iterator p begin() : return an iterator to the beginning of the map end() : return an iterator just past the end of the map DS 9-9

Example usage of STL map /** STL map */ #include <iostream> #include <string> #include <map> using namespace std; void main() { map<string, int> mymap; // a (string,int) map map<string, int>::iterator p; // an iterator to the map mymap.insert(pair<string, int>("rob", 28)); // insert ("Rob",28) mymap["joe"] = 38; mymap["joe"] = 50; mymap["sue"] = 75; p = mymap.find("joe"); mymap.erase(p); mymap.erase("sue"); p = mymap.find("joe"); if (p == mymap.end()) cout << "nonexistent\ n"; // insert("joe",38) // change to ("Joe",50) // insert("sue",75) // *p = ("Joe",50) // remove ("Joe",50) // remove ("Sue",75) // outputs: "nonexistent" } for (p = mymap.begin(); p!= mymap.end(); ++p) { // print all entries cout << "(" << p->first << "," << p->second << ")\ n"; } DS 9-10

A Simple List-Based Map We can efficiently implement a map using an unsorted list We store the items of the map in a list S (based on a doublylinked list), in arbitrary order header nodes/positions trailer 9 c 6 c 5 c 8 c entries DS 9-11

The find Algorithm Algorithm find(k): input: a key k output: the position of the matching entry of L, or end if there is no key k in L for each position p [L.begin(), L.end()) do if p.key() = k then return p return end // there is no entry with key equal to k DS 9-12

The put Algorithm Algorithm put(k,v): input: a key-value pair (k, v) output: the position of the inserted/modified entry for each position p [L.begin(), L.end()) do if p.key() = k then *p (k, v) return p p L.insertBack((k,v)) // there is no entry with key k n n + 1 return p // increment number of entries DS 9-13

The erase Algorithm Algorithm erase(k): input: a key k output: none for each position p [L.begin(), L.end()) do if p.key() = k then L.erase(p) n n 1 // decrement number of entries DS 9-14

Performance of a List-Based Map Performance: put takes O(n) time since we need to determine whether it is already in the sequence find and erase take O(n) time since in the worst case (the item is not found) we traverse the entire sequence to look for an item with the given key The unsorted list implementation is effective only for maps of small size or for maps in which puts are the most common operations, while searches and removals are rarely performed (e.g., historical record of logins to a workstation) DS 9-15