Presentation is loading. Please wait.

Presentation is loading. Please wait.

Heaps.

Similar presentations


Presentation on theme: "Heaps."— Presentation transcript:

1 Heaps

2 Heaps Definition Recall It must satisfy the following property:
A heap is a certain kind of complete binary tree. Recall Complete Binary Tree Every level but lowest has all possible nodes If lowest level is not full, all nodes as far left as possible It must satisfy the following property: The entry contained by the node is NEVER less than the entries of the node’s children // this is called the heap property

3 Height of a Heap Theorem: A heap storing n keys has height h≤ log n
Proof: (we apply the complete binary tree property) Let h be the height of a heap storing n keys Since there are 2i keys at depth i = 0, … , h - 1 and at least one key at depth h, we have n  … + 2h Thus, n  2h , i.e., h  log n 1 2 h - keys depth

4 Adding a Node to a Heap 45 Put the new node in the next available spot. (note: a heap is a complete tree) Restore the heap property: Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 35 23 27 21 22 4 19 42 We can add new elements to a heap whenever we like. Because the heap is a complete binary tree, we must add the new element at the next available location, filling in the levels from left-to-right. In this example, I have just added the new element with a key of 42. Of course, we now have a problem: The heap property is no longer valid. The 42 is bigger than its parent 27. To fix the problem, we will push the new node upwards until it reaches an acceptable location.

5 Adding a Node to a Heap 45 Put the new node in the next available spot. (note: a heap is a complete tree) Restore the heap property: Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 35 23 42 21 22 4 19 27 Here we have pushed the 42 upward one level, swapping it with its smaller parent 27. We can't stop here though, because the parent 35 is still smaller than the new node 42.

6 Adding a Node to a Heap 45 Put the new node in the next available spot. (note: a heap is a complete tree) Restore the heap property: Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 42 23 35 21 22 4 19 27 Can we stop now? Yes, because the 42 is less than or equal to its parent.

7 Adding a Node to a Heap 45 The parent has a key that is >= new node, or The node reaches the root. The process of pushing the new node upward is called reheapification upward. 42 23 35 21 22 4 19 27 In general, there are two conditions that can stop the pushing upward: 1. We reach a spot where the parent is >= the new node, or 2. We reach the root. This process is called reheapification upward). Note: we need to easily go from child to parent as well as parent to child.

8 Removing a Node from a Heap
45 Which node to remove? Remove the top of the Heap (the root node) !!! 42 23 35 21 22 4 We can also remove the top node from a heap. The first step of the removal is to move the last node of the tree onto the root. In this example we move the 27 onto the root. 19 27

9 Removing a Node from a Heap
27 Reconstruct the complete binary tree property: Move the last node onto the root. 42 23 35 21 22 4 We can also remove the top node from a heap. The first step of the removal is to move the last node of the tree onto the root. In this example we move the 27 onto the root. 19 27

10 Removing a Node from a Heap
Reconstruct the Heap property: Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 4 22 21 35 23 27 42 We'll fix the problem by pushing the out-of-place node downward. Perhaps you can guess what the downward pushing is called....reheapification downward.

11 Removing a Node from a Heap
Reconstruct the Heap property: Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 4 22 21 35 23 42 27 When we push a node downward it is important to swap it with its largest child. (Otherwise we are creating extra problems by placing the smaller child on top of the larger child.) This is what the tree looks like after one swap. Should I continue with the reheapification downward?

12 Removing a Node from a Heap
Reconstruct the Heap property: Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 4 22 21 27 23 42 35 Yes, I swap again, and now the 27 is in an acceptable location.

13 Removing a Node from a Heap
19 4 22 21 27 23 42 35 All the children have keys <= the out-of-place node, or The node reaches the leaf. The process of pushing the new node downward is called reheapification downward. Reheapification downward can stop under two circumstances: 1. The children all have keys that are <= the out-of-place node. 2. The out-of-place node reaches a leaf.

14 Heap Implementation Use linked node Use arrays
node implementation is for a general binary tree but we may need to have doubly linked node Use arrays A heap is a complete binary tree which can be implemented more easily with an array than with linked nodes and do two-way links

15 Array Representation of a Heap
Recall For the node with index i the left child is at index 2i the right child is at index 2i + 1 Links between nodes are not explicitly stored The cell of at index 0 is not used

16 Implementing a Heap 42 We will store the data from the nodes in a partially-filled array. 35 23 27 21 This slide shows the typical way that a heap is implemented. For the most part, there is nothing new here, because you already know how to implement a complete binary tree using a partially-filled array. That is what we are doing with the heap. An array of data

17 Implementing a Heap 42 Data from the root goes in the first location of the array. 35 23 27 21 Following the usual technique for implementing a complete binary tree, the data from the root is stored in the first entry of the array. 42 An array of data

18 Implementing a Heap 42 Data from the next row goes in the next two array locations. 35 23 27 21 The next two nodes go in the next two locations of the array. 42 35 23 An array of data

19 Implementing a Heap 42 Data from the next row goes in the next two array locations. 35 23 27 21 and so on. 42 35 23 27 21 An array of data

20 implementation in an ADT: array and size
Comparable[] a; int size; private boolean full() { // first element of array is empty return (size == a.length-1); }

21 implementation in an ADT: insertion
public void add(Comparable data) { if (full()) // expand array ensureCapacity(2*size); size++; a[size] = data; if (size > 1) heapifyUp(); }

22 implementation in an ADT: heapifyUp
private void heapifyUp() { Comparable temp; int next = size; while (next != 1 && a[next].compareTo(a[next/2]) > 0) temp = a[next]; a[next] = a[next/2]; a[next/2] = temp; next = next/2; }

23 implementation in an ADT: deletion
public Comparable removeMax() { if (size == 0) throw new IllegalStateException(“empty heap”); Comparable max = a[1]; a[1] = a[size]; size--; if (size > 1) heapifyDown(1); return max; }

24 implementation in an ADT: heapifyDown
private void heapifyDown(int root) { Comparable temp; int next = root; while (next*2 <= size) // node has a child int child = 2*next; // left child if (child < size && a[child].compareTo(a[child+1]) < 0) child++; // right child instead if (a[next].compareTo(a[child]) < 0) temp = a[next]; a[next] = a[child]; a[child] = temp; next = child; } else; next = size; // stop loop

25 Merging Two Heaps We are given two heaps and a key k
7 5 2 8 4 6 We are given two heaps and a key k We create a new heap with the root node storing k and with the two heaps as subtrees We perform heapifyDown to restore the heap-order property 3 7 5 2 8 4 6 8 7 5 2 6 4 3

26 Example We have 15 element with keys: 24, 25, 36, 28, 34, 33, 17, 10, 15, 35, 29, 13, 33, 32, 30 First we construct (n+1)/2 element as 24 25 36 28 34 33 17 10 Add one more key for each pairs and do the merge process: 15 25 24 35 28 36 29 33 34 13 10 17

27 Example 24, 25, 36, 28, 34, 33, 17, 10, 15, 35, 29, 13, 33, 32, 30 15 25 24 35 28 36 29 33 34 13 10 17 heapifyDown process: 25 15 24 36 28 35 34 33 29 17 10 13

28 Example 24, 25, 36, 28, 34, 33, 17, 10, 15, 35, 29, 13, 33, 32, 30 Add one more key for each two subtrees and do the merge process: 33 32 25 36 34 17 24 15 35 28 29 31 13 10 heapifyDown process: 36 25 15 24 35 28 33 34 32 31 29 17 10 13

29 Example 24, 25, 36, 28, 34, 33, 17, 10, 15, 35, 29, 13, 33, 32, 30 Merge process: 30 36 34 25 35 32 17 24 15 33 28 29 31 13 10 heapifyDown process: 36 35 25 15 24 33 28 30 34 32 31 29 17 10 13

30 Bottom-up Heap Construction
We can construct a heap storing n given keys in using a bottom-up construction with log n phases In phase i, pairs of heaps with 2i -1 keys are merged into heaps with 2i+1-1 keys 2i -1 2i+1-1


Download ppt "Heaps."

Similar presentations


Ads by Google