Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the pseudo-code for class, we used arrays with indices starting at 1 to repre

ID: 643400 • Letter: I

Question

In the pseudo-code for class, we used arrays with indices starting at 1 to represent our heap. However, in many programming languages, arrays start at 0. The book ignores the difference by skipping the first space and beginning at 1 anyway. Give equations for Parent(i), LeftChild(i), and RightChild(i) if we were to actually use this space.

Explanation / Answer

#include using namespace std; int heap[100]; int size; void max_heap(int parent) { int leftchild=(2*parent)+1; int rightchild=(2*parent)+2; int max; if(heap[leftchild]>heap[parent] and leftchildheap[max]) max=rightchild; if(max!=parent) { swap(heap[parent],heap[max]); max_heap(max); } } int main() { int x; coutsize; for(int i=0;i>heap[i]; } int lastparent=(size/2)-1; for(int i=lastparent;i>=0;i--) { max_heap(i); } cout
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote