Week 06 slides
Week 06 slides
Week 06 slides
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
COMP 311<br />
<strong>Week</strong> <strong>06</strong><br />
© 2010 Tim Kington, Todd Whittaker<br />
Agenda<br />
• This week’s expected outcomes<br />
• Guided Learning Activity solutions<br />
• Review Lab 1 solution<br />
• Introduce homework problems<br />
• Question and answer session
Agenda<br />
• This week’s expected outcomes<br />
• Guided Learning Activity solutions<br />
• Review Lab 1 solution<br />
• Introduce homework problems<br />
• Question and answer session<br />
Outcomes<br />
• Provide a recursive definition of a<br />
heap<br />
• Demonstrate heap insertion and<br />
removal algorithms
Outcomes<br />
• Apply the Adapter design pattern to<br />
implement a heap-based priority<br />
queue<br />
• Use a heap data structure to solve a<br />
given problem<br />
Learning Activities<br />
Guided Learning<br />
Activity Solutions for <strong>Week</strong> <strong>06</strong>
Learning Activities<br />
• Q: Show the heap that would be<br />
used to store the words “this,” “is,”<br />
“the,” “house,” “that,” “jack,” “built,”<br />
assuming they are inserting in that<br />
sequence.<br />
Learning Activities<br />
• Heap data structure<br />
• A special form of a complete binary<br />
tree in which the value of the root<br />
node is less than its children, and the<br />
children are themselves heaps.
Learning Activities<br />
• Heap data structure<br />
• A special form of a complete binary<br />
tree in which the value of the root<br />
node is less than its children, and the<br />
children are themselves heaps.<br />
A “min-heap.” A “maxheap”<br />
inverts the<br />
condition.<br />
Learning Activities<br />
2<br />
5 17<br />
8<br />
13<br />
30 18<br />
20 12 14 19 35
Learning Activities<br />
Root is less than<br />
its children.<br />
2<br />
5 17<br />
8<br />
13<br />
30 18<br />
20 12 14 19 35<br />
Learning Activities<br />
Root is less than<br />
its children.<br />
2<br />
5 17<br />
Left child is<br />
also a heap.<br />
8<br />
13<br />
30 18<br />
Right child is<br />
also a heap.<br />
20 12 14 19 35
Learning Activities<br />
private static class Node {<br />
T data;<br />
Node leftChild;<br />
Node rightChild;<br />
}<br />
// snip...<br />
Same kind of<br />
structure as a<br />
binary tree<br />
Learning Activities<br />
public boolean isHeap(Comparator cmp) {<br />
boolean leftResult = true;<br />
boolean rightResult = true;<br />
if (this.leftChild != null) {<br />
leftResult = cmp.compare(this.data,<br />
this.leftChild.data)
Learning Activities<br />
• Inserting elements<br />
• Step 1: place the element at the next<br />
available position in the tree<br />
• Step 2: Swap the element with its<br />
parent until either it becomes the root<br />
or it is less than its parent.<br />
Learning Activities<br />
• Insert 10 into this heap<br />
2<br />
5 17<br />
8<br />
13<br />
30 18<br />
20<br />
12<br />
14<br />
19<br />
35
Learning Activities<br />
• Insert 10 into this heap<br />
2<br />
5 17<br />
8<br />
13<br />
30 18<br />
20<br />
12<br />
14 19 35 10<br />
Learning Activities<br />
• Insert 10 into this heap<br />
2<br />
5 17<br />
8<br />
13<br />
10 18<br />
20<br />
12<br />
14 19 35 30
Learning Activities<br />
• Insert 10 into this heap<br />
2<br />
5 10<br />
8<br />
13<br />
17 18<br />
20<br />
12<br />
14 19 35 30<br />
Learning Activities<br />
• Heap insertion time complexity<br />
• How many times can a number<br />
“bubble up” one level?
Learning Activities<br />
• Removing elements<br />
• Step 1: Move the last element of the<br />
tree into the node to be removed.<br />
• Swap the node with its smallest child<br />
until you either reach the bottom of<br />
the tree or the node is smaller than its<br />
children.<br />
Learning Activities<br />
• Remove 2 from this heap<br />
2<br />
5 10<br />
8<br />
13<br />
17 18<br />
20<br />
12<br />
14 19 35 30
Learning Activities<br />
• Remove 2 from this heap<br />
30<br />
5 10<br />
8<br />
13<br />
17 18<br />
20<br />
12<br />
14<br />
19<br />
35<br />
Learning Activities<br />
• Remove 2 from this heap<br />
5<br />
30 10<br />
8<br />
13<br />
17 18<br />
20<br />
12<br />
14<br />
19<br />
35
Learning Activities<br />
• Remove 2 from this heap<br />
5<br />
8 10<br />
30<br />
13<br />
17 18<br />
20<br />
12<br />
14<br />
19<br />
35<br />
Learning Activities<br />
• Remove 2 from this heap<br />
5<br />
8 10<br />
12<br />
13<br />
17 18<br />
20<br />
30<br />
14<br />
19<br />
35
Learning Activities<br />
• Heap removal time complexity<br />
• How many times can a number<br />
“bubble down” one level?<br />
Learning Activities<br />
• Q: Show the heap that would be<br />
used to store the words “this,” “is,”<br />
“the,” “house,” “that,” “jack,” “built,”<br />
assuming they are inserting in that<br />
sequence.
Learning Activities<br />
• this is the house that jack built<br />
Learning Activities<br />
• this is the house that jack built<br />
this
Learning Activities<br />
• this is the house that jack built<br />
Learning Activities<br />
• this is the house that jack built
Learning Activities<br />
• this is the house that jack built<br />
Learning Activities<br />
• this is the house that jack built
Learning Activities<br />
• this is the house that jack built<br />
Learning Activities<br />
• this is the house that jack built
Learning Activities<br />
• this is the house that jack built<br />
Learning Activities<br />
• this is the house that jack built
Learning Activities<br />
• this is the house that jack built<br />
Learning Activities<br />
• this is the house that jack built
Learning Activities<br />
• this is the house that jack built<br />
Learning Activities<br />
• this is the house that jack built
Learning Activities<br />
• Q: Exchange the order of arrival of<br />
the first and last words and build the<br />
new heap.<br />
Learning Activities<br />
• built is the house that jack this
Learning Activities<br />
• Q: Give an algorithm to find all<br />
nodes less than some value X in a<br />
binary heap. Analyze its complexity.<br />
Learning Activities<br />
public int find(Comparator cmp, T target) {<br />
int result = 0;<br />
if (cmp.compare(this.data, target) < 0) {<br />
// in a minheap, no need to search left or<br />
// right children unless the root also matches.<br />
result = 1;<br />
if (this.leftChild != null) {<br />
result += leftChild.find(cmp, target);<br />
}<br />
if (this.rightChild != null) {<br />
result += rightChild.find(cmp, target);<br />
}<br />
}<br />
return result;<br />
}
Learning Activities<br />
public int find(Comparator cmp, T target) {<br />
int result = 0;<br />
if (cmp.compare(this.data, target) < 0) {<br />
// in a minheap, no need to search left or<br />
// right children unless the root also matches.<br />
result = 1;<br />
if (this.leftChild != null) {<br />
result += leftChild.find(cmp, target);<br />
}<br />
if (this.rightChild != null) {<br />
result += rightChild.find(cmp, target);<br />
}<br />
}<br />
Visits every node,<br />
return result;<br />
therefore O(n)<br />
}<br />
Learning Activities<br />
• Q: Explain and demonstrate how<br />
inserting the following numbers into<br />
a heap would work: 35, 20, 30, 50,<br />
45, 60, 18, 25
Learning Activities<br />
• A: 18<br />
25 20<br />
35<br />
45<br />
60 30<br />
50<br />
Learning Activities<br />
• Q: Explain and demonstrate how<br />
removing the number 18 from the<br />
heap would work.
Learning Activities<br />
• A: Swap 50 into 18’s place, and<br />
bubble down.<br />
Learning Activities<br />
• Q: Considering a binary heap, print<br />
the keys as encountered in a<br />
preorder transversal. Is the output<br />
sorted? Justify your answer.
Learning Activities<br />
• A: No. For the heap from the last<br />
question, a pre-order traversal gives<br />
20, 25, 35, 45, 30, 60, 50. In a<br />
heap, each node’s value is smaller<br />
than both of the values in its<br />
children, but there are no<br />
constraints on the relative values of<br />
the child nodes.<br />
Learning Activities<br />
• Q: Apply the Adapter design pattern<br />
to implement a heap-based priority<br />
queue. This activity will require you<br />
to implement a heap-based priority<br />
queue, utilizing the Adapter pattern.<br />
Your instructor will provide a<br />
skeleton Eclipse project and<br />
additional instructions.
Learning Activities<br />
• Note: a complete binary tree is<br />
easily kept in an array. Map array<br />
indices as follows:<br />
parentOf<br />
leftChildOf<br />
( i)<br />
(<br />
i<br />
)<br />
=<br />
=<br />
⎣<br />
( i −1) /<br />
2<br />
i<br />
+<br />
1<br />
2<br />
⎦<br />
rightChildOf<br />
( i)<br />
=<br />
2( i<br />
+ 1)<br />
Learning Activities<br />
HeapToQueue<br />
Adapter<br />
ArrayListHeap<br />
PriorityQueue
Learning Activities<br />
public void add(E element) {<br />
int childPos = list.size();<br />
In<br />
int parentPos = parent(childPos); “ArrayListHeap.java”<br />
list.add(childPos, element);<br />
while (childPos != 0 &&<br />
comparator.compare(list.get(parentPos),<br />
list.get(childPos)) > 0) {<br />
E temp = list.get(parentPos);<br />
list.set(parentPos, list.get(childPos));<br />
list.set(childPos, temp);<br />
childPos = parentPos;<br />
parentPos = parent(childPos);<br />
}<br />
}<br />
Learning Activities<br />
public void add(E element) {<br />
int childPos = list.size();<br />
In<br />
int parentPos = parent(childPos); “ArrayListHeap.java”<br />
list.add(childPos, element);<br />
while (childPos != 0 &&<br />
comparator.compare(list.get(parentPos),<br />
list.get(childPos)) > 0) {<br />
E temp = list.get(parentPos);<br />
list.set(parentPos, list.get(childPos));<br />
list.set(childPos, temp);<br />
childPos = parentPos;<br />
remove() left as<br />
parentPos = parent(childPos); an exercise for<br />
}<br />
the reader!<br />
}
Learning Activities<br />
• Q: Use a heap data structure to<br />
solve a given problem. Make use of<br />
your heap acting like a priority<br />
queue to simulate students waiting<br />
in line to ask their professor<br />
questions.<br />
Learning Activities<br />
• Each student can only ask a single<br />
question, and whenever the<br />
professor is done answering a<br />
question, she will address the next<br />
student waiting who has the<br />
shortest question. Your instructor<br />
will provide a skeleton Eclipse<br />
project and additional instructions.
Learning Activities<br />
public class Professor {<br />
private Queue answerQueue;<br />
private Queue arrivalQueue;<br />
private int currentTime;<br />
}<br />
// snip...<br />
Kept in order of<br />
question length<br />
Learning Activities<br />
public class Professor {<br />
private Queue answerQueue;<br />
private Queue arrivalQueue;<br />
private int currentTime;<br />
}<br />
// snip...<br />
Kept in order of<br />
Kept in order of<br />
arrival time.
Learning Activities<br />
public Professor() {<br />
this.answerQueue = new PriorityQueue(<br />
new Comparator() {<br />
public int compare(Student s1, Student s2) {<br />
return s1.getQuestionLength() –<br />
s2.getQuestionLength();<br />
}});<br />
this.arrivalQueue = new PriorityQueue(<br />
new Comparator() {<br />
public int compare(Student s1, Student s2) {<br />
return s1.getArrivalTime() –<br />
s2.getArrivalTime();<br />
}});<br />
this.currentTime = 0;<br />
}<br />
Learning Activities<br />
public void addStudent(Student student) {<br />
arrivalQueue.offer(student);<br />
}<br />
public void answerAllQuestions() {<br />
while (!arrivalQueue.empty() ||<br />
!answerQueue.empty()) {<br />
studentsArrive();<br />
answerOneQuestion();<br />
}<br />
}
Learning Activities<br />
protected void studentsArrive() {<br />
// put all arriving students into the answer queue<br />
Student arrivingStudent = arrivalQueue.peek();<br />
while (arrivingStudent != null &&<br />
arrivingStudent.getArrivalTime()
Learning Activities<br />
Answered Students ---------<br />
Student 1: arrives at 1, wanting 4 minutes, and leaves at 5 having waited 0 minutes.<br />
Student 2: arrives at 1, wanting 5 minutes, and leaves at 10 having waited 4 minutes.<br />
Student 3: arrives at 6, wanting 5 minutes, and leaves at 15 having waited 4 minutes.<br />
Student 4: arrives at 11, wanting 3 minutes, and leaves at 24 having waited 10 minutes.<br />
Student 5: arrives at 14, wanting 2 minutes, and leaves at 17 having waited 1 minutes.<br />
Student 6: arrives at 17, wanting 2 minutes, and leaves at 19 having waited 0 minutes.<br />
Student 7: arrives at 19, wanting 2 minutes, and leaves at 21 having waited 0 minutes.<br />
Student 8: arrives at 21, wanting 5 minutes, and leaves at 40 having waited 14 minutes.<br />
Student 9: arrives at 22, wanting 5 minutes, and leaves at 45 having waited 18 minutes.<br />
Student 10: arrives at 23, wanting 2 minutes, and leaves at 26 having waited 1 minutes.<br />
Student 11: arrives at 24, wanting 2 minutes, and leaves at 30 having waited 4 minutes.<br />
Student 12: arrives at 26, wanting 1 minutes, and leaves at 27 having waited 0 minutes.<br />
Student 13: arrives at 27, wanting 1 minutes, and leaves at 28 having waited 0 minutes.<br />
Student 14: arrives at 29, wanting 1 minutes, and leaves at 31 having waited 1 minutes.<br />
Student 15: arrives at 29, wanting 4 minutes, and leaves at 35 having waited 2 minutes.<br />
Total person-minutes of waiting: 59<br />
Huffman Trees<br />
• A way of compressing data<br />
• A simple coding uses the same<br />
number of bits for each character<br />
• Huffman codings use fewer bits for<br />
the most common characters
Huffman Trees<br />
Character<br />
Encoding<br />
e 0<br />
e<br />
0<br />
1<br />
t 11<br />
0 1<br />
s 100<br />
t<br />
r 101<br />
0<br />
1<br />
s<br />
r<br />
Huffman Trees<br />
11010011<br />
0<br />
1<br />
e<br />
0 1<br />
t<br />
0<br />
1<br />
s<br />
r
Huffman Trees<br />
11010011<br />
0<br />
1<br />
t<br />
e<br />
0 1<br />
t<br />
0<br />
1<br />
s<br />
r<br />
Huffman Trees<br />
11010011<br />
0<br />
1<br />
te<br />
e<br />
0 1<br />
t<br />
0<br />
1<br />
s<br />
r
Huffman Trees<br />
11010011<br />
0<br />
1<br />
tes<br />
e<br />
0 1<br />
t<br />
0<br />
1<br />
s<br />
r<br />
Huffman Trees<br />
11010011<br />
0<br />
1<br />
test<br />
e<br />
0 1<br />
t<br />
0<br />
1<br />
s<br />
r
Building Huffman Trees<br />
• Put each symbol in a min-heap<br />
ordered by frequency<br />
• While the queue is not empty<br />
• Remove two elements from the heap<br />
• Attach them to a new parent node<br />
• Place the parent node in the heap with<br />
the sum of the children's frequencies<br />
Building Huffman Trees<br />
e<br />
50<br />
t<br />
20<br />
s<br />
15<br />
r<br />
10
Building Huffman Trees<br />
e<br />
50<br />
25<br />
t<br />
20<br />
s<br />
15<br />
r<br />
10<br />
Building Huffman Trees<br />
e<br />
50<br />
45<br />
25<br />
t<br />
20<br />
s<br />
15<br />
r<br />
10
Building Huffman Trees<br />
Character<br />
Encoding<br />
e 0<br />
0<br />
1<br />
t 11<br />
e<br />
s 100<br />
r 101<br />
0<br />
1<br />
0 1<br />
t<br />
s<br />
r<br />
Homework Assignments<br />
• Due this week<br />
• Homework 4<br />
• Due next week<br />
• Lab 2
Homework Assignments<br />
• Problem 1<br />
• A d-ary<br />
heap is like a binary heap<br />
except that non-leaf nodes have d<br />
children instead of two. Explain how<br />
to implement a d-ary<br />
heap in an array.<br />
Comment on how this will affect the<br />
space and time complexity of the<br />
operations.<br />
Homework Assignments<br />
• Problem 2<br />
• Draw the binary heap that results from<br />
this series of inserts into an initially<br />
empty heap: Insert 10 12 1 14 6 5 8<br />
15. Show the result of performing two<br />
DeleteMin operations on the resulting<br />
heap. Show, separately, the result of<br />
each individual insert/delete.
Homework Assignments<br />
• Problem 3<br />
• What are the minimum and maximum<br />
numbers of elements in a heap of<br />
height h?<br />
Question and Answer Session
Lab 1 Solution