you will create a simple heap data structure with several methods including maxHeapify and buildMaxHeap. To get started, import the starter file (Heap.java) into the heap package you create in a new Java Project. Please do not change any of the method signatures in the Heap class. Implement the methods described below. You are free to test your code however you prefer. Remember that arrays are indexed starting at 0 in Java. This will change the math in the pseudocode. public int parent(int i) This method should return the index of the parent of the ith element of the data array. If the ith element is the root, then the method should return -1. public int left(int i) This method should return the index of the left child of the ith element of the data array. If the ith element is a leaf, then the index will potentially be greater than the size of the data array. That’s fine. public int right(int i) This method should return the index of the right child of the ith element of the data array. If the ith element is a leaf, then the index will potentially be greater than the size of the data array. That’s fine. public void maxHeapify(int loc) The method converts the tree rooted at the loc element in the array to a maxHeap. It assumes that the left and right children of loc are maxHeaps. public void buildMaxHeap() The method converts the data array into a maxHeap. Note that the entire data array might not be converted into a MaxHeap, just the portion of the array up until HeapSize.  this is the method signature: package heap; import java.util.Arrays; public class Heap {          private int[] data;     private int heapSize;          public Heap(int[] data,int heapSize) {         this.data = data;         this.heapSize = heapSize;     }          public void setHeapSize(int i) {         heapSize = i;     }          public int getHeapSize() {         return heapSize;     }          //return the parent of ith element in the array.     //should return -1 if the ith element is the root of the heap     public int parent(int i) {         return 0;              }          //returns the index of the left child of the ith element in the array     //for leaves the index will be greater than the heapSize     public int left(int i) {         return 0;              }          //returns the index of the right child of the ith element in the array     //for leaves the index will be greater than the heapSize     public int right(int i) {         return 0;              }          //modifies the data array so that the tree rooted at the loc element     //is a max heap.     //Assumes that the trees rooted at the left and right children of loc     //are max heaps     public void maxHeapify(int loc) {                               }          //converts the data array to an array that represents a max heap of size HeapSize     public void buildMaxHeap() {                       }          //helper method for debugging and printing     public String toString() {                  return Arrays.toString(Arrays.copyOfRange(data, 0, heapSize));     } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

you will create a simple heap data structure with several methods including
maxHeapify and buildMaxHeap. To get started, import the starter file (Heap.java) into the heap
package you create in a new Java Project
. Please do not change any of the method signatures in
the Heap class. Implement the methods described below. You are free to test your code however you
prefer.

Remember that arrays are indexed starting at 0 in Java. This will change the math in the
pseudocode.
public int parent(int i)
This method should return the index of the parent of the ith element of the data array. If the ith
element is the root, then the method should return -1.
public int left(int i)
This method should return the index of the left child of the ith element of the data array. If the ith
element is a leaf, then the index will potentially be greater than the size of the data array. That’s
fine.
public int right(int i)
This method should return the index of the right child of the ith element of the data array. If the ith
element is a leaf, then the index will potentially be greater than the size of the data array. That’s
fine.
public void maxHeapify(int loc)
The method converts the tree rooted at the loc element in the array to a maxHeap. It assumes that
the left and right children of loc are maxHeaps.
public void buildMaxHeap()


The method converts the data array into a maxHeap. Note that the entire data array might not be converted into a MaxHeap, just the portion of the array up until HeapSize.
 this is the method signature:

package heap;

import java.util.Arrays;

public class Heap {
    
    private int[] data;
    private int heapSize;
    
    public Heap(int[] data,int heapSize) {
        this.data = data;
        this.heapSize = heapSize;
    }
    
    public void setHeapSize(int i) {
        heapSize = i;
    }
    
    public int getHeapSize() {
        return heapSize;
    }
    
    //return the parent of ith element in the array.
    //should return -1 if the ith element is the root of the heap
    public int parent(int i) {
        return 0;
        
    }
    
    //returns the index of the left child of the ith element in the array
    //for leaves the index will be greater than the heapSize
    public int left(int i) {
        return 0;
        
    }
    
    //returns the index of the right child of the ith element in the array
    //for leaves the index will be greater than the heapSize
    public int right(int i) {
        return 0;
        
    }
    
    //modifies the data array so that the tree rooted at the loc element
    //is a max heap.
    //Assumes that the trees rooted at the left and right children of loc
    //are max heaps
    public void maxHeapify(int loc) {
        
                
    }
    

    //converts the data array to an array that represents a max heap of size HeapSize
    public void buildMaxHeap() {
        
        
    }
    
    //helper method for debugging and printing
    public String toString() {
        
        return Arrays.toString(Arrays.copyOfRange(data, 0, heapSize));

    }


}

Building a Heap: Prove
buildMax Heap (arr,n)
1
arr.heapSize = n
2 for i=[n/2] downto 0
3
maxHeapify (arr, i)
Lo
Transcribed Image Text:Building a Heap: Prove buildMax Heap (arr,n) 1 arr.heapSize = n 2 for i=[n/2] downto 0 3 maxHeapify (arr, i) Lo
Runtime of Max-Heapify
maxHeapify(arr,i)
left (i)
right (i)
1
1
2 r =
الله
3 if l≤arr.heapSize and arr[1]>arr[i]
4
LO
5
6
=
7
8
9
10
11
largest
else
= 1
largest
=
= i
if r≤arr.heapSize and arr[r]>arr[largest]
largest = r
if largest i
swap arr[i] and arr[largest]
maxHeapify (arr, largest)
Transcribed Image Text:Runtime of Max-Heapify maxHeapify(arr,i) left (i) right (i) 1 1 2 r = الله 3 if l≤arr.heapSize and arr[1]>arr[i] 4 LO 5 6 = 7 8 9 10 11 largest else = 1 largest = = i if r≤arr.heapSize and arr[r]>arr[largest] largest = r if largest i swap arr[i] and arr[largest] maxHeapify (arr, largest)
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Adjacency Matrix
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education