Heaps using abstract specification
Heaps using abstract specification
Abstract specification for trees
A new ADT: Priority Queue
Definition of heaps
Insertion in a heap
Abstract specification for trees
What’s the point of the notation?
Heaps using abstract specification
blup(h.L):
h = Ø → Ø
h ≠ Ø → blup(L).h
Firstly, the design of an algorithm is simplified a lot.
Let’s call the first element h and the remainder L.
If I have a head, I’ll process the list and have
the head after.
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.
What’s a Node in a Tree?
Heaps using abstract specification
<k, r, l>Key
Right son
Left son
3
72
5 8
<3, <2, Ø, Ø >, <7, <5, Ø, Ø>, <8, Ø, Ø> > >
<3,
<2, Ø, Ø >,
<7,
<5, Ø, Ø>,
<8, Ø, Ø> >
>
Writing algorithms
Heaps using abstract specification
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.
Navigation.
Contains(α, Ø) = false
α = k → k
α < k → Contains(α, l)
α > k → Contains(α, r)
Writing algorithms
Heaps using abstract specification
Write the algorithm Add that adds the key α to the binary search tree, using the syntax <k, l, r>.
Create a new node
If I don’t allow dupplicates, I return the node. Otherwise
I return Add(α, l).
Add(α, Ø) = < α, Ø, Ø >
α = k → <k, l, r>
α < k → <k, l, Add(α, r) >
α > k → <k, Add(α, l), r>Navigate
Writing algorithms
Heaps using abstract specification
Rotations can also be written simply: just describe what goes where.
Writing algorithms
Heaps using abstract specification
What about finding if a key is in a binary tree (i.e. if no order is maintained on the keys)?
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.
Complexity: T(n) = 2T(n/2) + O(1) hence T(n) is in O(n).
A new abstract data type: priority queues
What’s a priority queue?
Heaps using abstract specification
Often, you have to handle tasks with different priorities.
High priority
Working on the assignments
Low priority
Have fun
What’s a priority queue?
Heaps using abstract specification
Often, you have to handle tasks with different priorities.
To model it, your objects have a priority associated to them.
Work FunEatPriority: 1 (High) Priority: 3 (Medium) Priority: 8 (Low)
Heaps using abstract specification
Often, you have to handle tasks with different priorities.
To model it, your objects have a priority associated to them.
A priority queue is an ADT with:
add(int key, Object e)
int minKey()
key
Object removeMin()
Object minElement()
int maxKey()
Object removeMax()
Object maxElement()
And a priority, from smaller to larger keys or vice-versa:
Priority is given to smallest values
Priority is given to largest values
Either it supports min operations, or
it supposts max. Not
both!
What’s a priority queue?
Heaps using abstract specification
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);
q.add(3,eat);
q.add(8,fun);
q.add(1,send e-mail to prof);
{5, work}{3, eat}
{8, fun}
{1,send e-mail to prof}
Heaps using abstract specification
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).
{5, work}{3, eat}
{8, fun}
{1,send e-mail to prof}
while(!q.empy())
System.out.println(q.removeMin());
send e-mail to prof
eat
work
fun
So, what’s the magic behind the cloud?
Heaps
What’s a heap?
Heaps using abstract specification
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.
Inserting in a heap
Heaps using abstract specification
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?
Inserting in a heap
Heaps using abstract specification
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?
Inserting in a heap
Heaps using abstract specification
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.
Inserting in a heap
Heaps using abstract specification
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
Inserting in a heap
Heaps using abstract specification
I will have 2 elements. 2 in binary is (10), so my list will be 0.
Navigation: left.
I will have 3 elements. 3 in binary is (11), so my list will be 1.
Navigation: right.
I will have 4 elements. 4 in binary is (100), so my list will be 00.
Navigation: left.left.
5: 101. Left, right.6: 110. Right, left.This process takes care of the shape. We also have
to take care of the values.
Inserting in a heap
Heaps using abstract specification
This process takes care of the shape. We also have
to take care of the values.
In a max heap, the key of the parent must always be greater than the keys of its children.
7
4 6
13 2
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.
8
Inserting in a heap
Heaps using abstract specification
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.
Assuming you have a function binaryList, implement it in Java…