Question

In: Computer Science

True False The enqueue and dequeue operations in a priority queue take O(lg n) time, while...

True False

The enqueue and dequeue operations in a priority queue take O(lg n) time, while linked list and array implementations take O(1) time.

A binary heap is a (nearly) complete binary tree.

Heaps usually use a linked node structure for implementation.

When implementing heaps as arrays, you can leave a blank space at the front. If you do, the parent of a node at index i can be found at index floor(i/2).

When implementing heaps as arrays, if you don't leave a space at the front, you can find the children of the node at index i at 2i and 2i+1.

The heap order property states that each node must contain a value greater than its left child and less than its right child.

Insertion in a heap is done by adding at the end of the array and "percolating up".

The average case for insertion in a heap is O(lg n). The worst case is O(n).

Deletion in a heap requires removing the root item and shifting all other items over one space.

Building a heap by adding values one at a time is an O(n) operation. Building a heap "all at once" with the heapify() method (called buildHeap() in the textbook) is O(n lg n).

Solutions

Expert Solution

1)The enqueue and dequeue operations in a priority queue take O(lg n) time, while linked list and array implementations take O(1) time.

It is true that enqueue and dequeue operation on a priority queue is implemented in O(log n) time.In linked list implementation,it is arranged in order and hence dequeue operation is performed in O(1) time while in equeue opration we will have to traverse the list,so it takes O(n) time.

Therefore,This statement is overall False.

2) A binary heap is a (nearly) complete binary tree.

Def: - A nearly complete binary tree of height h is a binary tree of height h in which

a) There are 2d nodes at depth d for d = 1,2,...,h−1,

b) The nodes at depth h are as far left as possible.

This defintion holds good for max-heap or simply binary heap,

Therefore, this statement is True.

3) Heaps usually use a linked node structure for implementation.

Heaps are generally implemented as arrays.

Therefore, this statement is false.

4) When implementing heaps as arrays, you can leave a blank space at the front. If you do, the parent of a node at  index i can be found at index floor(i/2).

This statement is true.

5) When implementing heaps as arrays, if you don't leave a space at the front, you can find the children of the node at index i at 2i and 2i+1.

This statement is False.

6) The heap order property states that each node must contain a value greater than its left child and less than its right child.

This statement is false.

Because, for being a heap,root must be greater or smaller than both the childs.

7) Insertion in a heap is done by adding at the end of the array and "percolating up".

this is True.

Because,we add at the end and perform heapify process in which values percolates upwards if it is not its correct position.

8). The average case for insertion in a heap is O(lg n). The worst case is O(n).

It is False.

Because,in average case insertion in heap takes O(1) time. And in worst case it takes O(logn) time.

9)  Deletion in a heap requires removing the root item and shifting all other items over one space.

This is false.

Because, All items may not get shifted by one position.

10)  Building a heap by adding values one at a time is an O(n) operation. Building a heap "all at once" with the heapify() method (called buildHeap() in the textbook) is O(n lg n).

This is false.

Because adding a value takes O(logn) time(Due to heapify process).while there are n values and each value takes logn time so overall it will take O(nlogn) time.

  


Related Solutions

Discuss the relative efficiency of the enqueue and dequeue operations for an array-based queue implemented with...
Discuss the relative efficiency of the enqueue and dequeue operations for an array-based queue implemented with a fixed-front approach as opposed to a floating-front approach.
Using Python list tools, create the standard stack (push, pop) and queue (enqueue, dequeue) operations Counting...
Using Python list tools, create the standard stack (push, pop) and queue (enqueue, dequeue) operations Counting Letter Challenge: Create a function that... Takes in a string as parameter Counts how often each letter appears in the string Returns a dictionary with the counts BONUS: make it so lowercase and uppercase letter count for the same letter
C++ Given Stack Code, Implements Queue. enqueue, dequeue. Modify to function like Queue. Stack #ifndef STACK_H...
C++ Given Stack Code, Implements Queue. enqueue, dequeue. Modify to function like Queue. Stack #ifndef STACK_H #define STACK_H #include "Node.h" template class Stack { private: Node* top; public: // Constructor Stack() { top = nullptr; } void push(Object ab) { if (top != nullptr) { Node* newNode = new Node(ab, *top); top = newNode; } else { Node* newNode = new Node(ab); top = newNode; } } Object pop() { if (top != nullptr) { Node *returnVal = top; top...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method * Complete the peek() method * No other methods/variables should be added/modified */ public class A3Queue {    /*    * Grading:    * Correctly adds an item to the queue - 1pt    */    public void enqueue(E val) {        /*        * Add a node to the list        */    }    /*    * Grading:   ...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method * Complete the peek() method * No other methods/variables should be added/modified */ public class A3Queue {    /*    * Grading:    * Correctly adds an item to the queue - 1pt    */    public void enqueue(E val) {        /*        * Add a node to the list        */    }    /*    * Grading:   ...
In Java: Initiate empty queue of strings and recreate .isempty, .size, .dequeue, .enqueue methods. //You may...
In Java: Initiate empty queue of strings and recreate .isempty, .size, .dequeue, .enqueue methods. //You may not use the original methods of the stack api to answer. You may not add any more fields to the class. import java.util.NoSuchElementException; import edu.princeton.cs.algs4.Stack; public class StringQueue {    //You may NOT add any more fields to this class.    private Stack stack1;    private Stack stack2;    /**    * Initialize an empty queue.    */    public StringQueue() { //TODO   ...
Suppose you start with an empty queue and perform the following operations: enqueue 1, enqueue 2,...
Suppose you start with an empty queue and perform the following operations: enqueue 1, enqueue 2, dequeue, enqueue 3, enqueue 4, dequeue, enqueue 5. What are the resultant contents of the queue, from front to back? Group of answer choices 1, 2, 3, 4, 5 1, 3, 5 1, 2, 3 3, 4, 5 Assume you are using the text's array-based queue and have just instantiated a queue of capacity 10. You enqueue 5 elements, dequeue 4 elements, and then...
Task 3: Implement a queue through circular linked list. Develop enqueue, dequeue, status, isempty and isfull...
Task 3: Implement a queue through circular linked list. Develop enqueue, dequeue, status, isempty and isfull functions. Test your code in main function with 10 elements Note: for each task, you are supposed to create three files as specified in task1, i.e., implementation file, interface file and file containing point of entry. in C++
Give an O(lg n)-time EREW algorithm that determines for each object in an n-object list whether...
Give an O(lg n)-time EREW algorithm that determines for each object in an n-object list whether it is the middle (n/2 th) object.
Give an O(lg n)-time EREW algorithm to perform the prefix computation on an array x[1. ....
Give an O(lg n)-time EREW algorithm to perform the prefix computation on an array x[1. . n]. Do not use pointers, but perform index computations directly.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT