Question

1. Heapsort (4 points) Originally we stored our heap in an array. Consider instead storing our heap as a doubly linked list. (1) (2 points) For a node i what are the new asymptotic run times for left(i), right(i), and parent(i)? (2) (2 points) How does this affect the run times of insert(key), extractMax), findMax()?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1)

                If heap is in an array, then to find left child of any index will be (root node index)*2

                To find right child of any index will be (root node index)*2 + 1

                To find parent node of any index will be (index)/2

                For all these, it will take constant time with some computation.

                But in linked list every node is pointing to it’s left side node address and right side node address and parent address.

                So to find left child and right child and parent node for any node is

                                Left child = Given node->next

                                Right child = Given node->prev

                                Parent = given node->parent

                It is also constant time but no computation like in array.

2)

                In array, Insertion will take O(log n). because we can insert the key in right position by binary search. Where in linked list, we search the key in sequential order, so it will take O(n) time

                In array, find max will take constant time (i.e O(1)). Because the first index it self is a root node, so no need to search. Where in linked list, find max will also take constant time, because the head node will point to the root node which is max in the heap.

                In array, extract max will take O(log n), after deleting max node, we need replace that position with max number in the array, so we need to search, we follow binary search which will take O(log n). where in linked list, to find max also we need search, searching is sequential. So it will take O(n).

Add a comment
Know the answer?
Add Answer to:
Originally we stored our heap in an array. Consider instead storing our heap as a doubly...
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
  • 1. Which of the following is a proper array representation a binary min heap?

    1. Which of the following is a proper array representation a binary min heap?2. A heap is implemented using an array. At what index will the right child of node at index i be found? Note, the Oth position of the array is not used.Select one:a. i/2b. 2 i+1c. i-1d. 2 i3. Consider the following array of length 6. Elements from the array are added, in the given order, to a max heap. The heap is initially empty and stored as an array.A={18,5,37,44,27,53}What...

  • Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each...

    Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each node has a parent field as well as the usual left child, right child, and key fields. -Condition : Do not use Array representation. Use the following structure. typedef struct node *treePointer; typedef struct node { int key; treePointer parent; treePointer leftChild, rightChild; }; - INPUT i k : Insert the node with the key value of k in...

  • Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each...

    Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each node has a parent field as well as the usual left child, right child, and key fields. -Condition : Do not use Array representation. Use the following structure. typedef struct node *treePointer; typedef struct node { int key; treePointer parent; treePointer leftChild, rightChild; };    - INPUT i k : Insert the node with the key value of k in...

  • Consider the following complete binary tree is stored in an array the way we learned during...

    Consider the following complete binary tree is stored in an array the way we learned during heap lecture. The root node is stored at index 1. The last node (47) is stored at index heapsize. If you want to build a heap from the array using the heapify(), what position of the array you start doing percolate Down in heapify()? Answer: What is the run-time to build a heap from an array of size n using heapify() process ? OC...

  • Assume that a heap is implemented using an array called items where the root of the...

    Assume that a heap is implemented using an array called items where the root of the heap is stored in items[0]. Provide the mathematical proof of the following formulas that calculate the indices of the children and parent of any node items[i]. Left child of items[i] is items [2xi + 1] - Right child of items[i] is items [2xi + 2] Parent of items[i] is items[[(i-1)/2]]

  • Using C++, data structures, C++ STL, inputs and expected outputs are shown below. Max Heap Heap...

    Using C++, data structures, C++ STL, inputs and expected outputs are shown below. Max Heap Heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either > (in a max heap) or s (in a min heap) the key of C. The node at the "top" of the heap (with no parents) is called the root node. In binary-tree based heap, it...

  • #ifndef HEAP_H_ #define HEAP_H_ namespace cse { template<typename dtype> class heap { public: /** TO DO::...

    #ifndef HEAP_H_ #define HEAP_H_ namespace cse { template<typename dtype> class heap { public: /** TO DO:: default constructor perform new to reserve memory of size INITIAL_CAPACITY. set num_items = 0; */ heap() { // write your code here }; /** Create a heap from a given array */ heap(dtype *other, size_t len) { // write your code here }; /** copy constructor copy another heap to this heap. */ heap(const heap<dtype> &other) { //write your code here }; /** Accessor...

  • Complete HeapPriorityQueue (7 points). In lecture we implemented HeapPriorityQueue using an array-based representation of a heap...

    Complete HeapPriorityQueue (7 points). In lecture we implemented HeapPriorityQueue using an array-based representation of a heap (a complete binary tree whose entries satisfy the heap-order property). For this problem, complete the included HeapPriorityQueue class by using the LinkedBinaryTree class to represent a heap. Hint: the most challenging part of this problem is identifying the last Position in the heap and the next available Position in the heap. It is suggested that you review the array-based heap to better understand how...

  • In class, we discussed the priority queue (PQ) ADT implemented using min-heap. In a min-heap, the...

    In class, we discussed the priority queue (PQ) ADT implemented using min-heap. In a min-heap, the element of the heap with the smallest key is the root of the binary tree. On the other hand, a max-heap has as root the element with the biggest key, and the relationship between the keys of a node and its parent is reversed of that of a min-heap. We also discussed an array-based implementation of heaps. In this assignment, your task is to...

  • must be coded in c++ without any STL libraries sadly :( so im struggling on this...

    must be coded in c++ without any STL libraries sadly :( so im struggling on this problem, any help would be greatly appreciated, thanks in advance! :) assignment is due tomorrow and im really struggling on this last question :( a. Begin by implementing a BST for integers. The underlying structure is a linked list. You need these methods: i. BST(); -- Constructor ii. void put (int) – Inserts a value into the BST. iii. Void put(int[] a) – Inserts...

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