Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Exercise: Heaps Background A heap is a binary tree - based data structure that satisfies the heap property. In a max heap, the value of
Exercise: Heaps
Background
A heap is a binary treebased data structure that satisfies the heap property. In a max heap, the value of any parent node is greater than or equal to the values of its
children. Conversely, in a min heap, the value of any parent node is less than or equal to the values of its children. Heaps are commonly used for priority queues,
resource allocation, and sorting algorithms.
Task
Your task is to implement a max or min heap. Create a Heap class, which can build either a min or max heap, depending on the build option: max or min. When
complete, calculate the BigO for your algorithm.
Specification
Initializer: Initializes an empty heap and sets the build type to max or min.
insertvalue: Inserts a value into the heap while maintaining the min or max heap property:
extract: Removes and returns the min or max value from the heap.
peek: Returns the min or max value without removing it
size: Returns the number of elements in the heap.
isempty: Returns True if the heap is empty, otherwise False.
Implement the necessary helper methods eg heapify, swap, etc. to maintain the max heap property during insertion and extraction
Requirements
Your implementation should use an arraybased representation for the heap. You may use the heap example code as a starting point. Java and Python versions are
not available. If you need assistance translating the C code into Java or Python, let me know. You can assume that the input values are integers.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started