Presentation is loading. Please wait.

Presentation is loading. Please wait.

Podcast Ch22b Title: Inserting into a Heap

Similar presentations


Presentation on theme: "Podcast Ch22b Title: Inserting into a Heap"— Presentation transcript:

1 Podcast Ch22b Title: Inserting into a Heap
Description: Overview; inserting into a heap; pushHeap method Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

2 Heaps A maximum heap is an array‑based tree in which the value of a parent is ≥ the value of its children. A minimum heap uses the relation ≤.

3 For each tree, indicate whether it is a heap (maximum or minimum) or is not a heap.
(a) Maximum heap Minimum heap No heap (b) Maximum heap Minimum heap No heap (c) Maximum heap Minimum heap No heap

4 Inserting into a Heap Assume that the array the elements in the index range 0  i < last < n form a heap. The new element will enter the array at index last with the heap expanding by one element.

5 Inserting into a Heap (cont)
Insert item into a heap by moving nodes on the path of parents down one level until the item is assigned as a parent that has heap ordering. Path of parents for insertion of item = 50

6 Inserting into a Heap (cont)
The static method pushHeap() of the class Heaps inserts a new value in the heap. The parameter list includes the array arr, the index last, the new value item, and a Comparator object of type Greater or Less indicating whether the heap is a maximum or minimum heap.

7 Inserting into a Heap (continued)
The algorithm uses an iterative scan with variable currPos initially set to last. At each step, compare the value item with the value of the parent and if item is larger, copy the parent value to the element at index currPos and assign the parent index as the new value for currPos. The effect is to move the parent down one level. Stop when the parent is larger and assign item to the position currPos.

8 Inserting into a Heap (continued)

9 Start with the min heap show below. Draw the heap after inserting 25.

10 pushHeap() // the array elements in the range
// (0, last) are a heap; insert item // into the heap so that the range // (0, last+1) is a heap; use the Comparator // comp to perform comparisons public static <T> void pushHeap(T[] arr, int last, T item, Comparator<? super T> comp) { // assume the new item is at location // arr[last] and that the elements arr[0] // to arr[last-1] are in heap order int currPos, parentPos; // currPos is an index that traverses // path of parents; item is assigned // in the path currPos = last; parentPos = (currPos-1)/2;

11 pushHeap() (concluded)
// traverse path of parents up to the root while (currPos != 0) { // compare target and parent value if (comp.compare(item,arr[parentPos]) < 0) { // move data from parent position to // current position; update current position // to parent position; compute next parent arr[currPos] = arr[parentPos]; currPos = parentPos; parentPos = (currPos-1)/2; } else // heap condition is ok; break break; // the correct location has been discovered; // assign target arr[currPos] = item;

12 For each of the following array-based trees, identify all of the properties that are valid.
1. maximum heap 2. minimum heap 3. sorted tree (array used to build the tree is sorted) 4. tree in which every level has all possible nodes binary search tree Tree (a) has properties: _________________________ Tree (b) has properties: _________________________ Tree (c) has properties: _________________________ Tree (d) has properties: _________________________ Tree (e) has properties: _________________________


Download ppt "Podcast Ch22b Title: Inserting into a Heap"

Similar presentations


Ads by Google