logo CodeStepByStep logo

atLevel

Language/Type: C++ heaps arrays
Author: Marty Stepp (on 2016/06/16)

Consider the implementation of a class named HeapPriorityQueue, an implementation of a priority queue of integers using a vertically-ordered tree structure called a heap, represented as an array internally. Suppose that class is implemented using the members listed below. Write a member function named atLevel that could be added to the HeapPriorityQueue class. Your member function should accept as a parameter an integer representing a level in the heap and returns an integer count of the number of values the heap stores at that level, if any. The root level of the heap (i.e. the location of the minimum value) is level 1; the root's direct children are at level 2, and so on. An integer exception should be thrown if atLevel is called with a level of less than 1, or a level greater than the number of levels in the heap.

For example, if a variable named heap stores the data below, the call of heap.atLevel(4) would return 3 to represent the nodes why:39, oh:34, and do:14.

                            hi:-9
                       /             \
                ok:-4                    bye:2
            /          \                /     \
        you:33          me:5         who:32    us:22
       /      \         /
   why:39    oh:34    do:14

Constraints: You may call other member functions of the HeapPriorityQueue if you like, but your function should not modify the state of the heap. Its header should be declared in such a way as to indicate to the client that this function does not modify the list. Do not use any auxiliary data structures to solve this problem (no array, vector, stack, queue, string, etc). This function should run in O(log N) time or better, where N is the number of total elements stored in the heap.

Write the member function as it would appear in HeapPriorityQueue.cpp. You do not need to declare the function header that would appear in HeapPriorityQueue.h. Assume that you are adding this method to the HeapPriorityQueue class as defined below:

struct PQEntry {
    string data;
    int priority;
};

class HeapPriorityQueue {
public:
    HeapPriorityQueue();
    ~HeapPriorityQueue();
    void changePriority(string value, double newPriority);
    string dequeue();
    void enqueue(string value, double priority);
    bool isEmpty() const;
    int peek() const;
    int peekPriority() const;
    int size() const;
    string toString() const;
    
    your code goes here;

private:
    PQEntry* elements;   // array of element data
    int capacity;        // length of elements array
    int mysize;          // number of elements that have been added
};
Type your C++ solution code here:


This is a member function problem. Submit a member function that will become part of an existing C++ class. You do not need to write the complete class, just the member function described in the problem.

You must log in before you can solve this problem.


Log In

Need help?

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.