Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Heaps. 2 Background: Priority Queues. zQueues are a First-In, First-Out data structure; zPriority Queues are similar, except those of highest priority.

Similar presentations


Presentation on theme: "1 Heaps. 2 Background: Priority Queues. zQueues are a First-In, First-Out data structure; zPriority Queues are similar, except those of highest priority."— Presentation transcript:

1 1 Heaps

2 2 Background: Priority Queues. zQueues are a First-In, First-Out data structure; zPriority Queues are similar, except those of highest priority must be removed first. zHow should we implement this?

3 3 Priority Queue Implementation zArrays yInserts: O(logn) to find spot; O(n) to move all others down. yDeletes: O(1) to remove; O(n) to move others. zLinked Lists : yInserts: O(n) to find spot; O(1) to do insert. yDeletes: O(1) to remove.

4 4 Priority Queue Implementation zAVL-Trees yInserts: O(logn) to find spot; O(logn) to do rotation(s). yDeletes: O(logn) to remove; O(logn) to do rotations. zHeaps : yInserts: O(1) to find spot; O(logn) to do insert. yDeletes: O(1) to find; O(logn) to delete/rotate.

5 5 Heaps Vs. AVL-Trees zAVL-Trees have a lot of overhead to balance. zAVL-Trees store the values in order. zHeaps only provide access to the least/greatest. zThis requires less overhead and is easier to implement.

6 6 What is a Heap? zBINARY MIN HEAP is a complete binary tree in which each node is empty or has a key value that is less than both of its children. Both children are Binary Min Heaps zA COMPLETE BINARY TREE is a binary tree where the root is either empty OR T L is a perfect tree of height h-1 and T R is a complete tree of height h-1 OR T L is a complete tree of height h-1 and T R is a perfect tree of height h-2

7 7 More Definitions zA PERFECT TREE is one where the root is either empty or has H L – H R = 0 and both sub trees are also perfect trees. zi.e. the last level (and all levels) are completely filled.

8 8 Heap as a Tree zAs a tree, inserts would fill a level entirely, left to right before going on to the next level: A BC DEF G

9 9 Heap Insert Algorithm zThe previous slide discussed where the new node would go, but how to maintain the order of the heap? zAlgorithm: yCompare new node to parent; if the new node is greater than the parent, stop. yElse, swap new node and parent. Repeat algorithm at parent (percolate up).

10 10 Heap Example zNow let’s do an extended example, inserting a series of priority values into a heap. zWe will be inserting 90, 50, 25, 10, 15, 20, 75. zLet’s start with 90:

11 11 Heap Example z90 is no problem. zNow, insert 50: 90 Left to insert: 50, 25, 10, 15, 20, 75

12 12 Heap Example zDoes this require a swap? zYes: 90 50 Left to insert: 25, 10, 15, 20, 75

13 13 Heap Example zDone. Now insert 25: 50 90 Left to insert: 25, 10, 15, 20, 75

14 14 Heap Example zSwap? zYes: 50 90 Left to insert:10, 15, 20, 75 25

15 15 Heap Example zNow insert 10: 25 90 Left to insert:10, 15, 20, 75 50

16 16 Heap Example zSwap? zYes: 25 90 Left to insert:15, 20, 75 50 10

17 17 Heap Example zSwap again? zYes: 25 10 Left to insert:15, 20, 75 50 90

18 18 Heap Example zDone. Now insert 15: 10 25 Left to insert:15, 20, 75 50 90

19 19 Heap Example zSwap? zYes: 10 25 Left to insert:20, 75 50 9015

20 20 Heap Example zSwap again? zNo. 15 is where it needs to be. Now 20: 10 15 Left to insert:20, 75 50 9025

21 21 Heap Example zSwap? zYes: 10 15 Left to insert:75 50 902520

22 22 Heap Example zSwap again? zNo. 20 is good. Finally, insert 75: 10 15 Left to insert:75 20 902550

23 23 Heap Example zSwap? zNo. 75 is good; we are done. 10 15 Left to insert:done 20 90255075

24 24 Deleting from a Heap zThe minimum value is at the root of the tree; thus, it can be found easily, but how do we rebalance? zAlgorithm: yRemove Root’s value as minimum value yReplace root with last node; delete last. yIf root is less than both its children, stop. yElse, swap root and min child; repeat from min child node (percolate down).

25 25 Heap Delete Example zFirst, 10 must be removed and replaced with 75: 10 1520 90255075

26 26 Heap Delete Example zIs 75 less than both of its children? zNo. Swap with minimum (15): 75 1520 902550

27 27 Heap Delete Example zIs 75 less than both of its children? zNo. Swap with minimum (25): 15 7520 902550

28 28 Heap Delete Example z75 is at the terminal level; done. 15 2520 907550

29 29 Implementation Problem zThe problem with the tree/pointer representation is how to maintain the “next” pointer. zI think this can be done by keeping a counter and manipulating it to decide left or right from the root. zBut there is another way:

30 30 Array Implementation zAny binary tree may be represented in an array. zThe only questions really are yWhere are the two children of a node? yWhere is a node’s parent? zAnswer (for a node at position i): yThe two children are at 2*i and 2*i+1 yThe parent is at i div 2.

31 31 Array Example: zFor this tree: zHere’s the array: 10 1520 90255075 15 2 10 1 25 5 90 4 20 3 75 7 50 6

32 32 The End Slide z


Download ppt "1 Heaps. 2 Background: Priority Queues. zQueues are a First-In, First-Out data structure; zPriority Queues are similar, except those of highest priority."

Similar presentations


Ads by Google