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....
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)
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
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
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*
Please answer in Java for an immediate upvote :) Given the following array of 8 elements, trace the merge sort algorithm. Assume the array is to be sorted in ascending order. 81 16 4 6 34 11 23 67 ANSWER: (Hint 6 – lines): Why don’t we select the median element of all the n-entries in the array to be our pivot point? ANSWER:
Short Answer. Under what conditions are adding or removing an element in a linked structure an O(1) operation? Under what conditions are adding or removing an element in an array-based structure an O(1) operation? How does the memory usage of an array-based structure compare to a linked structure? what structure/implementation would be the best choice for managing a collection where the order is not important, but the user needs frequent access to elements within the collection? Explain your choice.
Please do this in Java!!! Describe a method for performing a card shuffle of a singly linked list of 2n elements, by converting it into two lists. A card shuffle is a permutation where a list L is cut into two lists, L1 and L2, where L1 is the first half of L and L2 is the second half of L, and then these two lists are merged into one by taking the first element in L1, then the first...