PLEASE ANSWER ALL IN JAVa
how does adding a new element to a heap work? describe the
algorithm in detail




The heap insertion algorithm in pseudo code:
Insert a node containing the insertion value
in the "fartest left location" of the lowest level
of the Binary Tree
Filter the inserted node up using this algorithm:
while ( inserted node's value < value in its parent node )
{
swap the values of the respective node;
} |
JAVA CODE
import java.util.Arrays;
import java.util.NoSuchElementException;
/** Class BinaryHeap **/
public class BinaryHeap {
/** The number of children each node has **/
private static final int d = 2;
private int heapSize;
private int[] heap;
/** Constructor **/
public BinaryHeap(int capacity)
{
heapSize = 0;
heap = new int[capacity + 1];
Arrays.fill(heap, -1);
}
/** Function to check if heap is empty **/
public boolean isEmpty( )
{
return heapSize == 0;
}
/** Check if heap is full **/
public boolean isFull( )
{
return heapSize == heap.length;
}
/** Function to get index parent of i **/
private int parent(int i)
{
return (i - 1)/d;
}
/** Function to insert element */
public void insert(int x)
{
if (isFull( ) )
throw new NoSuchElementException("Overflow Exception");
/** Percolate up **/
heap[heapSize++] = x;
heapifyUp(heapSize - 1);
}
/** Function heapifyUp **/
private void heapifyUp(int childInd)
{
int tmp = heap[childInd];
while (childInd > 0 && tmp < heap[parent(childInd)])
{
heap[childInd] = heap[ parent(childInd) ];
childInd = parent(childInd);
}
heap[childInd] = tmp;
}
/** Function to print heap **/
public void printHeap()
{
System.out.print("\nHeap = ");
for (int i = 0; i < heapSize; i++)
System.out.print(heap[i] +" ");
System.out.println();
}
}
PLEASE ANSWER ALL IN JAVa how does adding a new element to a heap work? describe...
Please finish all the questions,Thanks
Following is Appendix
assume the definition of Java class Heap given in the Appendix For this question, a. (2 marks Consider the following heap 30 12 20 19 6 10 18 Given the array representation of a heap discussed in class, what is the array that corre sponds to this heap? b. (5 marks) Successively insert into the heap of part (a.) 22, 35 and 11, in that order. Use the standard heap insertion algorithm....
How does Bitcoin's Proof of Work consensus algorithm work? please explain in detail but also simply to understand.
in c++ please. thank you!
Page 4 of 4 5. Heap and heapsort: answer the following three questions. a) (1 pt) What is the definition of a max heap? | 0 b) (2 pts) When we insert an element, 5, to the following max heap, what would be the resulting max heap? Give the detailed procedure. (14) (10) c) (2 pts) Based on b), when we remove the root element in the max heap, what would be the resulting max...
c++ please answer b to e
P " b) (2 pts) When we insert an element, 5, to the following max heap, what would be the resulting max heap? Give the detailed procedure. 20 c) (2 pts) Based on b), when we remove the root element in the max heap, what would be the resulting max heap? Give the detailed procedure. d) (7 pts) Describe how HeapSort works. Then use the example 20 10 5 15 35 30 25 to...
How does Edge-Triggered D Flip Flops work? (Describe in Detail)
Describe Internet Video in detail,what protocols does it use? - how does it work from application point of view?
Which of the following statements does not describe a valid way of adding new lines to a proof: Making a substitution using a logical equivalence Adding the right hand side of a logical implication if the left hand side already appears in the proof Forming the AND of two lines that already appear Making a substitution using a logical implication
someone EXPLAIN this please( python )
2 marks] Consider the heap structure below, what does the heap look like after removing the top element from it using the method presented in lectures? 3 19 32 21 15 19 32 2115 15 19 21 22 32
Please discuss in DETAIL the mental healthcare sytem in the US. Explain every element and how it is/isn't effective. *Please answer in DETAIL and provide ALL websites used*
//implement the binomial heap please import java.util.LinkedList; import java.util.NoSuchElementException; /** * Binomial Heap Implementation * * @author First Last * @since ${date} */ public class BinomialHeap<T extends Comparable<? super T>> implements binomialHeapInterface<T> { private static final int DEFAULT = 5; // default size for the binomial heap public Node<T>[] forest; private int n; private boolean isMaxHeap; /** * Node Class for nodes in Binomial Heap */ protected class Node<T> { private T value; private int degree; private LinkedList<Node<T>> children; /**...