Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem of the Day  You are trapped alone in a dark room with:  Candle;  Wood stove; and  Gas lamp (with full tank).  You only have one match; what.

Similar presentations


Presentation on theme: "Problem of the Day  You are trapped alone in a dark room with:  Candle;  Wood stove; and  Gas lamp (with full tank).  You only have one match; what."— Presentation transcript:

1 Problem of the Day  You are trapped alone in a dark room with:  Candle;  Wood stove; and  Gas lamp (with full tank).  You only have one match; what do you light 1 st ?

2 Problem of the Day  You are trapped alone in a dark room with:  Candle;  Wood stove; and  Gas lamp (with full tank).  You only have one match; what do you light 1 st ?

3 Problem of the Day  You are trapped alone in a dark room with:  Candle;  Wood stove; and  Gas lamp (with full tank). one match  You only have one match; what do you light 1 st ?

4 Problem of the Day  You are trapped alone in a dark room with:  Candle;  Wood stove; and  Gas lamp (with full tank). one match  You only have one match; what do you light 1 st ? The match!

5 CSC 212 – Data Structures

6 Priority Queue ADT  Prioritizes Entry s using their keys  For Entry s with equal priorities, order not specified  Priority given to each value when added to PQ  Normally, the priority not changeable while in PQ  Access single Entry : one with the lowest priority  Returns Entry using min() or removeMin()  Location are imaginary – only smallest matters

7 Heaps  Binary-tree based PQ implementation  Still structured using parent-child relationship  At most 2 children & 1 parent for each node in tree  Heaps must also satisfy 2 additional properties  Parent at least as important as its children  Structure must form a complete binary tree 2 95 67

8 BinaryTree   Picturing Linked BinaryTree B C A D   BACD BinaryTree root size  4

9 Legal CompleteBinaryTree  ADT which extends BinaryTree  Add & remove methods defined plus those inherited  For this ADT, trees must maintain specific shape  Fill lowest level first, then can start new level below it 2 95 67 Illegal 2 95 7 6

10 CompleteBinaryTree  ADT which extends BinaryTree  Add & remove methods defined plus those inherited  For this ADT, trees must maintain specific shape  Fill lowest level first, then can start new level below it  Lowest level must be filled in from left-to-right Legal 2 95 67 Illegal 2 95 67

11 What Is Purpose of a Heap?  Root has critical Entry that we always access  Entry at root always has smallest priority in Heap  O(1) access time in min() without any real effort  CompleteBinaryTree makes insert() easy  Create leftmost child on lowest level  When a level completes, start next one  Useful when:

12 Upheap  Insertion may violate heap-order property  Upheap immediately after adding new Entry  Goes from new node to restore heap’s order  Compare priority of node & its parent  If out of order, swap node's Entry s  Continue upheaping from parent node  Stop only when either case occurs:  Found properly ordered node & parent  Binary tree's root is reached

13 6 insert() in a Heap 2 5 79

14 6 1 2 5 79

15 6 1 Start your upheaping! 2 5 79

16 6 1 2 5 79

17 1 6 2 5 79

18 1 6 Upheaping Must Continue 2 5 79

19 1 6 2 5 79

20 2 6 Upheaping Sounds Icky 1 5 79

21 2 6 Check If We Should Continue 1 5 79

22 2 6 Stop At The Root 1 5 79

23 2 6 insert() Once Again 1 5 793

24 2 6 Upheaping Begins Anew 1 5 793

25 2 6 Maintain Heap Order Property 1 5 793

26 2 6 We Are Done With This Upheap! 1 5 793

27 Removing From a Heap  removeMin() must kill Entry at heap’s root  For a complete tree, must remove last node added  How to reconcile these two different demands?  Removed node's Entry moved to the root  Then remove node from the complete tree  Heap's order preserved by going down

28 Removing From a Heap  removeMin() must kill Entry at heap’s root  For a complete tree, must remove last node added  How to reconcile these two different demands?  Removed node's Entry moved to the root  Then remove node from the complete tree  Heap's order preserved by going down

29 Downheap  Restores heap’s order during removeMin()  Downheap work starts at root  Swap with smallest child, if at least out-of-order  Downheaping continues with old smallest child  Stop at leaf or when node is legal

30 5 Before removeMin() is called 1 2 97

31 5 Move Last Entry Up To Root 1 2 97

32 5 1 2 97

33 5 9 2 7

34 5 Compare Parent W/Smaller Child 9 2 7

35 5 9 2 7

36 5 2 9 7

37 5 Continue Downheaping W/Node 2 9 7

38 5 2 9 7

39 5 Swap If Out Of Order 2 7 9

40 5 Check If We Should Continue 2 7 9

41 5 Stop When We Reach a Leaf 2 7 9

42 Implementation Excuses  upheap & downheap travel height of tree  O (log n ) running time for each of these  Serves as bound for adding & removing from PQ  What drawbacks does heap have?  PriorityQueue can be faster using Sequence  Only for specific mix of operations, however  PriorityQueue using heaps are fastest overall

43 Sequence -based BinaryTree  Node at index specified for location in T REE  Root node stored at index 0  Root’s left child at index 1  Right child of root at index 2  Left child’s right child at index 4  Right child’s left child at index 5  Node at index n ’s left child is at index 2n + 1  Node at index n ’s right child is at index 2n + 2

44 Sequence -based BinaryTree  Node at index specified for location in T REE  Root node stored at index 0  Root’s left child at index 1  Right child of root at index 2  Left child’s right child at index 4  Right child’s left child at index 5  Node at index n ’s left child is at index 2n + 1  Node at index n ’s right child is at index 2n + 2 But how much space will this need for to hold a heap?

45 Sequence to Implement Heap 2 0 2 0

46 29 01 2 01 9

47 293 012 2 01 9 2 3 `

48 29399 0123 2 01 9 2 3 3 `

49 Sequence to Implement Heap 29399 0123 2 01 9 2 3 3 ` Add nodes to end of the Sequence Similarly, remove node at end NO space is wasted for this! Add nodes to end of the Sequence Similarly, remove node at end NO space is wasted for this!

50 Heaps  Binary-tree based PQ implementation  Still structured using parent-child relationship  At most 2 children & 1 parent for each node in tree  Heaps must also satisfy 2 additional properties  Parent at least as important as its children  Can not use any tree; must form complete binary tree 2 95 67


Download ppt "Problem of the Day  You are trapped alone in a dark room with:  Candle;  Wood stove; and  Gas lamp (with full tank).  You only have one match; what."

Similar presentations


Ads by Google