Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMSC 341 Lecture 12.

Similar presentations


Presentation on theme: "CMSC 341 Lecture 12."— Presentation transcript:

1 CMSC 341 Lecture 12

2 Announcements Postponing Priority Queues until later

3

4 HashTable Class template <class HashedObj> class HashTable {
public: explicit HashTable(const HashedObj) &notFound, size=101); HashTable(const HashTable &rhs) : ITEM_NOT_FOUND(rhs.ITEM_NOT_FOUND),theLists(rhs.theLists){} const HashedObj &find(const HashedObj &x) const; void makeEmpty(); void insert (const HashedObj &x); void remove (const HashedObj &x); const HashTable &operator=(const HashTable &rhs); private: vector<List<HashedObj>> theLists; const HashedObj ITEM_NOT_FOUND; };

5 Handling Collisions Revisited
Open addressing (aka closed hashing) all elements stored in the table itself (so table should be large. Rule of thumb: M>= 2N) upon collision, item is hashed to a new (open) slot. Hash function h: U X {0,1,2,….}  {0,1,…,M-1} h(k,I) = (h’(k) + f(I))mod m for some h’: U  {0,1,…,M-1} and f(0) = 0 Each try is called a probe

6 Linear Probing Function: f(i) = ci Example:
h’(k) = k mod 10 in a table of size 10 , f(i) = i U={89,18,49,58,69} Example: h’(k) = k mod 10 in a table of size 10 (not prime, but easy to calculate) U={89,18,49,58,69} f(I) = I 1. 89 hashes to 9 2. 18 hashes to 8 3. 49 hashes to 9, collides with 89 h(k,1) = (49%10+1)%10=0 4. 58 hashes to 8, collides with 18 h(k,1)=(50%10+1)%10=9, collides with 89 h(k,2)=(50%10+2)%10=0, collides with 49 h(k,3)=(50%10+3)%10=1 5. 69 hashes to 9, collides with 89 h(69,1) = (h’(69)+f(1))mod 10 = 0, collides with 49 h(69,2) = (h’(69+f(2))mod 10 =0, collides with58 h(69,3) = (h’(69)+f(3))mod 10 = 2

7 Linear Probing (cont) Problem: Clustering
when table starts to fill up, performance  O(N) Asymptotic Performance insertion and unsuccessful find, average # probes  1/2(1+1/(1-)2) if   1, the denominator goes to zero and the number of probes goes to infinity

8 Linear Probing (cont) Remove
Can’t just use the hash function(s) to find the object,and remove it, because objects that were inserted after x were hashed based on x’s presence. Can just mark the cell as deleted so it won’t be found anymore. Other elements still in right cells Table can fill with lots of deleted junk

9 Quadratic Probing Function: f(i) = c2i2 + c1i + c0 Example:
f(i) = i2, m=10 U={89,18,49,58,69} hashes to 9 2. 18 hashes to 8 3. 49 hashes to 9, collision with 89 hashes to 8, collides with 18 5. 69 hashes to 9

10 Quadratic Probing (cont.)
Advantage: reduced clustering problem Disadvantages: reduced number of sequences no guarantee that empty slot will be found if tablesize is not prime For example: h(k,I) = (h’(k)+I^2)mod M h(k, I) = h(k) + 0 + 1 + 4 + 9 + 16 + 25 + 26 with m = 10, h’(k) = k mod 10 key I= | | | | duplicate values to right of line I cannot exceed 5 in this table, only 6 slot available for each key If table size is prime, an open slot is guaranteed when <= 1/2.

11 Double Hashing Use two hash functions: h’1(k), h’2(k)
h(k,I) = (h’1(k) + ih’2(k)) mod M Choosing h’2(k) don’t allow h’2(k) = 0 for any k. a good choice: h’2(k) = R - (k mod R) with R a prime smaller than M Characteristics No clustering problem Requires a second hash function

12 Balanced Trees Problem
specific tree configuration dependent on order in which nodes are inserted may become noticeably unbalanced, leading to performance approaching worst case -- O(n) Solution ensure that trees remain balanced (or pretty balanced), no matter what

13 AVL Trees Method impose balance condition: The height of the left and right subtrees of a node can differ by no more than one. adjust structure after each insertion or deletion to maintain balance condition; uses rotation Pros and Cons guarantee O(lg n) performance lots of adjustments can make insertion expensive

14 Rotation Operation To rotate about a node t and its left child l:
t->left = l->right; l->right = t; To rotate about a node t and its right child r: t->right = r->left; r->left = t;

15 Splay Trees Concept adjust tree in response to accesses to make common operations efficient after access node is moved to root by splaying Performance amortized such that m operations take O(m lg n) where n is the number of insertions

16 Splay Operation Traverse tree from node x to root, rotating along the way until x is the root Each rotation If x is root, do nothing. If x has no grandparent, rotate x about its parent. If x has a grandparent, if x and its parent are both left children or both right children, rotate the parent about the grandparent, then rotate x about its parent if x and its parent are opposite type children (one left and the other right), rotate x about its parent, then rotate x about its new parent (former grandparent)

17


Download ppt "CMSC 341 Lecture 12."

Similar presentations


Ads by Google