12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

204 Chap. 5 Binary Trees5.5 Implement a priority queue class based on the max-heap class implementationof Figure 5.19. The following methods should be supported for manipulatingthe priority queue:void enqueue(int ObjectID, int priority);int dequeue();void changeweight(int ObjectID, int newPriority);Method enqueue inserts a new object in<strong>to</strong> the priority queue with ID numberObjectID <strong>and</strong> priority priority. Method dequeue removes theobject with highest priority from the priority queue <strong>and</strong> returns its object ID.Method changeweight changes the priority of the object with ID numberObjectID <strong>to</strong> be newPriority. The type for E should be a class thats<strong>to</strong>res the object ID <strong>and</strong> the priority for that object. You will need a mechanismfor finding the position of the desired object within the heap. Use anarray, s<strong>to</strong>ring the object with ObjectID i in position i. (Be sure in yourtesting <strong>to</strong> keep the ObjectIDs within the array bounds.) You must alsomodify the heap implementation <strong>to</strong> s<strong>to</strong>re the object’s position in the auxiliaryarray so that updates <strong>to</strong> objects in the heap can be updated as well in thearray.5.6 The Huffman coding tree function buildHuff of Figure 5.29 manipulatesa sorted list. This could result in a Θ(n 2 ) algorithm, because placing an intermediateHuffman tree on the list could take Θ(n) time. Revise this algorithm<strong>to</strong> use a priority queue based on a min-heap instead of a list.5.7 Complete the implementation of the Huffman coding tree, building on thecode presented in Section 5.6. Include a function <strong>to</strong> compute <strong>and</strong> s<strong>to</strong>re in atable the codes for each letter, <strong>and</strong> functions <strong>to</strong> encode <strong>and</strong> decode messages.This project can be further extended <strong>to</strong> support file compression. To do sorequires adding two steps: (1) Read through the input file <strong>to</strong> generate actualfrequencies for all letters in the file; <strong>and</strong> (2) s<strong>to</strong>re a representation for theHuffman tree at the beginning of the encoded output file <strong>to</strong> be used by thedecoding function. If you have trouble with devising such a representation,see Section 6.5.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!