ADTs so far
Lower level ADTs Linked lists (singly- and doubly- linked) Arrays “Stretchable”, efficient, but… Sequential access Arrays Even more efficient, but not “stretchable” Random Access Vectors, Sequences, Lists All the features, but pay in efficiency Really, implemented with the above 1/13/2019 Gene Itkis; cs112
Higher Level ADTs For all: isEmpty() size() insert(elem) remove() Stacks last inserted Queues first inserted Priority Queues highest priority 1/13/2019 Gene Itkis; cs112
Dictionaries Retrieve by key
Dictionary Examples Phone directory Student records Credit cards DB Name is key; phone # is the info Reverse lookup: phone # is key Student records Credit cards DB E.g. check that the given credit card is valid Extra: “Authenticate” the result ETC. 1/13/2019 Gene Itkis; cs112
Dictionary Interface isEmpty() size() insert(elem) find(key) Using elem.key find(key) remove(key) 1/13/2019 Gene Itkis; cs112
Simple Implementations Unordered List insert O(1) – fast (delete: the same) find O(n) – slow Ordered list Linked list implementation – same as unordered Array Binary search: O(lg n) – pretty fast O(n) – slow (delete: the same) 1/13/2019 Gene Itkis; cs112
Better Implementations Ordered trees Hash tables Other Skip-lists 1/13/2019 Gene Itkis; cs112
Ordered Trees Order Depth x< y,z : min at root – heap () x y<x<z : search Depth Shallow Balanced There might be exceptions: e.g., Leftist heaps “Strong” balance Approximate E.g., depth of leaves within factor of 2 (R-B trees) heap 1/13/2019 Gene Itkis; cs112
Ordered Trees Searching Insert/Delete Easy Destroys balance Fix balance How? AVL trees Nodes keep children heights Rotate when needed: when children heights are >1 apart Red-Black/2-3-4 trees 1/13/2019 Gene Itkis; cs112
Hash tables Example Idea Find professors by office number Find tools in a tool-box Might not work for everyone Idea “Figure” info location from the key 1/13/2019 Gene Itkis; cs112
Hash Tables: Idea Hash function If it works… Problem? H(key)=i Find – O(1) Insert/Delete – O(1) Problem? Collisions: H(key’)=H(key) key … H i key info … 1/13/2019 Gene Itkis; cs112
Hash Table: Issues Good Hash functions Collision Resolution … key H Hash table key’ key” Good Hash functions Minimize collision chances “Random looking” Collision Resolution Chaining Open Addressing Linear Probing: d=1 Quadratic Probing: d=i2 Double Hashing: d=H2(key’) key info key info d key’ info key’ info d key” info 1/13/2019 Gene Itkis; cs112
Collision Resolution Methods Comparison Chaining Requires extra space As with linked list Stretchable Can degenerate to linked-list search Open Addressing No extra space needed Has size limit Also can degenerate to unordered list search 1/13/2019 Gene Itkis; cs112