Computer Science Fundamentals (CS153 2003S)
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Experiments in Java]
[Scheme Reference]
[Scheme Report]
[CS153 2002S (Walker)]
[CS151 2003S (Rebelsky)]
[CS152 2000F (Rebelsky)]
[SamR]
Back to LInear Structures. On to The Dictionary ADT.
Held: Monday, 28 April 2003
Summary: Today we consider another linear structure, the priority queue, and some key implementations.
Related Pages:
Assignments:
Notes:
Overview:
add,
get,
peek, and
isEmpty.
get and peek return is
the highest-priority element
add an element, shove it at the front or the end,
whichever is easiest.
get the highest priority element? You step through
the list, looking at elements in turn.
lookssomething like a tree (okay, an upside-down tree or a unisex family tree). Because each node has two subtrees, we call it a binary tree.
public class BinaryTreeNode
{
// +--------+--------------------------------------------------
// | Fields |
// +--------+
/** The highest-priority element of the priority queue. */
private Object top;
/**
* Half of the remaining elements. Set to null if there are
* no other elements.
*/
private BinaryTreeNode left;
/**
* The other half of the remaining elements. Set to null
* if there are no other elements.
*/
private BinaryTreeNode right;
/**
* The comparator used to determine priorities.
*/
private Comparator prioritize;
// ...
} // BinaryTreeNode
2 2 2 2 2 2 3
/ \ / / \ / \ / \ / \
3 7 3 3 7 3 7 3 7 3 3
/ \ | / / \
9 7 8 9 9 7
2 2 2 2 2
/ \ / \ / \ /|\ |
3 7 9 7 7 3 3 3 3 3
/ / \ / \ / \ / \
9 8 8 9 7 9 7 4 7
holeat the top of the heap. We put the last element in the last level in that hole (which may sound like an odd idea, but at least it maintains the near-completeness property).
5
5
/
6
5 4
/ \ to / \
6 4 6 5
4 4
/ \ / \
6 5 to 4 5
/ /
4 6
4
/ \
4 5
/ \
6 7
4 4 2
/ \ / \ / \
4 5 to 4 2 to 4 4
/ \ | / \ | / \ |
6 7 2 6 7 5 6 7 5
2 ? 5 3 3
/ \ / \ / \ / \ / \
3 4 to 3 4 to 3 4 to 5 4 to 4 4
/ \ | / \ | / \ / \ / \
6 4 5 6 4 5 6 4 6 4 6 5
0
/ \
1 2
/ \ / \
3 4 5 6
/ \
7 8
nothing at this position.)
floor((i-1)/2).
double indirectionhere.
delete leastoperations. Each takes O(log2n) by our prior analysis.
for (int i = 1; i < stuff.length; ++i) {
percolateUp(stuff[i]);
} // for
Thursday, 15 January 2003 [Samuel A. Rebelsky]
Sunday, 27 April 2003 [Samuel A. Rebelsky]
Back to LInear Structures. On to The Dictionary ADT.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Experiments in Java]
[Scheme Reference]
[Scheme Report]
[CS153 2002S (Walker)]
[CS151 2003S (Rebelsky)]
[CS152 2000F (Rebelsky)]
[SamR]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Tue May 6 09:21:08 2003.
The source to the document was last modified on Sun Apr 27 23:27:36 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Outlines/outline.49.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby