Question

In: Computer Science

I need step by step instructions of inserting these into a heap. this, is, the, house,...

I need step by step instructions of inserting these into a heap. this, is, the, house, that, jack, built. They should be inserted in this sequence. THen swap the first and last variables and do it again. I know the answer is suppose to be but not sure how I'm suppose to get that answer.   

2nd iteration would be.   built, is, the house, that jack, this.

I know the ansers in array format would be just keep getting it wrong on my own:

1. built | is | house | this | that | the | jack
2. built | house | jack | is | that | the | this

Solutions

Expert Solution

#include <bits/stdc++.h>
using namespace std;
// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
void heapify(string arr[], int n, int i)
{
        int largest = i; // Initialize largest as root
        int l = 2 * i + 1; // left = 2*i + 1
        int r = 2 * i + 2; // right = 2*i + 2

        // If left child is larger than root
        if (l < n && arr[l] > arr[largest])
                largest = l;

        // If right child is larger than largest so far
        if (r < n && arr[r] > arr[largest])
                largest = r;

        // If largest is not root
        if (largest != i)
        {
                swap(arr[i], arr[largest]);

                // Recursively heapify the affected sub-tree
                heapify(arr, n, largest);
        }
}

// main function to do heap sort
/* A utility function to print array of size n */
void printArray(string arr[], int n)
{
        for (int i = 0; i < n; ++i)
                cout << arr[i] << " ";
        cout << "\n";
}
void heapSort(string arr[], int n)
{
        // Build heap (rearrange array)
        for (int i = n / 2 - 1; i >= 0; i--)
                heapify(arr, n, i);

        // One by one extract an element from heap
        for (int i = n - 1; i > 0; i--)
        {
                printArray(arr, n);

                // Move current root to end
                swap(arr[0], arr[i]);

                // call max heapify on the reduced heap
                heapify(arr, i, 0);
        }
}


// Driver program
int main()
{
        string arr[] = { "this", "is", "the", "house", "that", "jack", "built"};
        int n = sizeof(arr) / sizeof(arr[0]);

        heapSort(arr, n);

        printArray(arr, n);
}

OUTPUT:


Related Solutions

I need step by step instructions on where to find the information to answer this question:...
I need step by step instructions on where to find the information to answer this question: Your task involves an analysis of general economic conditions or systematic risk, i.e., the risk that affects all industries and companies, in the U.S. macroeconomy. Your goal is to determine in percentage terms an optimal allocation of $1,000,000 among the following three asset classes: U.S. equities, U.S. Treasury bonds, and cash. The goal is to maximize your expected return over the next 12 months....
I need step by step instructions on using Excel to do this assignment and the values...
I need step by step instructions on using Excel to do this assignment and the values I need to enter to compute what is being asked of me. I don't know how to use Excel and you tube is no help right now because I can't find specifics of what I need done. I'll post part two of this assignment as a seperate question. I need to get through part one first. Thanks... 1. Exercise 11   Section 3-1 11. Weight...
I know the answers I just need detailed step-by-step instructions. Brad Company developed the following budgeted...
I know the answers I just need detailed step-by-step instructions. Brad Company developed the following budgeted life-cycle income statement for two proposed products. Each product's life cycle is expected to be two years. Product A Product B Total Sales $200,000 $200,000 $400,000 Cost of goods sold 120,000 130,000 250,000 Gross profit $ 80,000 $ 70,000 $150,000 Period expenses:      Research and development (70,000)      Marketing (50,000) Life-cycle income $ 30,000 A 10 percent return on sales is required for new products. Because...
Need step by step solution how to build a simple 3D house of any area or...
Need step by step solution how to build a simple 3D house of any area or lenth etc in autocad?
Step by step instructions for a neck assessment
Step by step instructions for a neck assessment
I need name of the final compound, and step by step equations of the reaction for...
I need name of the final compound, and step by step equations of the reaction for the experiment below. Ethylenediamine complexes of Cobalt Procedure 1. Dissolved 1.5 g CoCl2 in 5mL water 2. While the CoCl2 6H2O is dissolving, measure out 10mL of an ethylenediamine solution. (The solution is prepared by dissolving 130mL of anhydrous ehylenediamine in one liter of water) 3.Cool the ethylenediamine solution in ice. 4.Slowly add 2.1mL 3M HCl 5.Add the CoCl2 solution to the partially neutrolized...
I need a step by step for entering in excel. For this assignment, your group will...
I need a step by step for entering in excel. For this assignment, your group will utilize the preliminary data collected in the Topic 2 assignment. Considering the specific requirements of your scenario, complete the following steps using Excel. The accuracy of formulas and calculations will be assessed. Select the appropriate discrete probability distribution. If using a binomial distribution, use the constant probability from the collected data and assume a fixed number of events of 20. If using a Poisson...
Note: I need a step by step answer to the question with an explanation of your...
Note: I need a step by step answer to the question with an explanation of your answer so that I can understand how to reproduce the steps. Marsha Inc. has the following budgeted data for 2016: Cash balance, beginning $15,000 Collections from customers 145,000 Direct materials purchases 25,000 Expenses: Operating expenses 50,000 Payroll 75,000 Income taxes 6,000 Other: Machinery purchases 30,000 Operating expenses include $20,000 depreciation for buildings and equipment. All purchases of materials are paid for in the period...
Note: I need a step by step answer to the question with an explanation of your...
Note: I need a step by step answer to the question with an explanation of your answers: White Corporation’s budget calls for the following sales for next year: Quarter 1 90,000 Quarter 3 68,000 Quarter 2 76,000 Quarter 4 96,000 Each unit of the product requires 3 pounds of direct materials. The company’s policy is to begin each quarter with an inventory of product equal to 5% of that quarter’s estimated sales requirements and an inventory of direct materials equal...
I DID STEP 1-2 AND 3 I JUST NEED A STEP 4 ANSWERS AND THIS IS...
I DID STEP 1-2 AND 3 I JUST NEED A STEP 4 ANSWERS AND THIS IS SO EMERGENCY(DUE DATE TONIGHT) PLEASE HELP (I WILL GIVE UPVOTE AND GOOD COMMENT THANK YOU) if you need to see step 1-2-3 I can add on the comment. Step 1 - WhoIs Step 2 - TCP Port Scanning Step 3 - UDP Port Scanning Step 4 - Analysis ---- Answer these questions: How can this type of scanning be used to help a network...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT