Write a Binary Min heap in Java that has a dictionary or map structure to store the key (which would be the item) and the value is the index where the key is stored in the binary heap's array. Some of the requirements for the heap is: An internal array to store the elements contained in the heap. An integer attribute to store the size of the heap. The size is equal to the number of elements in the heap, not the size of the array. A dictionary or map structure to serve as a lookup table so the index of any given element can be found in O(1) time (on average). In the text this attribute is named, Position. For each entry in the dictionary, the key would be the item, and the value would be the index where the item is stored. The dictionary will be used to implement the ChangePriority() method. A HeapNode component or class that represents an individual node of the heap. This component is essential in order to be able to store both an item’s data value and its priority value together in the heap. Do not assume an item’s data value and its priority value are one and the same  As for the methods that need to be created for the heap:  Heapify_Up(index) – moves an element located at the specified index upwards in the heap to correctly reposition an element whose value is less than the value of its parent. This condition may result from removing an element or from changing an element’s value. This method is described on pages 60-61 of the text, and pseudocode is provided on page 61. Heapify_Down(index) – moves an element located at the specified index downwards in the heap to correctly reposition an element whose value is greater than the value of either of its children. This condition may result from removing an element or from changing an element’s value. This method is described on pages 62-63 of the text, and pseudocode is provided on page 63. StartHeap(N) – initializes an empty heap that is set up to store at most N elements. This operation takes O(N) time, as it involves initializing the array that will hold the heap. Insert(item, value) – inserts the item, item, with an ordering value, value, into the heap at the end of the array, then uses Heapify_Up to position the item so as to maintain the heap order. If the heap currently has n elements, this takes O(log n) time. FindMin() – identifies the minimum element in the heap, which is located at index 1, but does not remove it. This takes O(1) time. Delete(index) – deletes the element in the specified heap position by moving the item in the last array position to index, then using Heapify_Down to reposition that item. This is implemented in O(log n) time for heaps that have n elements. ExtractMin() – identifies and deletes the element with the minimum key value, located at index 1, from the heap. This is a combination of the preceding two operations, and so it takes O(log n) time. Delete(item) – deletes the element item from the heap. This can be implemented as a call to Delete(Position[item]), which operates in O(logn) time for heaps that have N elements provided Position allows the index of v to be returned in O(1) time ChangePriority(item, newPriority), which changes the priority value of element v to newPriority. To implement this operation in O(logn)time, we first need to be able to identify the position of element v in the array, which we do by using the position dictionary(look-up table). Once we have identified the position of element v, we change the key and then apply Heapify-up or Heapify-down as appropriate.

icon
Related questions
Question

Write a Binary Min heap in Java that has a dictionary or map structure to store the key (which would be the item) and the value is the index where the key is stored in the binary heap's array. Some of the requirements for the heap is:

  1. An internal array to store the elements contained in the heap.
  2. An integer attribute to store the size of the heap. The size is equal to the number of elements in the heap, not the size of the array.
  3. A dictionary or map structure to serve as a lookup table so the index of any given element can be found in O(1) time (on average). In the text this attribute is named, Position. For each entry in the dictionary, the key would be the item, and the value would be the index where the item is stored. The dictionary will be used to implement the ChangePriority() method.
  4. HeapNode component or class that represents an individual node of the heap. This component is essential in order to be able to store both an item’s data value and its priority value together in the heap. Do not assume an item’s data value and its priority value are one and the same
  5.  As for the methods that need to be created for the heap: 
  1. Heapify_Up(index) – moves an element located at the specified index upwards in the heap to correctly reposition an element whose value is less than the value of its parent. This condition may result from removing an element or from changing an element’s value. This method is described on pages 60-61 of the text, and pseudocode is provided on page 61.
  2. Heapify_Down(index) – moves an element located at the specified index downwards in the heap to correctly reposition an element whose value is greater than the value of either of its children. This condition may result from removing an element or from changing an element’s value. This method is described on pages 62-63 of the text, and pseudocode is provided on page 63.
  3. StartHeap(N) – initializes an empty heap that is set up to store at most N elements. This operation takes O(N) time, as it involves initializing the array that will hold the heap.
  4. Insert(item, value) – inserts the item, item, with an ordering value, value, into the heap at the end of the array, then uses Heapify_Up to position the item so as to maintain the heap order. If the heap currently has n elements, this takes O(log n) time.
  5. FindMin() – identifies the minimum element in the heap, which is located at index 1, but does not remove it. This takes O(1) time.
  6. Delete(index) – deletes the element in the specified heap position by moving the item in the last array position to index, then using Heapify_Down to reposition that item. This is implemented in O(log n) time for heaps that have n elements.
  7. ExtractMin() – identifies and deletes the element with the minimum key value, located at index 1, from the heap. This is a combination of the preceding two operations, and so it takes O(log n) time.
  8. Delete(item) – deletes the element item from the heap. This can be implemented as a call to Delete(Position[item]), which operates in O(logn) time for heaps that have N elements provided Position allows the index of v to be returned in O(1) time
  9. ChangePriority(item, newPriority), which changes the priority value of element v to newPriority. To implement this operation in O(logn)time, we first need to be able to identify the position of element v in the array, which we do by using the position dictionary(look-up table). Once we have identified the position of element v, we change the key and then apply Heapify-up or Heapify-down as appropriate.
1. An internal array to store the elements contained in the heap.
2. An integer attribute to store the size of the heap. The size is equal to the number of
elements in the heap, not the size of the array.
3. A dictionary or map structure to serve as a lookup table so the index of any given element
can be found in 0(1) time (on average). In the text this attribute is named, Position. For
each entry in the dictionary, the key would be the item, and the value would
be the index where the item is stored. The dictionary will be used to implement the
Change Priority() method.
4. A Heap Node component or class that represents an individual node of the heap. This
component is essential in order to be able to store both an item's data value and its
priority value together in the heap. Do not assume an item's data value and its priority
value are one and the same.
5. Heapify_Up(index) - moves an element located at the specified index upwards in the
heap to correctly reposition an element whose value is less than the value of its parent.
This condition may result from removing an element or from changing an element's
value. This method is described on pages 60-61 of the text, and pseudocode is provided
on page 61.
6. Heapify_Down(index) - moves an element located at the specified index downwards in
the heap to correctly reposition an element whose value is greater than the value of
either of its children. This condition may result from removing an element or from
changing an element's value. This method is described on pages 62-63 of the text, and
pseudocode is provided on page 63.
7. StartHeap(N) - initializes an empty heap that is set up to store at most N elements. This
operation takes O(N) time, as it involves initializing the array that will hold the heap.
8. Insert(item, value) - inserts the item, item, with an ordering value, value, into the heap
at the end of the array, then uses Heapify_Up to position the item so as to maintain the
heap order. If the heap currently has n elements, this takes O(log n) time.
9. FindMin() - identifies the minimum element in the heap, which is located at index 1, but
does not remove it. This takes O(1) time.
10. Delete(index) – deletes the element in the specified heap position by moving the item in
the last array position to index, then using Heapify_Down to reposition that item. This is
implemented in O(log n) time for heaps that have n elements.
11. ExtractMin() - identifies and deletes the element with the minimum key value, located
at index 1, from the heap. This is a combination of the preceding two operations, and so
it takes O(log n) time.
12. Delete(item) - deletes the element item from the heap. This can be implemented as a call
to Delete(Position[item]), which operates in O(logn) time for heaps that have N elements
provided Position allows the index of v to be returned in 0(1) time
13. Change Priority (item, newPriority), which changes the priority value of element v to
newPriority. To implement this operation in O(logn)time, we first need to be able to
identify the position of element u in the array, which we do by using the position
dictionary(look-up table). Once we have identified the position of element u, we change
the key and then apply Heapify-up or Heapify-down as appropriate.
Transcribed Image Text:1. An internal array to store the elements contained in the heap. 2. An integer attribute to store the size of the heap. The size is equal to the number of elements in the heap, not the size of the array. 3. A dictionary or map structure to serve as a lookup table so the index of any given element can be found in 0(1) time (on average). In the text this attribute is named, Position. For each entry in the dictionary, the key would be the item, and the value would be the index where the item is stored. The dictionary will be used to implement the Change Priority() method. 4. A Heap Node component or class that represents an individual node of the heap. This component is essential in order to be able to store both an item's data value and its priority value together in the heap. Do not assume an item's data value and its priority value are one and the same. 5. Heapify_Up(index) - moves an element located at the specified index upwards in the heap to correctly reposition an element whose value is less than the value of its parent. This condition may result from removing an element or from changing an element's value. This method is described on pages 60-61 of the text, and pseudocode is provided on page 61. 6. Heapify_Down(index) - moves an element located at the specified index downwards in the heap to correctly reposition an element whose value is greater than the value of either of its children. This condition may result from removing an element or from changing an element's value. This method is described on pages 62-63 of the text, and pseudocode is provided on page 63. 7. StartHeap(N) - initializes an empty heap that is set up to store at most N elements. This operation takes O(N) time, as it involves initializing the array that will hold the heap. 8. Insert(item, value) - inserts the item, item, with an ordering value, value, into the heap at the end of the array, then uses Heapify_Up to position the item so as to maintain the heap order. If the heap currently has n elements, this takes O(log n) time. 9. FindMin() - identifies the minimum element in the heap, which is located at index 1, but does not remove it. This takes O(1) time. 10. Delete(index) – deletes the element in the specified heap position by moving the item in the last array position to index, then using Heapify_Down to reposition that item. This is implemented in O(log n) time for heaps that have n elements. 11. ExtractMin() - identifies and deletes the element with the minimum key value, located at index 1, from the heap. This is a combination of the preceding two operations, and so it takes O(log n) time. 12. Delete(item) - deletes the element item from the heap. This can be implemented as a call to Delete(Position[item]), which operates in O(logn) time for heaps that have N elements provided Position allows the index of v to be returned in 0(1) time 13. Change Priority (item, newPriority), which changes the priority value of element v to newPriority. To implement this operation in O(logn)time, we first need to be able to identify the position of element u in the array, which we do by using the position dictionary(look-up table). Once we have identified the position of element u, we change the key and then apply Heapify-up or Heapify-down as appropriate.
Expert Solution
steps

Step by step

Solved in 4 steps with 5 images

Blurred answer