CSCI 2300 | Data Structures and Algorithms | Spring 2000 |
This lab will investigate the implementation of leftist heaps, and compare their performance to binary heaps.
You are also given the public interface to a class implementing a priority queue as a leftist heap. Read through the file comments and make sure you understand what each method is supposed to do. You will be providing the implementation for the class, and modifying the main program to use your new class.
Now you will implement the leftist heap.
Create the node class (you may find it simpler to make it an external class rather than nesting it in the leftist tree class). Remember that it should be a template class.
You will also need to write several private member functions, described below.
The first function will do all the work, but will make some assumptions about its arguments:
Recall that the algorithm for this recursive merge is as follows:
if the first node has no left child then make the second node the left child of the first otherwise: merge the second node with the right child of the first (do not recursively call this function directly; instead you will call the function below) the merged leftist tree becomes the right child of the first node, which is now the root of the merged tree ensure the leftist tree invariant is satisfied by swapping the children of the root node if the left child has a shorter null path length than the right update the null path length of the root node; it will be one more than the null path length of its right child
The second function simply ensures that the assumptions required by the first are satisfied and then calls it. To do this:
If you have made it this far, you are almost done. All that remains is to implement the public member functions, which will be easy given the private functions above.
At the conclusion of this lab, take a moment to review the following:
You have explored the implementation of leftist heaps, and compared their performance to binary heaps.