39
Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first- in-first-out (FIFO). Functions: createEmptyQueue() returns a newly created empty queue front(Q) returns the first node of Q dequeue(Q) returns and removes the first node of Q enqueue(Q, x) returns Q with x added as the last element isEmptyQueue(Q) returns true if Q is empty and false if it is not

Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Embed Size (px)

Citation preview

Page 1: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Queues

Queue Q = x0 x1 x2 x3 … xn-1 n = # elements

A queue is a list but the nodes are only accessed first-in-first-out (FIFO).

 

Functions:

createEmptyQueue() returns a newly created empty queue

front(Q) returns the first node of Q

dequeue(Q) returns and removes the first node of Q

enqueue(Q, x) returns Q with x added as the last element

isEmptyQueue(Q) returns true if Q is empty and false if it is not

Page 2: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Homework 3a

• Describe how to implement a queue using an array (assume it will never have more than 100 elements).

• Do the five queue functions.• Describe how to implement a queue using

an set of nodes (this queue will have no number of element limit).

• Do the five queue functions.

Page 3: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Queue – Array

Nam

eLast: Sm

artF

irstNam

e: JoeS

tudentNum

ber: 8S

SN: 123-34-1112G

rade: 95

Nam

eLast: Sm

artF

irstNam

e: JoeS

tudentNum

ber: 8S

SN: 123-34-1112G

rade: 95

0 321 654

int front = the front of the queue

int end = the end of the queue

Nam

eLast: Sm

artF

irstNam

e: JoeS

tudentNum

ber: 8S

SN: 123-34-1112G

rade: 95

Nam

eLast: Sm

artF

irstNam

e: JoeS

tudentNum

ber: 8S

SN: 123-34-1112G

rade: 95

in this case front = 1 end = 5

Page 4: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

createEmptyQueue()

declare an array of max size

array q[100]

int front = 0

int end = 0

Page 5: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

front(Q)

if isEmptyQueue(q)

return null

else

return q[front]

Page 6: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

dequeue(Q)

temp = front

front = (front + 1) modulo 100

return q[temp]

Page 7: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

enqueue(Q, x)

q[end] = x

end = (end + 1) modulo 100

Page 8: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

isEmptyQueue(Q)

if front = end

return true

else

return false

Page 9: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Queue – Linked-List

q

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

Page 10: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Queue – Linked-List

f

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

e

Page 11: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

createEmptyQueue()

Declare pointers to type node called f and e

f null

e null

Page 12: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

front(Q)

f

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

return node pointed to by f

e

Page 13: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

dequeue(Q)

f

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

e

Page 14: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

dequeue(Q)

f

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

temp = f

temp

e

Page 15: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

dequeue(Q)

f

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

f = f.next

temp

e

Page 16: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

dequeue(Q)

f

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

temp.next = nullreturn temp

temp

null

e

Page 17: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

enqueue(Q, x)single pointer

q

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

x

Page 18: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

enqueue(Q, x)- sp

q

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

x

temp

temp = qtemp = temp.nextuntil temp.next = null

Page 19: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

enqueue(Q, x)- sp

q

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

x

temp

Page 20: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

enqueue(Q, x)- sp

q

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

x

temp

temp.next = x

Page 21: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

enqueue(Q, x)

f

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

xe

Page 22: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

enqueue(Q, x)

f

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

xe

e.next = x

Page 23: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

enqueue(Q, x)

f

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

xe

e = x

Page 24: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

isEmptyQueue(Q)

return f == null

Page 25: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Linked List Searchhead

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

null

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123-34-1112

Grade: 95

Page 26: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Speed of List Search

• O(n) for when implemented as a linked-list

• O(lg n) possible if implemented as an array– How?

Page 27: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Speed of List Search

1

Page 28: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Speed of List Search

2

Page 29: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Speed of List Search

3

Page 30: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Speed of List Search

4

Page 31: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Speed of List Search

5

Page 32: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Speed of List Search -- Array

• It took 5 compares

• There were 29 elements

• lg(16) < lg(29) < lg(32)

• 4 < lg(29) < 5

• What if there were 1,000,000 elements?

• How many compares would it take?

Page 33: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Speed of List Search -- Array

• lg(1000000) = 20• In 20 compares we can search a list of 1,000,000

elements.• But we can’t always use an array to hold the data

because we may not know the size limit of the database.

• How can we dynamically represent the data in such a way that we can still get searches in lg(n)?

Page 34: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Trees

Page 35: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Trees

• Root

• Nodes

• Edges

• Leaves

• Height of a Tree

• Depth of a Node

• Parent / Child

Page 36: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Binary Tree

Page 37: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Binary Search Tree

54

30

93

25 7910

5 60

86

44

72

35

84

21

Page 38: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Binary Search Tree

Tree T is a binary search tree made up of n elements: x0 x1 x2 x3 … xn-1  Functions:createEmptyTree() returns a newly created empty binary treedelete(T, p) removes the node pointed to by p from the tree Tinsert(T, p) returns T with the node pointed to by p added in

the proper location search(T, key) returns a pointer to the node in T that has a key

that matched key returns null if key is not found traverse(T) prints the contents of T in orderisEmptyTree(T) returns true if T is empty and false if it is not

Page 39: Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()

Homework 4

• Describe how to implement a binary search tree using a set of nodes (this tree will have no number of element limit).

• Do the six BST functions.• Can you determine how to implement a

binary search tree using an array (assume it will never have more than 100 elements)?

• Consider the six BST functions.