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