Question
C++ Write a function (heapify) to satisfy the heap property from an array A. In order to implement the heapify function, please implement the below
C++ Write a function (heapify) to satisfy the heap property from an array A. In order to implement the heapify function, please implement the below functions first: int getParent(int index); // Purpose: calculates the index of a node's parent node // Post: integer value returned to caller int getLeftChild(int index); // Purpose: calculates the index of node's left child // Post: integer value returned to caller int getRightChild(int index); // Purpose: calculates the index of a node's right child // Post: integer value returned to the user void swap(int& item1, int& item2); // Purpose: exchanges two values // Post: item1 = old item2 and item2 = old item1, results // returned through reference parameter 2. Write a function to build a heap (buildHeap). 3. Test output with an array A[10] = {4,1,3,2,16,9,10,14,8,7}. 4. Create a function to insert numbers to the heap. 5. Test the insert function with inserting 20 and 17 sequentially to the heap. Function Prototypes: int getParent(int index); int getLeftChild(int index); int getRightChild(int index); void swap(int& item1, int& item2); void heapify(int A[], int index, int size); void build_heap(int A[], int size); void printArray(int A[], int size); void heapInsert(int A[], int &size, int Item);
SAMPLE RUN:
Print Array A: 4, 1, 3, 2, 16, 9, 10, 14, 8, 7
Parent of the node 5 is the node 2
Left child of the node 3 is the node 7
Right child of the node 3 is the node 8
Calling Heapify A, 1: 4, 16, 3, 2, 7, 9, 10, 14, 8, 1
Build heap
Print Heap A: 16, 14, 10, 8, 7, 9, 3, 2, 4, 1
After inserting the number 20 into heap A: 20, 16, 10, 8, 14, 9, 3, 2, 4, 1, 7
After inserting the number 17 into heap A: 20, 16, 17, 8, 14, 10, 3, 2, 4, 1, 7, 9
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started