Lecture Notes for SE 598, Data Structures and Algorithms
9 August 2000, Implementing Maps and Multimaps
- use a linked-list - binary tree
- problem - binary trees can get out of balance; insertion order is
important
- solution - fix the binary tree if it gets out of balance
- problem - rebalancing a binary tree is expensive on each insertion
- solutions - don't rebalance perfectly, don't rebalance after every
insertion
- trade-off - the less perfect the tree, the less often the need to
rebalance
- examples
- avl trees - balance is near perfect (+- 1), rebalance on every
insertion
- red-black trees - balance is so-so (factor of 2), rebalance can be
much less frequent
- stl uses red-black trees
- use a vector - hash table
- vectors are fast to access but slow to find
- if a value can be associated with its index, then find would be fast
- suppose
"alphonse"
could be associated with 1
- but, the association must be fast
- such vectors are known as hash tables
- hash functions associate arbitrary values with indices
- hash functions turn arbitrary values into indices
- fast, constant (almost) associations
-
index = 0;
for (int i = sizeof(str) - 1; i >= 0; i--)
index += str[i] mod v.size()
- what's the problem - there are many strings but (relatively) few
indices
- collisions
- dealing with collisions
- use the first available free slot in the hash table - open addressing
- typedef pair hash_table_slot
- insert
- find
- advantages - simple
- disadvantages - fixed size, potentially slow lookup
- have each hash-table slot be the head of a chain - closed addressing
or chaining
- each hash-table slot (bucket) points to a linked list of values
- insert
- find
- advantage - simple, arbitrary size
- disadvantage - potentially slow lookup
- the stl may have hashed associative containers too
- advantages - fast
- problems - slow, no more sorting, difficult to use right
This page last modified on 9 August 2000.