Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

READ REQUIREMENTS PLEASE I got some problem with the code. Hope you can help me to finish the code. I know how to insert integers

READ REQUIREMENTS PLEASE

I got some problem with the code. Hope you can help me to finish the code.

I know how to insert integers only, but my homework was also insert with a string and I don't know how to do it. Please help me to correct the code to insert(int,String)

It is a heap tree in java.

Do not just copy and past a new code

image text in transcribed

private void swap(int fpos, int spos)

{

int tmp;

tmp = Heap[fpos];

Heap[fpos] = Heap[spos];

Heap[spos] = tmp;

}

private void minHeapify(int pos)

{

if (!isLeaf(pos))

{

if ( Heap[pos] > Heap[leftChild(pos)] || Heap[pos] > Heap[rightChild(pos)])

{

if (Heap[leftChild(pos)]

{

swap(pos, leftChild(pos));

minHeapify(leftChild(pos));

}else

{

swap(pos, rightChild(pos));

minHeapify(rightChild(pos));

}

}

}

}

public void insert(int element)

{

Heap[++size] = element;

int current = size;

while (Heap[current]

{

swap(current,parent(current));

current = parent(current);

}

}

public void print()

{

for (int i = 1; i

{

System.out.print(Heap[i]);

System.out.print(" ");

}

}

public void minHeap()

{

for (int pos = (size / 2); pos >= 1 ; pos--)

{

minHeapify(pos);

}

}

public int remove()

{

int popped = Heap[FRONT];

Heap[FRONT] = Heap[size--];

minHeapify(FRONT);

return popped;

}

public static void main(String...arg)

{

System.out.println("The Min Heap is ");

MinHeap minHeap = new MinHeap(15);

minHeap.insert(5,k);

minHeap.insert(3,j);

minHeap.insert(17,a);

minHeap.insert(10,b);

minHeap.insert(84,q);

minHeap.insert(19,w);

minHeap.insert(6,e);

minHeap.insert(22,n);

minHeap.insert(9,c);

minHeap.print();

System.out.println();

minHeap.remove();

minHeap.print();

}

}

public class MinHeap private intl] Heap; private int size; private int maxsize; private static final int FRONT-1 public MinHeap(int maxsize) this.maxsize maxsize this.size-O; Heap new int[this.maxsize 1 Heap[O] Integer.MIN_VALUE private int parent(int pos) return pos/2 private int leftChild(int pos) return (2 * pos); private int rightChild(int pos) return (2 pos) +1; private boolean isLeaf(int pos) if (pos >= (size / 2) && pos

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_2

Step: 3

blur-text-image_3

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

Automating Access Databases With Macros

Authors: Fish Davis

1st Edition

1797816349, 978-1797816340

More Books

Students also viewed these Databases questions

Question

What are the primary ways in which an LBO is financed?

Answered: 1 week ago

Question

What is a business model?

Answered: 1 week ago