Assume that we start with a random array of size n= 2k-1 and form a heap.
a) What is the probability that the third largest element will be a child of the root? Justify
b) Before you form a heap, you notice that none of the three smallest elements are near the
top of the array, or more formally none of them are in any of the first (n-3)/4 locations
of the array. What is the probability that the third smallest element will be the parent of
a leaf? Justify
#include <iostream> using namespace std; void MAX_HEAPIFY(int a[], int i, int n) { int l,r,largest,loc; l=2*i; r=(2*i+1); if((l<=n)&&a[l]>a[i]) largest=l; else largest=i; if((r<=n)&&(a[r]>a[largest])) largest=r; if(largest!=i) { loc=a[i]; a[i]=a[largest]; a[largest]=loc; MAX_HEAPIFY(a, largest,n); } } void BUILD_MAX_HEAP(int a[], int n) { for(int k = n/2; k >= 1; k--) { MAX_HEAPIFY(a, k, n); } } void HEAPSORT(int a[], int n) { BUILD_MAX_HEAP(a,n); int i, temp; for (i = n; i >= 2; i--) { temp = a[i]; a[i] = a[1]; a[1] = temp; MAX_HEAPIFY(a, 1, i - 1); } } int main() { int n; cout<<"Enter the size of the array"<<endl; cin>>n; int a[n]; cout<<"Enter the elements in the array"<<endl; for (int i = 1; i <= n; i++) { cin>>a[i]; } HEAPSORT(a, n); cout<<":::::::SORTED FORM::::::"<<endl; for (int i = 1; i <= n; i++) { cout<<a[i]<<endl; } }
Assume that we start with a random array of size n= 2k-1 and form a heap....
Problem 8. (Heap Top-k) Prof Dubious has made the following claim, and has provided a proof Claim. Let n and k be positive integers such that 2*-1n. In amax-heap H of n elements, the top 21 elements are in the first k layers of the heap. Proof. Since is a max-heap, each node in H must satisfy the heap property, i.e., if H, is an element of H with at least one child then Hmaxchldren(H)). We know that every subtree...
5. A three-heap with n elements can be stored in an array A, where A[O] contains the root of the tree. a) Draw the three-heap that results from inserting 5, 2, 8, 3, 6, 4, 9, 7, 1 in that order into an initially empty three-heap. You do not need to show the array representation of the heap. You are only required to show the final tree, although if you draw intermediate trees. b) Assuming that elements are placed in...
Discrete Mathematics
Time Complexity Analysis Due: May 9th, 2019 Math 4 6026 Heap Sort Another algorithm for sorting uses a specialized tree structure called a "heap." Specifically, we will use a binary heap, which is like a binary tree with hierarchy. Here is an example of a binary heap structure 1. 2. There is a top vertex, called the parent vertex (aka node). The top parent vertex connects to two vertices a level below. These vertices are the "left child"...
Data Structures using C
BuildHeap and Heap Sort In preparation: If you have not done so already, you should complete Worksheet 33 to leam more about the heap data structure. In some applications it is useful to void buildHeap (struct dyArray heap) { initialize a Heap with an existing vector int max = dy Array Size(heap); int i; of values. The values are not assumed for (i = max/2-1; i >= 0; i--) to be organized into a heap, and...
Min heap class implementation in Python.
Implement a min-using an array. Your min-heap class will have one private attribute, an array of integers. Implement the following methods for the min-heap class You may not use the built-in min function. init - Constructor makes an empty heap str - Prints the heap out in any way you want for debugging only) makenull(self) - Makes the heap empty insert(self,x) - Insert element x into the heap parent(self,i) - Returns the index of...
Complete HeapPriorityQueue (7 points). In lecture we implemented HeapPriorityQueue using an array-based representation of a heap (a complete binary tree whose entries satisfy the heap-order property). For this problem, complete the included HeapPriorityQueue class by using the LinkedBinaryTree class to represent a heap. Hint: the most challenging part of this problem is identifying the last Position in the heap and the next available Position in the heap. It is suggested that you review the array-based heap to better understand how...
Consider the following complete binary tree is stored in an array the way we learned during heap lecture. The root node is stored at index 1. The last node (47) is stored at index heapsize. If you want to build a heap from the array using the heapify(), what position of the array you start doing percolate Down in heapify()? Answer: What is the run-time to build a heap from an array of size n using heapify() process ? OC...
In class, we discussed the priority queue (PQ) ADT implemented using min-heap. In a min-heap, the element of the heap with the smallest key is the root of the binary tree. On the other hand, a max-heap has as root the element with the biggest key, and the relationship between the keys of a node and its parent is reversed of that of a min-heap. We also discussed an array-based implementation of heaps. In this assignment, your task is to...
Implement the class MaxHeapPriorityQueue as a heap with the following operations: • MaxHeapPriorityQueue() creates a new heap that is empty. It needs no parameters and returns nothing. Note that as discussed in the video lecture, the index range of the array implementation of a heap is 1:n, NOT 0:n-1 • parent(index) returns the value of the parent of heap[index]. index is between 1 and the size of the heap. If index<=1 or index>size of the heap, it returns None •...
Write a second constructor that could be added to the HeapPriorityQueue class. This constructor accepts an array of elements as a parameter and uses that array as the heap rather than creating a new array. Of course, the array passed in is probably not in proper heap ordering, so you must rearrange it until it is. There's a neat trick for achieving this: If you just "bubble down" all of the non-leaf nodes of the heap, starting from the last...