12048 - Tsai_DS_1025 Max Heap   

Description

Please consider the following c++ struct code without STL. Implement some max heap function. You need to use array representaion.

#include <iostream>
using namespace std;
template <class T>
class MaxPQ {
public:
    MaxPQ(T);
    ~MaxPQ();
    bool isEmpty() const;
    T& top() const;
    void push(const T&);
    void pop();
    void PostorderTrav(T);
    
private:
    T *heap;
    int heapSize;
    int capacity;
};

 

Hint:

1. implement the main function and seven functions.

MaxPQ:constructor i.e initialize private member

~MaxPQ:destructor i.e delete a heap array

isEmpty():check if priority queue is empty

top():return reference to the maximum element

push():insert an element to the priority queue

pop():delete the element with maximum priority

PostorderTrav():print all element with post order traversal in the priority queue

2. PostorderTrav() can use recursive format

Input

There are multiple lines in the input.

The first line contains an integer N(0 < N <= 1000), indicating the number of instructions.

According to the different instructions, operating the different tasks.

Tree height is smaller than 10.

Output

isEmpty():put "Yes" if the priority queue is empty, "No" otherwise.

top():if the priority queue is empty, print zero. Otherwise, print maximum element, you need to print '\n' behind an element.

print():called PostorderTrav() then print all elements with post order traversal in the priority queue, you need to print a space after every element.

pop() : if left child is the same to right child, please swap with left child.

You need to print '\n' at the end of the line.

Sample Input  Download

Sample Output  Download

Tags




Discuss