Deque Implementations and Priority Queues, Study notes of Data Structures and Algorithms

Different implementations of a double ended queue (deque) using python lists and linked lists, as well as the concept of a priority queue. Code snippets and discusses the time complexity of various operations.

Typology: Study notes

2012/2013

Uploaded on 04/30/2013

jut
jut 🇮🇳

4.5

(63)

77 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
A Deque (pronounced “Deck”) is a linear data structure which behaves like a double-ended queue, i.e., it allows adding
or removing items from either the front or the rear of the Deque.
Deque
frontrear
removeFront( )
removeRear
addFront(newItem)addRear(newItem)
( )
1. One possible implementation of a Deque would be to use a Python list to store the Deque items such that
the rear item is always stored at index 0,
the front item is always stored at the highest index (or -1)
'a' 'b' 'c' 'd'
0 1 2 3
items:
Deque Object
List Object
(rear) (front)
class Deque(object):
def __init__(self):
a) Complete the __init__ method and determine the big-oh, O ( ), for each Deque operation, assuming the above
implementation. Let n be the number of items in the Deque.
size
removeRear
addRear
removeFront
addFront
isEmpty
b) Write the methods for the addRear and removeRear operation.
def
Rea
r(
def
r(
self,
newItem):
2. An alternative implementation of a Deque would be a linked implementation as in:
LinkedDeque Object
_front:
_rear:
_size: 4
data next data next data next data next
'a' 'b' 'c' 'd'
class LinkedDeque(object):
def __init__(self):
a) Complete the __init__ method and determine the big-oh, O ( ), for each Deque operation assuming the above
linked implementation. Let n be the number of items in the Deque.
sizeremoveRearaddRearremoveFrontaddFrontisEmpty
b) Suggest an improvement to the above linked implementation of the Deque to speed up some of its operations.
Data Structures (CS 1520) Lecture 6 Name:_________________
Lecture 6 - Page 1
Docsity.com
pf2

Partial preview of the text

Download Deque Implementations and Priority Queues and more Study notes Data Structures and Algorithms in PDF only on Docsity!

A Deque (pronounced “Deck”) is a linear data structure which behaves like a double-ended queue, i.e., it allows adding or removing items from either the front or the rear of the Deque.

rear^ Deque^ front

removeRear removeFront( )

addRear(newItem) addFront(newItem)

  1. One possible implementation of a Deque would be to use a Python list to store the Deque items such that  the rear item is always stored at index 0 ,  the front item is always stored at the highest index (or -1)

'a' 'b' 'c' 'd'

items:

Deque Object List Object

(rear) (front)

class Deque(object): def init(self):

a) Complete the init method and determine the big-oh, O ( ), for each Deque operation, assuming the above implementation. Let n be the number of items in the Deque. isEmpty addFront removeFront addRear removeRear size

b) Write the methods for the addRear and removeRear operation.

def addRear(self, newItem): def removeRear(self):

  1. An alternative implementation of a Deque would be a linked implementation as in:

LinkedDeque Object

_front:

_rear:

_size: (^4)

data next data next data next data next 'a' 'b' 'c' 'd'

class LinkedDeque(object): def init(self):

a) Complete the init method and determine the big-oh, O ( ), for each Deque operation assuming the above linked implementation. Let n be the number of items in the Deque. isEmpty addFront removeFront addRear removeRear size

b) Suggest an improvement to the above linked implementation of the Deque to speed up some of its operations.

Data Structures (CS 1520) Lecture 6 Name:_________________

Lecture 6 - Page 1Docsity.com

from node import Node class Node2Way(Node): def init(self,initdata): Node.init(self, initdata) self.previous = None def getPrevious(self): return self.previous def setPrevious(self,newprevious): self.previous = newprevious

  1. An alternative implementation of a Deque would be a doubly-linked implementation as in:

DoublyLinkedDeque Object

_front:

_rear:

_size: (^4)

previous data next previous data next previous data next previous data next 'a' 'b' 'c' 'd'

a) Determine the big-oh, O ( ), for each Deque operation assuming the above doubly-linked implementation. Let n be the number of items in the Deque. isEmpty addFront removeFront addRear removeRear size

  1. A priority queue has the same operations as a regular queue, except the items are NOT returned in the FIFO (first-in, first-out) order. Instead, each item has a proirity that determines the order they are removed. A hospital emergence room operates like a priority queue -- the person with the most serious injure has highest priority even if they just arrived.

a) Suppose that we have a priority queue with integer priorities such that the smallest integer corresponds to the highest priority. For the following priority queue, which item would be dequeued next?

priority queue:

b) To implement a priority queue, we could use an unorder Python list. If we did, what would be the big-oh notation for each of the following methods: (justify your answer)

 enqueue:

 dequeue:

c) To implement a priority queue, we could use a Python list order by priorities in decending order. If we did, what would be the big-oh notation for each of the following methods: (justify your answer)

 enqueue:

 dequeue

Data Structures (CS 1520) Lecture 6 Name:_________________

Lecture 6 - Page 2Docsity.com