For binary heap, heapify-up, heapify-down, insert, delete min/max, heap sort
pls give examples with solutions in C
Answer:
The complete code for heap sort is given below, which includes insert/delete, and heapify.
please have a look and let me know if further assistance is required.
code:
#include <iostream>
using namespace std;
void heapifyArray(int array[], int a, int m) // a is the size of
the heap, hepify will be done of the root node with the node
present at the index m
{
int large = m; // Root will be initialized as largest
int leftChild = 2*m + 1; // Assign left child
int rightChild = 2*m + 2; // Assign right child
if (leftChild < a && array[leftChild] > array[large])
// If left child is bigger than the root
large = leftChild;
if (rightChild < a && array[rightChild] >
array[large]) // If right child is bigger than the root
large = rightChild;
if (large != m) // If the root is not the largest of all
{
swap(array[m], array[large]); //swap the values
heapifyArray(array, a, large); // Recursion is applied here to
hepify the rest
}
}
void sortHeap(int array[], int a) //fucntion definition of heap
sort function
{
for (int m = a / 2 - 1; m >= 0; m--) // This will build a
heap
heapifyArray(array, a, m);
for (int m=a-1; m>=0; m--) // This will extract the elements
from the heap
{
swap(array[0], array[m]); // Send root element to the bottom of
almost complete binary tree
heapifyArray(array, m, 0); // Now again do the hepify operation to
do build heap
}
}
void displayArray(int array[], int length) // To display the
array
{
for (int i=0; i < length; i++)
printf("%d ", array[i]);
printf("\n");
}
// Main function is given below
int main()
{
int array[] = {45, 32, 87, 2, 6, 99, 39, 76, 54, 19, 36, 12}; //
Array declaration with initialization
int a = sizeof(array)/sizeof(array[0]); // For calculating current
size fo the given array
sortHeap(array, a); // Function of Insertion Sort is called
here
printf("Sorted array: \n"); // Displaying the sorted array
displayArray(array, a); // To display the array
return 0;
}
Output:
Please give it a thumbs up if this helped you, also provide your valuable feedback.
For binary heap, heapify-up, heapify-down, insert, delete min/max, heap sort pls give examples wi...
Give the psuedo code for inserting and deleting an element in a binary min heap and binary max heap data structure. Discuss the worst case running time for each pseudo code.
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...
1. Consider the following unordered list: 20, 35, 25, 10, 40, 50, 45. Perform heap sort to sort this list in nondecreasing (ascending) order. a. Perform the bottom-up method to arrange these values into a max heap. Show the heapify operations on each relevant subtree. (10 points) b. Show the tree representation and the array representation of these numbers after every dequeue operation. Remember that dequeue does not delete a number. Dequeue will instead remove that number from the heap...
Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each node has a parent field as well as the usual left child, right child, and key fields. -Condition : Do not use Array representation. Use the following structure. typedef struct node *treePointer; typedef struct node { int key; treePointer parent; treePointer leftChild, rightChild; }; - INPUT i k : Insert the node with the key value of k in...
Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each node has a parent field as well as the usual left child, right child, and key fields. -Condition : Do not use Array representation. Use the following structure. typedef struct node *treePointer; typedef struct node { int key; treePointer parent; treePointer leftChild, rightChild; }; - INPUT i k : Insert the node with the key value of k in...
Task: A ternary heap is like a binary heap, except instead of each node having a max of two children, each node has a max of three children. Given a ternary min heap, return true if it is a valid heap. That is, it satisfies the heap property (the parent is less than all of its children) Requirements: Implement an isValidHeap function to determine whether a particular array of integers constitutes a valid ternary min-heap. bool isValidHeap(int arr[])// example declaration...
1. Which of the following is a proper array representation a binary min heap?2. A heap is implemented using an array. At what index will the right child of node at index i be found? Note, the Oth position of the array is not used.Select one:a. i/2b. 2 i+1c. i-1d. 2 i3. Consider the following array of length 6. Elements from the array are added, in the given order, to a max heap. The heap is initially empty and stored as an array.A={18,5,37,44,27,53}What...
NOTE: Completing the Third Chart is the most
important. This is one question with three parts.
(4 pts) Is the following array-based tree a min-heap or a max-heap or not a heap at all? 85 91 S8 95 100 92 a. Min-heap b. Max-heap c. Not a heap 5 pts) Turn the following array-based binary tree into a max-heap. Show your work step by step. (You will not need all the columns) 34 7 12 47 19 5 pts) Show...
[12] 3. a) Draw the binary min-heap after inserting the following values, one after another. 21, 13, 12, 25, 4, 20, 16, 1, 11 You must show each step of building the heap and eventually the final tree. Please, put your final tree inside a box so that it can be easily understood among other intermediate trees. b) A 4-ary max heap is like a binary max heap, but instead of 2 children, nodes have 4 children. A 4-ary heap...
C++ Question Can I get an example of a max heap that has an insert function, delete function and a print function? Please use namespace std and iostream and whatever else is needed I guess.