Question

Assume that a heap is implemented using an array called items where the root of the heap is stored in items[0]. Provide the m

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

In a heap using array, the node can be located via simple formula or arithmetic using the node's index. Below I will derive the equivalent equations for heaps with their root at index 0, also additional nodes on heaps with their root at index 1. I'll define the level of a node as its distance from the root, such that the root itself occupies level 0.

CHILD NODES :-

Let node 'i' be located at the level L, and keep in mind that any level contains exactly   nodes. There are   nodes contained in the layers since, the root is stored at 0 and kth node will be stored at index (k-1). These Observations concludes that following expression for the index of the last node in layer l.

Assume there be j nodes after node i in layer L, such that

Each of these j nodes must have exactly 2 children, so there must be two j nodes which separates i's right child from the end of its layer L+1.

Note that left child of any node is 1 place before its right child, we get .

If the root is located at index 1 instead of 0, the last node in each level is instead at index . Using this throughout yields and for heaps with their root at 1.

PARENT NODES:-

Every node is either the left or right child of its parent, Therefore:-

Hence,

consider the expression .

If node 'i' is a left child, this gives the result , Also, it also gives the correct result if node 'i' is a right child. In this case, should be even, and hence should be odd.

Therefore, its parent can be found using :

.

Add a comment
Know the answer?
Add Answer to:
Assume that a heap is implemented using an array called items where the root of the...
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
  • 3. We begin by considering a 3-heap (a heap where each node has at most 3...

    3. We begin by considering a 3-heap (a heap where each node has at most 3 children) stored as an array. What are the indices of the parent and nth child of index k? That is, fill in the following mathematical functions. 20 pt parent(k)= child-o(k) = child-1(k)= child-2(k)= 4. Update the implementation for a minHeap delete Min given a 3-heap, include your Java code. 20 pts

  • 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...

  • 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...

  • 5. A three-heap with n elements can be stored in an array A, where A[O] contains...

    5. A three-heap with n elements can be stored in an array A, where A[O] contains the root of the tree. a) Draw the three-heap that results from inserting 5, 2, 8, 3, 6, 4, 9, 7, 1 in that order into an initially empty three-heap. You do not need to show the array representation of the heap. You are only required to show the final tree, although if you draw intermediate trees. b) Assuming that elements are placed in...

  • Originally we stored our heap in an array. Consider instead storing our heap as a doubly...

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

  • Discrete Mathematics Time Complexity Analysis Due: May 9th, 2019 Math 4 6026 Heap Sort Another algorithm for sorting uses a specialized tree structure called a "heap." Specifically, we wi...

    Discrete Mathematics Time Complexity Analysis Due: May 9th, 2019 Math 4 6026 Heap Sort Another algorithm for sorting uses a specialized tree structure called a "heap." Specifically, we will use a binary heap, which is like a binary tree with hierarchy. Here is an example of a binary heap structure 1. 2. There is a top vertex, called the parent vertex (aka node). The top parent vertex connects to two vertices a level below. These vertices are the "left child"...

  • #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...

  • Assume that we start with a random array of size n= 2k-1 and form a heap....

    Assume that we start with a random array of size n= 2k-1 and form a heap. a) What is the probability that the third largest element will be a child of the root? Justify b) Before you form a heap, you notice that none of the three smallest elements are near the top of the array, or more formally none of them are in any of the first (n-3)/4 locations of the array. What is the probability that the third...

  • 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...

  • Min heap class implementation in Python. Implement a min-using an array. Your min-heap class will have...

    Min heap class implementation in Python. Implement a min-using an array. Your min-heap class will have one private attribute, an array of integers. Implement the following methods for the min-heap class You may not use the built-in min function. init - Constructor makes an empty heap str - Prints the heap out in any way you want for debugging only) makenull(self) - Makes the heap empty insert(self,x) - Insert element x into the heap parent(self,i) - Returns the index of...

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