Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

how can I mirror a heap in Java heap class package Heap; import java.util.Queue; public class Heap { // We will assume a maximum heap

image text in transcribed

how can I mirror a heap in Java

heap class

package Heap;

import java.util.Queue;

public class Heap {

// We will assume a maximum heap dimension // You could easily change the code to re-size if the heap gets full // Here, we just throw an exception in this case private static int defaultDim = 1024;

HeapNode[] h; int last;

public Heap() { h = new HeapNode[defaultDim]; last = -1; }

// public Heap(Object[] o, int[] a) throws InvalidSizeException() { // if (a.length != o.length) // throw new InvalidSizeException(); // h = new HeapNode[a.length]; // for (int i = 0; i

public boolean isEmpty() { return (last

public void printHeap() { if (isEmpty()) { System.out.println("Heap is Empty"); return; } printByLevel(0, 0); }

private void printByLevel(int i, int level) { if (i > last) return; for (int j = 0; j

public Object findMin() throws HeapEmptyException { if (isEmpty()) throw new HeapEmptyException(); return h[0].content; }

private int getParent(int i) { if (i == 0) return -1; return ((i - 1) / 2); }

private int getLeft(int i) { return (i * 2 + 1); }

private int getRight(int i) { return (i * 2 + 2); }

private void swap(int i, int j) { HeapNode tmp = h[i]; h[i] = h[j]; h[j] = tmp; }

public void insert(Object o, int p) throws HeapFullException { // Find the right place to put this new node last++;

if (last >= h.length) throw new HeapFullException();

// Wrap the content in a node h[last] = new HeapNode(o, p);

// Bubble up this node if need be int i = last; int parent; for (parent = getParent(i); i > 0 && h[i].key

public Object removeMin() throws HeapEmptyException { if (last

// Get the value to return Object value = h[0].content;

// Now swap the last heap node for the root, and update the last index swap(0, last--);

// Bubble down the root value if need be int i = 0; boolean swapped; do { swapped = false; int left = getLeft(i); int right = getRight(i); if (left > last && right > last) { // we got to the end of the heap, so nothing lese to do i = last + 1; } else if (left

// **************************Get max function************************** public int getMax() throws HeapEmptyException { int max = 0; if (isEmpty()) { throw new HeapEmptyException(); } else { for (int i = last / 2; i

return max;

}

*****************************************************************

HeapNode

package Heap;

public class HeapNode { Object content; int key;

public HeapNode (Object o, int priority) { content = o; key = priority; } }

************************************************************************

1. Heap, 20pt) Add to the Heap class (minimum heap) provided to you in assignment 3 a method that takes a heap and outputs its mirror image. For example: [15%] Min Heap Your method should be called mirror Heap(Heap h). . Is the mirror of a heap is necessarily a heap? Give a two line explanation . The height of a mirror of a heap (with n nodes) is also necessarily . Test your code with example above. Print out the heap and its mirror for it. [2%] O(log(n))? Give a two line explanation for it. [2%) Note you can take the content of each node the string "mirror" 11%)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

1 2 3 Data Base Techniques

Authors: Dick Andersen

1st Edition

0880223464, 978-0880223461

More Books

Students also viewed these Databases questions