h3-sol - Homework 3 Solutions Fundamental Algorithms Spring...

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

View Full Document Right Arrow Icon
Homework 3 Solutions Fundamental Algorithms, Spring 2008, Professor Yap Due: Wed Mar 5, in class. HOMEWORK with SOLUTION, prepared by Instructor and T.A.s INSTRUCTIONS: Please read questions carefully. When in doubt, please ask. Please write succinctly, to the point. You may post general questions to the homework discussion forum in class website. Also, bring your questions to recitation on Monday. 1. (16 Points) This is Exercise 2.1 (p.6) in Lecture III (please download the version dated Feb 27). Consider the dictionary ADT. (a) Describe algorithms to implement this ADT when the concrete data structures are linked lists. Try to be brief and give algorithms at the high level that we use in our lecture notes. (b) Analyze the worst complexity of your algorithms in (a). NOTE: complexity is be analyzed to Θ-order. (a’) Describe algorithms to implement this ADT when the concrete data structures are arrays instead of linked lists. (b’) Analyze the complexity of your algorithms in (a’). 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
SOLUTION: (a) We implement the dictionary ADT with linked lists as follows. First we describe how a linked list L represents a dictionary. The linked list L has a node called the head , and it is denoted by L. head . Every node u in the list has two ±elds, u. key and u. data . Thus ( u. key ,u. data ) represents an item stored in L . Also, u has a pointer u. next to the next node (unless u is the last node in which case u. next = nil ). From the node L. head , we can reach every node in the list using the next pointer. For simplicity, we assume there is a ±eld called L. tail that gives us the last node in the list. We use “shell programming” based on the following list traversal algorithm: ListTraversal ( L ): u L. head while ( u n = nil ) VISIT( u ) u u. next CLEANUP() The macros VISIT and CLEANUP can be empty initially. To implement the various other operations, we can rede±ne VISIT and CLEANUP appropriately. We now implement the 3 dictionary operations: (a) lookUp ( K ): We use the ListTraversal shell, but de±ne VISIT( u ) as the fol- lowing macro: V ISIT ( u ) : if ( u. key = K ) return ( u ) Thus, if the key K is found in u , we exit from the while-loop and return u . The CLEANUP() macro is: CLEANUP() : return ( nil ) (b) insert ( K,D ): We use the ListTraversal shell, but de±ne VISIT( u ) as the following macro: V ISIT ( u ) : if ( u. key = K ) return ( nil ) Thus, if the key K is found in u , we return nil , indicating failure (duplicate key). The CLEANUP() macro is:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/11/2010 for the course COMPUTER S 301 taught by Professor . during the Spring '10 term at Kadir Has Üniversitesi.

Page1 / 5

h3-sol - Homework 3 Solutions Fundamental Algorithms Spring...

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

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