1 / 24

Heaps using abstract specification

Heaps using abstract specification. Abstract specification for trees. A new ADT: Priority Queue. Definition of heaps. Insertion in a heap. Heaps using abstract specification. Abstract specification for trees. What’s the point of the notation?.

kirby
Download Presentation

Heaps using abstract specification

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Heaps using abstract specification

  2. Abstract specification for trees A new ADT: Priority Queue Definition of heaps Insertion in a heap Heaps using abstract specification

  3. Abstract specification for trees

  4. What’s the point of the notation? Firstly, the design of an algorithm is simplified a lot. blup(h.L): h = Ø → Ø h ≠ Ø → blup(L).h If I have a head, I’ll process the list and have the head after. Let’s call the first element h and the remainder L. Secondly, Java is one of many languages and you won’t use this one all your life. In other languages you actually write it like in the specification. Scheme, Lisp, F#... All functional languages. Heaps using abstract specification

  5. What’s a Node in a Tree? Right son <k, r, l> Key Left son <3, <2, Ø, Ø >, <7, <5, Ø, Ø>, <8, Ø, Ø> > > 3 <3, <2, Ø, Ø >, <7, <5, Ø, Ø>, <8, Ø, Ø> >> 7 2 5 8 Heaps using abstract specification

  6. Writing algorithms Write the algorithm Contains that returns true if α is in the binary search tree and false otherwise, using the syntax <k, l, r>. I stop when I’m out of the tree (didn’t find it), or when I find it. Contains(α, Ø) = false α = k → k α < k → Contains(α, l) α > k → Contains(α, r) Navigation. Heaps using abstract specification

  7. Writing algorithms Write the algorithm Add that adds the key α to the binary search tree, using the syntax <k, l, r>. Create a new node Add(α, Ø) = < α, Ø, Ø > α = k → <k, l, r> α < k → <k, l, Add(α, r) > α > k → <k, Add(α, l), r> If I don’t allow dupplicates, I return the node. Otherwise I return Add(α, l). Navigate Heaps using abstract specification

  8. Writing algorithms Rotations can also be written simply: just describe what goes where. Heaps using abstract specification

  9. Writing algorithms What about finding if a key is in a binary tree (i.e. if no order is maintained on the keys)? Complexity: T(n) = 2T(n/2) + O(1) hence T(n) is in O(n). Find(α, Ø) = false α = k → true α≠ k → Find(α, l) v Find(α, r) Same thing to stop. But now, if I don’t find it, I ask: is it in at least one of the children? In Java, you would store the result from Find(α, l), and store the result from Find(α, r), then look at how to combine them. Heaps using abstract specification

  10. A new abstract data type: priority queues

  11. What’s a priority queue? Often, you have to handle tasks with different priorities. High priority Working on the assignments Low priority Have fun Heaps using abstract specification

  12. What’s a priority queue? Often, you have to handle tasks with different priorities. To model it, your objects have a priority associated to them. Eat Work Fun Priority: 1 (High) Priority: 3 (Medium) Priority: 8 (Low) Heaps using abstract specification

  13. What’s a priority queue? Often, you have to handle tasks with different priorities. To model it, your objects have a priority associated to them. key A priority queue is an ADT with: add(int key, Object e) And a priority, from smaller to larger keys or vice-versa: Either it supports min operations, or it supposts max. Not both! int minKey() int maxKey() Priority is given to largest values Priority is given to smallest values Object minElement() Object maxElement() Object removeMin() Object removeMax() Heaps using abstract specification

  14. What’s a priority queue? Imagine that this cute fluffy cloud is a priority queue. The priority is on small keys (so we support min operations). q.add(5,work); {5, work} {3, eat} q.add(3,eat); {1,send e-mail to prof} q.add(8,fun); {8, fun} q.add(1,send e-mail to prof); Heaps using abstract specification

  15. What’s a priority queue? Imagine that this cute fluffy cloud is a priority queue. The priority is on small keys (so we support min operations). send e-mail to prof {5, work} eat {3, eat} So, what’s the magic behind the cloud? work {1,send e-mail to prof} fun {8, fun} while(!q.empy()) System.out.println(q.removeMin()); Heaps using abstract specification

  16. Heaps

  17. What’s a heap? A heap is one family of data structures that implements priority queues. We’re interested in binary heaps. We’ll study max heaps, that is, heaps supporting max operations. A heap has two properties: • values. Each node is ≥ to all of its children. • shape. All levels of the tree must be full, and nodes are added from left to right in the last level. Heaps using abstract specification

  18. Inserting in a heap We want to insert in a way such that each level is full, from left to right. How can you navigate in order to insert a new key to the last place? Heaps using abstract specification

  19. Inserting in a heap We want to insert in a way such that each level is full, from left to right. How can you navigate in order to insert a new key to the last place? Heaps using abstract specification

  20. Inserting in a heap We want to insert in a way such that each level is full, from left to right. Let’s use a bit of magic. ? ? This new node is the 5th one to be inserted. In binary, 5 is 01. 1 Let’s drop the first 1. When we see a 0 we go left, and with a 1 we go right. Heaps using abstract specification

  21. Inserting in a heap We want to insert in a way such that each level is full, from left to right. The magic recipe Let k be the number of elements. Let L be the binary representation of k without the first 1. Read L and: • go left for 0 • go right for 1 Heaps using abstract specification

  22. Inserting in a heap 5: 101. Left, right. 6: 110. Right, left. This process takes care of the shape. We also have to take care of the values. I will have 2 elements. 2 in binary is (10), so my list will be 0. I will have 3 elements. 3 in binary is (11), so my list will be 1. I will have 4 elements. 4 in binary is (100), so my list will be 00. Navigation: left. Navigation: right. Navigation: left.left. Heaps using abstract specification

  23. Inserting in a heap 8 This process takes care of the shape. We also have to take care of the values. 7 4 6 In a max heap, the key of the parent must always be greater than the keys of its children. 3 2 1 We have to keep this property when we insert a new element. Solution: when you’re navigating, if you’re greater than an element that you’re going through, then swap with it. Heaps using abstract specification

  24. Inserting in a heap Assuming you have a function binaryList, implement it in Java…  add(α, <k, l, r>) = add(α, <k, l, r>, binaryList(α) ) add(α, <k, l, r>, h.L) : h = 1 ^ α > k → < α, l, add(k, r, L) > h = 1 ^ α≤ k → < k, l, add(α, r, L) > h = 0 ^ α > k → < α, add(k, l, L), r> h = 0 ^ α ≤ k → < k, add(α, l, L), r> If I have to go right and the key I’m inserting is greater than the current one C then swap the elements and continue with C in the right subtree. Heaps using abstract specification

More Related