0% found this document useful (0 votes)
35 views2 pages

07 Handout 1

nonesss

Uploaded by

plgzxuan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views2 pages

07 Handout 1

nonesss

Uploaded by

plgzxuan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

IT1815

Heaps and Priority Queues or remove(). The dequeue operation starts with the
Heaps minimum element.
 A heap is a complete binary tree where the value of PriorityQueue class from the java.util package.
each of each parent node is either higher or lower Example:
than the value of its child nodes. PriorityQueue<Integer> printer = new
 These are the two (2) types of heap: PriorityQueue<>();
o Min Heap – The value of each parent node is  To enqueue, use add() or offer(). To dequeue, use
less than or equal to the values of its child poll()
nodes.
o Max Heap – The value of each parent node is
greater than or equal to the values of its child
nodes.

Min Heap Max Heap


 Heaps can be implemented in Java using ArrayList
from the
java.util
package.
Example:
ArrayList<Integer> heap = new ArrayList<>();
 To create a heap with initial values, use the
addAll() method of the Collections class (also from
the java.util package). Example (based on the given
min heap above): ArrayList<Integer> minHeap =
new ArrayList<>();
Collections.addAll(minHeap, 1, 7, 6, 8, 9);
Explanation: The root node is always at index 0.
Its child nodes are always at index 1 and index 2.
Indices 3, 4, 5, and 6 can store the child nodes of
those two (2) nodes and so on.
 To determine the root node, use the get()
method. Example:
System.out.print(minHeap.get(0));

Priority Queues
 A priority queue is a special type of queue where
elements are processed based on their order (natural
or custom).
 Priority queues can be implemented in Java
using the
 Sample code to dequeue in Java based on natural  A comparator is used to create a specific ordering for
order: PriorityQueue<Integer> printer = new a collection of objects.
PriorityQueue<>(); printer.add(9);  To create a comparator in Java, use the Comparator
printer.add( interface and its methods such as comparing(),
7); comparingInt(), and compare().
printer.add(
 Sample code to dequeue in Java based on a custom
1);
printer.add( order:
3); Comparator<String> comp =
Comparator.comparing(String::length);
while (!printer.isEmpty()) //:: indicates a method reference,
{ System.out.print(printer.remove() + " "); ContainingType::methodName PriorityQueue<String> prio =
} new PriorityQueue<>(comp); prio.add("cat");
Output: 1 3 7 9 prio.add("c");
 Sample code to dequeue in Python based on natural prio.add("cats");
while (!prio.isEmpty()) {
order: System.out.println(prio.remove());
from queue import }
PriorityQueue prio = References:
PriorityQueue() Koffman, E. & Wolfgang, P. (2016). Data structures: Abstraction and design using
prio.put("dog") Java.
prio.put("d") Hoboken: John Wiley & Sons, Inc.
Oracle Docs. (n.d.). Citing sources. Retrieved
prio.put("dogs")
from https://docs.oracle.com/javase/8/docs/api/java/util/package-
while not prio.empty(): summary.html
next_item = prio.get() Python Software Foundation. (n.d.). The Python tutorial. Retrieved
print(next_item) from https://docs.python.org/3/tutorial/index.html

07 Handout 1 *Property of STI


[email protected] Page 1 of 1

You might also like