Priority Queues/Sorted: Difference between revisions
From charlesreid1
No edit summary |
(→Notes) |
||
| Line 14: | Line 14: | ||
The removal of the minimum item is then very easy - it is removed from the front of the list. Removal (and peek) is an O(1) operation. | The removal of the minimum item is then very easy - it is removed from the front of the list. Removal (and peek) is an O(1) operation. | ||
==Class== | |||
The class was organized as follows: | |||
* Class extended priority queue base class PriorityQueueBase<T> | |||
* Class accepted generic types <T> (in retrospect, this should have been <K,V>) | |||
* Class utilizes a protected Item<T> class in the base class for handling key-value pairs | |||
* Class stores key-value items in a linked list of Item<T> objects | |||
Utility methods: | |||
* toString | |||
* isEmpty | |||
* size | |||
Functional methods: | |||
* add - the add method needs to search for a location to put a new element, to prevent the list from getting out of order. This is an O(N) operation. | |||
* removeMin - this is an O(1) operation, remove from the front of the list. | |||
* peekMin - return the min without removing it | |||
==Flags== | ==Flags== | ||
Revision as of 06:04, 22 June 2017
Git Code
Link on git.charlesreid1.com: https://charlesreid1.com:3000/cs/java/src/master/priority-queues/SortedPriorityQueue.java
Notes
Sorted priority queue class implements a sorted list to keep track of items in the priority queue.
This class implements insertion sort for each add operation to keep the minimum at the front of the list.
This means add() is O(N).
The removal of the minimum item is then very easy - it is removed from the front of the list. Removal (and peek) is an O(1) operation.
Class
The class was organized as follows:
- Class extended priority queue base class PriorityQueueBase<T>
- Class accepted generic types <T> (in retrospect, this should have been <K,V>)
- Class utilizes a protected Item<T> class in the base class for handling key-value pairs
- Class stores key-value items in a linked list of Item<T> objects
Utility methods:
- toString
- isEmpty
- size
Functional methods:
- add - the add method needs to search for a location to put a new element, to prevent the list from getting out of order. This is an O(N) operation.
- removeMin - this is an O(1) operation, remove from the front of the list.
- peekMin - return the min without removing it
Flags
| Stacks and Queues Part of Computer Science Notes
Series on Data Structures
Stacks and Queues: Python StacksQueues/Python · StacksQueues/Python/ArrayStack · StacksQueues/Python/ArrayQueue · StacksQueues/Python/ArrayDeque StacksQueues/Python/LinkedStack
Stacks and Queues: Java StacksQueues/Java · StacksQueues/Java/ArrayStack · StacksQueues/Java/ArrayQueue · StacksQueues/Java/ArrayQueueFS · StacksQueues/Java/ArrayDeque StacksQueues/Java/LinkedStack · StacksQueues/Java/LinkedQueue · StacksQueues/Java/LinkedDeque
Applications Postfix_Expressions#Stacks · StacksQueues/Subsets · StacksQueues/Subsets/Java
|