03 - CMPT 307 Data Structures and Algorithms Outline...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPT 307 Data Structures and Algorithms Outline Solutions to Exercises on Elementary Data Structures 1. Suggest how storage for elements can be allocated and deallocated within the hash table itself by linking all unused slots into a free list. Assume that one slot can store a flag and either one element plus a pointer or two pointers. All dictionary and free-list operations should run in O (1) expected time. Does the free list need to be doubly linked, or does a singly linked free list suffice? The flag in each slot will indicate whether the slot is free. A free is in the free list, a doubly linked list of all free slots in the table. The slot thus contains two pointers. A used slot contains an element and a pointer (possibly Nil) to the next element that hashes to this slot. (Of course, that pointer points to another slot in the table.) Operations: (1) Insertion If the element hashes to a free slot, just remove the slot from the free list and store the element there (with a Nil pointer). The free list must be doubly linked in order for this deletion to run in O (1) time. If the element hashes to a used slot j , check whether the element x already there belongs there (its key also hashes to slot j ). If so, add the new element to the chain of elements in this slot. To do so, allocate a free slot (e.g. take the head of the free list) for the new element and put this new slot at the head of the list pointed to by the hashed-to slot ( j ). If not, E is part of another slots chain. Move it to a new slot by allocating one from the free list, copying the old slots ( j s) content (element x and pointer) to the new slot. Then insert the new element in the non-empty slot as usual. To update the pointer to j , it is necessary to find it by searching the chain of elements starting in the slot x hashes to....
View Full Document

This note was uploaded on 11/19/2009 for the course CS CMPT 307 taught by Professor A.bulatov during the Fall '09 term at Simon Fraser.

Page1 / 3

03 - CMPT 307 Data Structures and Algorithms Outline...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online