Title: CS1102 Tut 8 Heaps
1CS1102 Tut 8 Heaps
- Max Tan
- tanhuiyi_at_comp.nus.edu.sg
- COM1-01-09 Tel65164364http//www.comp.nus.edu.s
g/tanhuiyi
2Group Assignments
- Group 1 Q3
- Group 2 Q4
- Group 3 Q1
- Group 4 Q2
3First 15 minutes
- Priority Queue ADT
- Heap Data structure that realizes the P-Queue
ADT - Question Must a P-Queue be a heap?
4First 15 minutes
- Priority Queue ADT
- A queue which dequeues items with priority first
- Implementation
- Unsorted array
- Sorted array
- Heap
- and many others!
5First 15 minutes
last
26
- Pick the largest,
- Swap it with the last element of the array
7
18
3
4
16
15
2
1
6First 15 minutes
last
2
- Re-establish heap property (bubble down)
7
18
3
4
16
15
1
7First 15 minutes
last
18
- Select max node, swap with last
7
16
3
4
2
15
1
8First 15 minutes
last
1
7
16
3
4
2
15
9First 15 minutes
last
16
7
15
3
4
2
1
10First 15 minutes
last
1
- Select biggest and swap
- Repeat until done!
- Hence this is an INPLACE sorting algorithm
7
15
3
4
2
11Q1 (a) Answer
- Show the result of
- (a) inserting 12, 10, 1, 14, 6, 5, 8, 15, 3, 9,
7, 4, 11, 13, and 2, one at a time, in an
initially empty heap.
Insert 1
Insert 10
Insert 12
12Q1 (a) Answer
- Show the result of
- (a) inserting 12, 10, 1, 14, 6, 5, 8, 15, 3, 9,
7, 4, 11, 13, and 2, one at a time, in an
initially empty heap.
14 is bigger than parent, Bubble Up
Insert 14
Insert 6
13Q1 (a) Answer
- Show the result of
- (a) inserting 12, 10, 1, 14, 6, 5, 8, 15, 3, 9,
7, 4, 11, 13, and 2, one at a time, in an
initially empty heap.
Insert 5
5 is bigger than parent, Bubble Up
Final tree
.....
14Q1 (b) Answer
- (b) Then show the result by using the heap
construction algorithm instead.
First, just insert the elements one by one,
creating a complete binary tree, ignoring the
heap property
Then, starting from the last internal node, 8, up
to the root, bubble down.
12
12
10
1
1
10
8
14
6
5
14
6
5
15
3
9
7
4
11
13
2
15
3
9
7
4
11
13
2
15Q1 (b) Answer
- (b) Then show the result by using the heap
construction algorithm instead.
Next internal node, 5. Bubble down.
12
12
1
10
1
10
13
14
6
14
5
6
15
3
9
7
4
11
2
15
3
9
7
4
2
16Q1 (b) Answer
- (b) Then show the result by using the heap
construction algorithm instead.
Final tree
Next internal node, 6. Choose the max value of
the children (which is 9). Bubble down.
12
10
1
14
.............
15
3
7
4
2
17Q2
- Using the same heap definition in Q2, write
two more methods as follows (assume you were
given bubbleUp (p) and bubbleDown (p) which
bubble up and down respectively at position p) - (a) updateKey (p, v), which changes the value of
the key at position p to v. - (b) delete (p), which deletes the key at position
p of the heap.
18Q2 updateKey()
- What can happen to the old value after calling
updateKey() - Either increase or decrease
IF oldvalue increases Would you need to bubble
down?
Call updateKey on this node
19Q2 updateKey()
- What can happen to the old value after calling
updateKey() - Either increase or decrease
IF oldvalue decreases Would you need to bubble
up?
Call updateKey on this node
20Q2 (a) Answer
- public void updateKey ( int p, Comparable newVal
) - if (p gt (currentSize-1) p lt 0)
- throw new NoSuchElementException()
- int c newVal.compareTo(arrayp)
- arrayp newVal
- if (c gt 0)
- bubbleUp(p)
- else if (c lt 0)
- bubbleDown(p)
-
-
21Q2 (b) Answer (1)
- public void delete( int p )
- if (p gt (currentSize-1) p lt 0)
- throw new NoSuchElementException()
- currentSize--
- int c arrayp.compareTo(arraycurrentSize)
- arrayp arraycurrentSize
- if (c gt 0)
- bubbleDown(p)
-
- else if (c lt 0)
- bubbleUp(p)
-
22Q2 (b) Answer (2)
- Alternative solution
- Get the last element from the array, reduce the
size by 1, and call updateKey (p, lastElement).
23Q3 (a)
- findBest() which returns the athlete with the
least penalty points. What is the complexity. - Use a min heap! O(1) complexity note that
deletion has not been specified to be required!
24Q3 (b) - 1
- addPenalty(string AtheleteName, int penalty)
which adds the new penalty points to Atheletes
record. Assume names are unique. - Linear search by name in O(n), then call
updateKey() as in the previous question in
O(lgn). Total is O(n)
25Q3 (b) - 2
- addPenalty(string AtheleteName, int penalty)
which adds the new penalty points to Atheletes
record. Assume names are unique. - Use the treap data structure that is introduced
in the next question for a total of O(lgn) where
key is name and priority would be the penalty
points.
26Q4 (a)
- Heap property for every node v, the search key
in v is greater to those in the children of v - BST property for every node v, the search key in
v is greater then those in its left child, and
less then those in its right child
27Q4 (a)
- Is a treap a heap?
- A heap must be complete. A treap does not, so a
treap is not a heap!
28Q4 (b)
- You are given a set of n pairs (k,p) and you are
asked to find a corresponding treap. For
instance, if n 4 and the pairs are (5,9) (7,4)
(8,6) (1,2) then a correct answer is given in
the picture above. How many such treaps are
possible in general? Justify your answer briefly.
- There can only be a unique treap.
- This is because the root must always be the node
with the highest priority to fulfill the heap
property. - To fulfill the BST property, the left child must
have a smaller key then the root, while the right
child must have a bigger key then the root. - Each of these children must also fulfill the heap
property and thus are unique.
29Q4 (c)
- TreeNode insert(TreeNode curr, TreeNode t)
-
- if(curr null)
- return t
- //Insert based on the BST property
- if(t.key gt curr.key)
- curr.right insert(curr.right, t)
- else
- curr.left insert(curr.left, t)
-
- if(curr.priority lt curr.left.priority)
- currrotateRight(curr)
- if(curr.priority lt curr.right.priority)
- currrotateLeft(curr)
- //Check parent for heap violation and
perform rotation - //accordingly
- checkHeap(curr.parent)
- return root
-
30Q4 (d)
- How should you draw ?
- You should realize that, to satisfy the heap
property, you must pick the node with the maximum
priority to be the root node - You can then divide the children into the left
and right subtrees by their key values
31Q4 (d)
Inserting (k 2, p 5), (k 5, p 2), (k 5,
p 1) is just usual BST insertion. No heap
property violation.
32Q4 (d)
Now we insert (k 4, p 7)
Another Violation! Rotate left here
k2 p5
k2 p5
Violation! Rotate right here
k4 p7
k5 p2
k5 p2
k5 p1
k4 p7
k5 p1
Inserted node
33Q4 (d)
Now we insert (k 4, p 7)
34Q4 (d)
Final treap
35Q4
- How many possible treaps?
- The root node must be unique because it has the
be the max priority - The elements in the left and right subtree must
also be unique (since they follow the key in the
root) - The root of the left subtree must be max
priority, same for the right subtree - Hence recursively, the tree must be unique!
36Q4
- Efficient algorithm to insert a node
- B.Tree insert from top
- Heap insert below and bubble up
- Treap Can we insert below ?
- No!
- So insert from the top!
37Q4
- Suppose we insert a node at the root
- If it has a higher priority then the root, then
we know then it should replace the root, and the
old root be inserted into the left or right
subtrees - If it has a lower priority then the root, then we
know that it should be inserted into the left or
right subtrees - But what is wrong with this?
- This might violate the b-tree property!
38Q4
- Hence, we insert the node by just considering the
key values - Recall rotation in AVL trees allow us to maintain
the property of b-tree while balancing the height - Hence we can also do rotation to maintain the
property of heap! - Algorithm is a O(logh) algorithm!
39Q4