#C11006. Implementation of d-ary Heap Operations
Implementation of d-ary Heap Operations
Implementation of d-ary Heap Operations
Implement a d-ary heap with d = 3 that supports a set of operations. The heap should maintain a collection of integers and must support the following commands:
- insert x: Insert the integer x into the heap.
- extract_min: Remove and output the smallest element in the heap. Output
NULL
if the heap is empty. - delete x: Delete the element x from the heap if it exists. If there are multiple occurrences, remove any one.
- show: Display the current elements in the heap in increasing order separated by spaces. If the heap is empty, output
EMPTY
.
Your program should read from standard input and write to standard output. The first line of input contains an integer Q specifying the number of operations, followed by Q lines each containing one command.
Note: All formulas or specific conditions should be written in LaTeX format if needed. For example, the d-ary heap is defined with $d=3$ in this problem.
inputFormat
The input begins with an integer Q, the number of operations. Each of the following Q lines contains one command. Commands are of the following form:
insert x
extract_min
delete x
show
There is a single space between the command and the integer (when applicable).
outputFormat
For each operation that produces an output (extract_min
and show
), print the result on a new line. The output should be exactly as follows:
- If the heap is empty and a
show
command is issued, printEMPTY
. - If the heap is empty and an
extract_min
command is issued, printNULL
. - Otherwise, print the smallest element or the sorted list of elements as required.
12
insert 5
insert 3
insert 4
show
extract_min
show
delete 4
show
extract_min
show
extract_min
show
3 4 5
3
4 5
5
5
EMPTY
NULL
EMPTY
</p>