Question

c++, hello, can someone please explain the following questions preferably in code with an explanation of how & why it happened? the professor said this was going to be on a final but i am very con...

c++,

hello, can someone please explain the following questions preferably in code with an explanation of how & why it happened? the professor said this was going to be on a final but i am very confused on this.

a) transfer an array into a binary tree?

b) how to make it into a heap?

c) how to make it sorted?

d) and how to work with its children and parents?

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <cstdlib>
using namespace std ;

// here the array elements are passed as reference so the actual value inside the passed array will be modified
void swap(int *a, int *b)
{
    int temp = *a; // copy value at address a and place it to temp
    *a = *b; // content of what pointed by  pointer b will be updated inside the variable pointed by pointer b
    *b = temp;
}
void display(int arr[], int n)
{
    for (int i=0; i<n; ++i)
        cout << arr[i] << " ";
    cout << "\n";
}


void MinHeapify(int *arr, int index, int size)
{
    // We need to find the minimum of ( parent , left child , right child )


    // if parent is at index i then left child will be at array index 2*index +1
    int left = 2*index + 1;
    int right = 2*index + 2; // for parent at index i its parent will be at index 2* index + 2
    int smallest = index; // initialize index as the smallest of ( parent , left child and the right child )

    if (left < size  /* we should remain inside the array scope */  && arr[left] < arr[index])
        smallest = left;
    if (right < size && arr[right] <arr[smallest])
        smallest = right;

    // now you found the smallest between( parent , left child , and right child )

    // if smallest is parent then for min heap there is no problem but

    // but if parent is greater than any of the child we need to swap the ( parent( index i ) and the smallest )
    if (smallest != index)
    {
        swap(&arr[index], &arr[smallest]);
        MinHeapify(arr, index, smallest);// since the index element reached to the new position now we need to check
        // wether the element at the new position satisfy the property of min heap so again call minHeapify() on the
        // swapped element
    }
}


void heapSort(int *arr, int size)
{

    // we will take the minimum( root ) and place it at the last array[last _ index]  and take the second last
    // element and make it root
    for (int i=size-1 ; i>=0; i--)
    {
        // swap current root to end
        swap(arr[0], arr[i]);

        // since the root element has  been modified  in order to ensure that the tree satisfy  minHeap property we
        // apply the minHeapify() on the reduced size heap( size is reduced by -1) so

        // minHeapify() will not check the prevoius root( minimum ) at each pass 
        MinHeapify(arr, 0 , i   );//
    }
}



void buildMinHeap(int *arr, int size)
{
    /* this method will start its action from the first non leaf that is
     * since lowest level( leaf)  contain size/2 +  1  ( index    size to size/2 -1 ) nodes so
     * skip all leaves and start from the first non leaf node
     * which is at the index size/2 - 1 and iterate till you reach the root ( index 0 ) */


    int index;
    for(index = size/2 -1; index >= 0; index-- ) // for all non leaf nodes
        MinHeapify(arr, index, size);
}



int main()
{
    int *arr, size, index;
    printf("Enter size of heap\n");
    scanf("%d", &size);

    //allocate memory
    arr = (int *) malloc(sizeof(int) * size);

    printf("Enter elements in heap\n");
    // Here we are adding the elements in array and this array will be converted to min heap
    for(index = 0; index < size; index++)
        scanf("%d", &arr[index]);


cout<< "display the initial array  : ";
    display(arr , size );

    buildMinHeap(arr, size);
    cout << "The min heap array is :";
    display(arr , size);


    // for sorting we can apply heap sort procedure which will take the min heaped( array) as input and produce the
    // sorted array
    heapSort(arr , size );
    cout<< "The sorted array using heap-sort  :" << endl ;

    display(arr , size );





}

Enter size of heap Enter elements in heap 23 23 54 54 34 34 75 75 31 31 display the initial array23 54 66 22 34 75 31 The min

Add a comment
Know the answer?
Add Answer to:
c++, hello, can someone please explain the following questions preferably in code with an explanation of how & why it happened? the professor said this was going to be on a final but i am very con...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • can someone please explain how to do this? I am very confused and it is due...

    can someone please explain how to do this? I am very confused and it is due tomorrow. Directions: Write the null and alternate hypothesis, determine the appropriate t-test for cach of the following problems, decide upon a decision rule, draw the rejection region, and write a statement that describes the relationship the researcher is entitled to make. (You may use Excel to calculate means, variance and standard deviation). 11 An investigator believes that families in Virginia have more children on...

  • Can someone please explain how to do this I am very confused and it is due...

    Can someone please explain how to do this I am very confused and it is due tomorrow Directions: Write the null and alterate hypothesis, determine the appropriate t-test for each of the following problems, decide upon a decision rule, draw the rejection region, and write a statement that describes the relationship the researcher is entitled to make. (You may use Excel to calculate means, variance and standard deviation) 1. An investigator predicts that individuals that fit the Type A Behavior...

  • hello there, i have to implement this on java processing. can someone please help me regarding...

    hello there, i have to implement this on java processing. can someone please help me regarding that? thanks War is the name of a popular children’s card game. There are many variants. After playing War with a friend for over an hour, they argue that this game must never end . However! You are convinced that it will end. As a budding computer scientist, you decide to build a simulator to find out for sure! You will implement the logic...

  • Read “Instituionalizing our Demise: America vs Multiculturalism” by Roger Kimball on pg 268 and “Reinventing America”...

    Read “Instituionalizing our Demise: America vs Multiculturalism” by Roger Kimball on pg 268 and “Reinventing America” Call for a new national indentity” by Elizabeth Martinez on pg 275. Create a double entry notebook for each reading selection It should be atleast five observation and responses. wric 268 PART 2 essay pro. exactly how and why their authors disagree. Instead of with parties in conflict as mediators do, you will nt of view designed to appeal to both sides, mediatn posing...

  • 10. The Beck & Watson article is a Group of answer choices quantitative study qualitative study...

    10. The Beck & Watson article is a Group of answer choices quantitative study qualitative study 11. Beck & Watson examined participants' experiences and perceptions using what type of research design? Group of answer choices particpant obersvation phenomenology 12. Select the participants in the Beck & Watson study Group of answer choices Caucasian women with 2-4 children Caucasian pregnant women 13. In the Beck & Watson study, data was collected via a(n) Group of answer choices internet study focus group...

  • 14. Select the number of participants in the Beck & Watson study Group of answer choices...

    14. Select the number of participants in the Beck & Watson study Group of answer choices 8 13 22 35 15. Beck & Watson determined their final sample size via Group of answer choices coding saturation triangulation ethnography 16.Through their study, Beck & Watson determined Group of answer choices after a traumatic birth, subsequent births have no troubling effects after a traumatic birth, subsequent births brought fear, terror, anxiety, and dread Subsequent Childbirth After a Previous Traumatic Birth Beck, Cheryl...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT