Question: in a BinaryHeap class in java , how to write the code of function private void PrintHeap(int node, int level) full code(include class code and

in a BinaryHeap class in java, how to write the code of function private void PrintHeap(int node, int level)

in a BinaryHeap class in java, how to write the code offunction private void PrintHeap(int node, int level) full code(include class code andtest code) is given below //////////////BinaryHeap class code//////////// public class BinaryHeap {//////////////////////////// // private attributes //////////////////////////// private int[] arr; private int maxsize; //

full code(include class code and test code) is given below

//////////////BinaryHeap class code////////////

public class BinaryHeap { //////////////////////////// // private attributes //////////////////////////// private int[] arr; private int maxsize; // array capacity private int size; // number of items in heap //////////////////////////// // private methods //////////////////////////// protected void swap(int index1, int index2) { int tmp = arr[index1]; arr[index1] = arr[index2]; arr[index2] = tmp; } //Fixes partial ordering from i to root // PRE: // PARAM: i is index to be bubbled up from // POST: swaps i with parent if arr[i] > parent private void SiftUp(int i) {int parentIndex,temp;

if(i!=0)

{

parentIndex=i/2;

if(arr[parentIndex] > arr[i])

{

temp=arr[parentIndex];

arr[parentIndex]=arr[i];

arr[i]=temp;

//SiftUp[parentIndex];

} // int index = size; // int parentindex = i/2; // // while ((i!=0) && (arr[parentindex]>arr[index])) // { // // swap(index, parentindex); } } // Fixes partial ordering from i to leaf // PRE: // PARAM: i is index to be bubbled down from // POST: swaps i with parent if arr[i]

if (child >= size) // n is size of heap, index starting from 0

return; //i is leaf node

if (child + 1

child = arr[child]>arr[child+1]?child:(child+1); //select the largest child

if (arr[child] > arr[i]) { //if child is larger, swap and shiftdown again

int t= arr[child];

arr[child] = arr[i];

arr[i] = t; // int index = 0; // int leftindex = i*2; // int rightindex = i*2+1; // // // // while (leftindexarr[rightindex]) // { // smallerChild = rightindex; // } // // if (arr[index]>arr[smallerChild]) // { // swap(index, smallerChild); // } else { // // break; // }

// SiftDown(smallerChild); }

} // Recursively prints the heap structure as described // in lab specification // PRE: // PARAM: node is the current index, level is the depth of node // POST: private void PrintHeap(int node, int level) { } //////////////////////////// // public methods //////////////////////////// // Default constructor // PRE: // POST: creates and empty heap of size 10 public BinaryHeap() { maxsize = 10; arr = new int[maxsize]; size = 0; } // Constructor (int) // PRE: // PARAM: n - size of the heap // POST: creates and empty heap of size n public BinaryHeap(int n) { maxsize = n; arr = new int[maxsize]; size = 0; } // Inserts int into heap // PRE: Underlying array is not full // PARAM: x - integer to be inserted // POST: inserts x into heap, maintaining the heap // properties - grows array if heap is full public void Insert(int x) { if (size == maxsize) // heap is full { // grow the array maxsize = 2 * maxsize; int[] newarr = new int[maxsize]; for (int i = 0; i

////////// test code////////////

import java.util.Random;

public class CSCI225Lab7Driver { static Random r = new Random(System.nanoTime()); static final int numitems = 12; static final int maxvalue = 100; public static void main(String[] args) { HeapTest1(); HeapTest2(); } public static void HeapTest1() { System.out.println("Heap test 1"); BinaryHeap hp = new BinaryHeap(); hp.Insert(7); hp.Insert(1); hp.Insert(3); hp.Insert(13); hp.Insert(1); hp.Insert(4); System.out.println("Heap: "); hp.PrintHeap(); System.out.println(""); while (!hp.IsEmpty()) { try { System.out.println("Removed: " + hp.Remove()); System.out.println("Heap: "); hp.PrintHeap(); System.out.println(""); } catch (Exception e) { System.out.println(e.getMessage()); } } } public static void HeapTest2() { BinaryHeap hp = new BinaryHeap(); for (int i = 0; i Print Heap The PrintHeap method is to print the contents of the heap in a "tree-like" fashion. For example, if your heap (as an array) is [9, 7,8,4,6,2,0,1,3,5], then PrintHeap should output **4 ***1 ***3 5 *8 **2 0 First print the current element, then its left subtree, then its right subtree. Preface each element with a number of asterisks equal to its depth in the heap Once you are finished with this exercise, rewrite the PrintHeap method to print the heap in a different tree-like format

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!