Question

2) Questions about standard ADTs and their implementations that we have studied, including asymptotic analysis of their oper
0 0
Add a comment Improve this question Transcribed image text
Answer #1

solution:

DougPriorityQueue can be implemented using max-heap with time as its key.

// find the following c code for implementing abstract max-heap and corresponding algorithm

// Note: in array representation of our tree. left child of ith node is at index 2 *i and right child will be at

2*i+1 index.

#include<stdio.h>
#include<stdlib.h>   // for calloc() function.

typedef struct Heap_node
{
    void* data;               // this data can store any type of data as void pointer is universal which can point to any data type.
    float key;
}heap_node;

typedef struct Heap{
   heap_node* arr;
   int n;
}heap;

void initializeMinHeap(heap* h,int size)
{
   h->arr = (heap_node*) calloc (sizeof(heap_node*),size);
   h->n = -1;                   // -1 representing empty heap.
}

void maxHeapifyUp(heap* h,int i)                    //making the ith index find its place to satisfy max-heap property
{
    if(i==0)                                        // base case when the element is root itself.
        return;
    else if(h->arr[i].key > h->arr[(i-1)/2].key)    // if not satisfying max-heap property, then swap with its parent.
    {
        heap_node temp = h->arr[i];
        h->arr[i] = h->arr[(i-1)/2];
        h->arr[(i-1)/2] = temp;
        maxHeapifyUp(h,(i-1)/2);                    // after swaping call recursively at the parent.
    }
}

void maxHeapifyDown(heap* h,int i)
{
    int largeIndex = i;
    if((2*i)>=h->n)                                 // base case when i is the leaf node.
        return;
    else
    {

        if(h->arr[2*i+1].key > h->arr[largeIndex].key) // finding the larger one among the children and parent itself
            largeIndex = 2*i+1;
        if(h->arr[2*i+2].key > h->arr[largeIndex].key)
            largeIndex = 2*i+2;
        if(largeIndex == i)                            // if the ith node is the largest then simply return.
            return;
        else{
            heap_node temp = h->arr[i];                 // else swap with the largest.
            h->arr[i] = h->arr[largeIndex];
            h->arr[largeIndex] = temp;
            maxHeapifyDown(h,largeIndex);               // call recursively at the swapped position.
        }

    }
}

void* extractMax(heap *h)
{
    void* temp = h->arr[0].data;                      
    if(h->n==-1)                                        // base case when heap is empty.
        return NULL;
    else{
        h->arr[0] = h->arr[h->n];                       // replace root with last element in heap array i.e some leaf node.
        --(h->n);
        maxHeapifyDown(h,0);                            // recursively heapify down to find its spot and retain the heap property.
    }
    return temp;                                        // return the value of the extracted one.
}


/* extractMax() is similar to dequeue() in max-heap as we need element with maximum key.
   This can be done in O(log n), where n is no.of elements in heap as we need to swap with log n levels
   after replacing the root with leaf node.*/

void maxHeapInsert(heap *h,void* data,float key)
{
    ++(h->n);                                           // updating heap size.
    h->arr[h->n].data = data;                           // inserting at the last.
    h->arr[h->n].key = key;
    maxHeapifyUp(h,h->n);                               // calling heapifyup to find its spot.
}


/* maxHeapInsert() will have time complexity of O(log n), as for searching for the right spot needs checking log n levels.

// Please refer above image for indentation.

Tree will be as follows after inserting the 4 elements in our DougPriorityQueue.

                                       Tom, time = 22
                                   /                 \
                           Jim, time = 17.2       Bill, time = 11.1
                               /
                           Bob, time = 2.31


please give me thumb up

Add a comment
Know the answer?
Add Answer to:
2) Questions about standard ADT's and their implementations that we have studied, including asymptotic analysis of...
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
  • You have to present three thoughtful responses to what other students wrote to the two questions...

    You have to present three thoughtful responses to what other students wrote to the two questions below. You should not say I agree or not agree only. You have to explain your response. You have up to 100 words for each response. 1. What are the most three important things you have learned from the paper. Please explain. 2. As a CIO of a healthcare provider (e.g., Henry Ford Health System, Bauman), how would you use the digital technologies. Please...

  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

  • I need help with my very last assignment of this term PLEASE!!, and here are the instructions: After reading Chapter T...

    I need help with my very last assignment of this term PLEASE!!, and here are the instructions: After reading Chapter Two, “Keys to Successful IT Governance,” from Roger Kroft and Guy Scalzi’s book entitled, IT Governance in Hospitals and Health Systems, please refer to the following assignment instructions below. This chapter consists of interviews with executives identifying mistakes that are made when governing healthcare information technology (IT). The chapter is broken down into subheadings listing areas of importance to understand...

  • Risk management in Information Security today Everyday information security professionals are bombarded with marketing messages around...

    Risk management in Information Security today Everyday information security professionals are bombarded with marketing messages around risk and threat management, fostering an environment in which objectives seem clear: manage risk, manage threat, stop attacks, identify attackers. These objectives aren't wrong, but they are fundamentally misleading.In this session we'll examine the state of the information security industry in order to understand how the current climate fails to address the true needs of the business. We'll use those lessons as a foundation...

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