Title: HEAPS
1HEAPS
- Heaps
- Properties of Heaps
- HeapSort
- Bottom-Up Heap Construction
- Locators
2Heaps
- A Heap is a Binary Tree H that stores a
collection of - keys at its internal nodes and that
satisfies two - additional properties
- -1)Relational Property
- -2)Structural Property
3Heap Properties
- Heap-Order Property (Relational)
- In a heap H, for every node v (except the
root), the key stored in v is greater than or
equal to the key stored in vs parent. - A structure fulfilling the Heap-Order Property
- A structure which fails to fulfill the Heap-Order
Property
4Heap Properties
- Complete Binary Tree (Structural) A Binary
Tree T - is complete if each level but the last is
full, and, in - the last level, all of the internal nodes
are to the - left of the external nodes.
- A structure fulfilling the Structural Property
A structure which fails to fulfill the
Structural Property
5Heaps (contd.)
6Height of a Heap
- Proposition A heap H storing n keys has height
- h log(n1)ù ( ceiling of n1 )
- Justification Due to H being complete, we
know - - i of internal nodes is at least
- 1 2 4 ... 2 h-2 1 2 h-1 -1 1
2 h-1 - - i of internal nodes is at most
- 1 2 4 ... 2 h-1 2 h - 1
- - Therefore
- 2 h-1 n and n 2 h - 1
- - Which implies that
- log(n 1) h log n 1
- - Which in turn implies
- h log(n1)ù
- - Q.E.D.
7Height of a Heap (contd.)
- Lets look at that graphically
Consider this heap which has height h 4 and n
13 Suppose two more nodes are added. To
maintain completeness of the tree, the two
external nodes in level 4 will become internal
nodes i.e. n 15, h 4 log
(151) Add one more n 16, h 5 ceiling
log(161)
8Heap Insertion
- So here we go ...
- The key to insert is 6
9Heap Insertion
Add the key in the next available spot in the
heap.
10Upheap
- Upheap checks if the new node is smaller
- than its parent. If so, it switches the two
- Upheap continues up the tree
11Removal From a HeapRemoveMinElement()
The removal of the top key leaves a hole We
need to fix the heap First, replace the hole
with the last key in the heap Then, begin
Downheap
12Downheap
- Downheap compares the parent with the smallest
- child. If the child is smaller, it switches the
two.
13Downheap Continues
14Downheap Continues
15End of Downheap
Downheap terminates when the key is greater
than the keys of both its children or the
bottom of the heap is reached.
(total switches) lt (height of tree - 1) log N
16Implementation of a Heap
- public class HeapSimplePriorityQueue
implementsSimplePriorityQueue BinaryTree
TPosition lastComparator comparator... -
17Implementation of a Heap(cont.)
Two ways to find the insertion position z in a
heap
18Heap Sort
- All heap methods run in logarithmic time or
better - If we implement PriorityQueueSort using a heap
for - our priority queue, insertItem and
removeMinElement - each take O(logk), k being the number of elements
in - the heap at a given time.
- We always have n or less elements in the heap,
so - the worst case time complexity of these
methods is - O(logn).
- Thus each phase takes O(nlogn) time, so the
- algorithm runs in O(nlogn) time also.
- This sort is known as heap-sort.
- The O(nlogn) run time of heap-sort is much
better - than the O(n 2 ) run time of selection and
insertion - sort.
19Bottom-Up Heap Construction
- If all the keys to be stored are given in
advance we - can build a heap bottom-up in O(n)
time. - Note for simplicity, we describe bottom-up
heap - construction for the case for n keys
where n 2 h 1 - h being the height.
- Steps
- 1) Construct (n1)/2 elementary heaps with
one key each. - 2) Construct (n1)/4 heaps, each with 3
keys, by joining pairs of elementary heaps
and adding a new key as the root. The new key
may be swapped with a child in order to perserve
heap- order property. - 3) Construct (n1)/8 heaps, each with 7
keys, by joining pairs of 3-key heaps and adding
a new key. Again swaps may occur. - ...
- 4) In the ith step, 2lt i lt h, we form
(n1)/2 i heaps, each storing 2 i -1 keys, by
joining pairs of heaps storing - (2 i-1 -1) keys.
- Swaps may occur.
20Bottom-Up Heap Construction(cont.)
21Bottom-Up Heap Construction(cont.)
22Bottom-Up Heap Construction(cont.)
23Bottom-Up Heap Construction(cont.)
24Analysis of Bottom-Up HeapConstruction
- Proposition Bottom-up heap construction with n
- keys takes O(n) time.
- - Insert (n 1)/2 nodes
- - Insert (n 1)/4 nodes
- - Upheap at most (n 1)/4 nodes 1 level.
- - Insert (n 1)/8 nodes
- - ...
- - Insert 1 node.
- - Upheap at most 1 node 1 level.
25Locators
- Locators can be used to keep track of elements
as they are moved around inside a positional
container. - A Locator sticks with a specific element, even
if that - element changes positions in the container.
- The Locator ADT supports the following
fundamental methods - element() Return the element of the item
associated with the Locator. - key() Return the key of the item associated with
the Locator. - isContained() Return true if and only if the
Locator is associated with a container. - container() Return the container associated with
the Locator.
26Locators and Positions
- At this point, you may be wondering what the
- difference is between locators and positions,
and - why we need to distinguish between them.
- Its true that they have very similar methods
- The difference is in their primary usage
- Positions are used primarily to represent the
internal - form of a data structure
- Locators are used primarily to keep track of
data, - regardless of what position its in (and even
if its - not in any position right now)
27Locators and Positions Example
- For example, consider the cs16 Valet Parking
Service (started by the teaching assistants
because they had too much free time on their
hands). - When they began their business, Mike and Ryan
decided to create a data structure to keep track
of where exactly the cars were. - Ryan suggested having Positions represent what
parking space (or part of the green) the car was
in. - However, Mike realized that the car-owners
coming into the CIT to reclaim their car wouldnt
remember what space the TAs had placed their car
in. And the TAs might want to move cars from
space to space. - So he suggested using the UTAs as Locators.
Each UTA would be able to get a car upon demand
(when element() was called on them) and would
know to come running when the cars owner came
by. (the owner would be their key) - And it was done, and the staff were able to
dispense with the last remainders of their free
time.
28Priority Queue with Locators
- It is easy to extend the sequence-based and
heap-based implementations of a Priority Queue to
support Locators. - A Locator holds the key, element pair.
- The Priority Queue ADT can be extended to
implement the Locator ADT - A Locator in this implementation must have a
instance variable pointing to its Position. - All of the methods of the Locator ADT can then
be implemented in O(1) time.