140 likes | 147 Views
ADTs so far. Lower level ADTs. Linked lists (singly- and doubly- linked) “ Stretchable ” , efficient, but … Sequential access Arrays Even more efficient, but not “ stretchable ” Random Access Vectors, Sequences, Lists All the features, but pay in efficiency
E N D
Lower level ADTs • Linked lists (singly- and doubly- linked) • “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 Gene Itkis; cs112
Higher Level ADTs For all: • isEmpty() • size() • insert(elem) • remove() • Stacks • last inserted • Queues • first inserted • Priority Queues • highest priority Gene Itkis; cs112
Dictionaries Retrieve by key
Dictionary Examples • Phone directory • 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. Gene Itkis; cs112
Dictionary Interface • isEmpty() • size() • insert(elem) • Using elem.key • find(key) • remove(key) 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 • find • Binary search: O(lg n) – pretty fast • insert • O(n) – slow (delete: the same) Gene Itkis; cs112
Better Implementations • Ordered trees • Hash tables • Other • Skip-lists Gene Itkis; cs112
x y z heap Ordered Trees • Order • x< y,z : min at root –heap () • 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) Gene Itkis; cs112
Ordered Trees • Searching • Easy • Insert/Delete • 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 Gene Itkis; cs112
Hash tables • Example • Find professors by office number • Find tools in a tool-box • Might not work for everyone • Idea • “Figure” info location from the key Gene Itkis; cs112
Hash Tables: Idea Hash table key’ • Hash function • H(key)=i • If it works… • Find – O(1) • Insert/Delete – O(1) • Problem? • Collisions: • H(key’)=H(key) key … H i keyinfo … Gene Itkis; cs112
Hash table key’ key H … d d … Hash Table: Issues 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’) keyinfo keyinfo key’info key’info key”info 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 Gene Itkis; cs112