stacks queues introduction to trees. stacks an everyday example your boss keeps bringing you...

165
Stacks Queues Introduction to Trees

Upload: kelley-garrison

Post on 18-Jan-2018

214 views

Category:

Documents


0 download

DESCRIPTION

An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’ item I gave you on hold; this is more important, so rush it out first.” We’ll end up doing the last item first (last in, first out).

TRANSCRIPT

Page 1: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

StacksQueues

Introduction to Trees

Page 2: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Stacks

Page 3: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

An Everyday Example

• Your boss keeps bringing you important items to deal with and keeps saying:

“Put that last ‘rush’ item I gave you on hold; this is more important, so rush it out first.”

We’ll end up doing the last item first(last in, first out).

Page 4: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

In general...

• A stack can keep track of, “Where was I?”– Activation Stack– Compilers

ifif

if

endifendif

if

• Cafeterias use stacks: Plates, trays...

LB

Page 5: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

The Stack

Push Pop

Page 6: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Idea: a “Last In, First Out” (LIFO) data structure

Behaviors:• Push: Add to top of stack• Pop: Remove from top of stack (and return that

top value)• Top: Return topmost item (but leave it on the

stack)• Is_Full: is it full?• Is_Empty: is it empty?• Initialize: empty stack

Properties

Page 7: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

The Stack as a Logical Data Structure

• The stack is an idea• It implies a set of logical behaviors• It can be implemented various ways– Using a linked list or a tree or an array

• In this example, we’ll focus on dynamic implementations using dynamic data...

Page 8: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Stacks:Dynamic Implementation

A linked list with restricted set of operations to change its state: only modified from one end

4

17

42

top

Page 9: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Defining the Node Type

This is the simple data structure we will use in the following example.

Node definesa record data isoftype Num next isoftype Ptr toa Nodeendrecord // Node

Page 10: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Recall that the same code (with some small modifications) will work with more complex data structures such as is shown here:

Student_Rec definesa Record Name isoftype String SSN isoftype String GPA isoftype Numendrecord // Student_Rec

Node definesa Record data isoftype Student_Rec next isoftype Ptr toa Nodeendrecord // Node

Complex Node Definition

Page 11: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node)// Purpose: push one value onto stack // Pre: top points to NIL-terminated list// Post: the list has one node added

procedure Pop (value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean)// Pop a value off stack; if empty, result// contains FALSE (value will be undefined)// Pre: top points to a NIL-terminated list// Post: list has one fewer, value is old data

Application Programmer Interface

Page 12: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Push

• Create new node• Add it to the front

17

42

top

Page 13: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Push

• Create new node• Add it to the front

42

17

top

4

temp

?

Page 14: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Push

• Create new node• Add it to the front

42

17

top

4

temp

Page 15: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Push

• Create new node• Add it to the front

4

42

17

top

Page 16: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Push

Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) // Push one value onto stack

temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- tempendprocedure // Push

Page 17: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Pop

• Capture the first value (to return)• Remove the first node (move top to next)

4

42

17

top

Page 18: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Pop

• Capture the first value (to return)• Remove the first node (move top to next)

4

42

17

top

value = 4

Page 19: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Pop

• Capture the first value (to return)• Remove the first node (move top to next)

4

42

17

top

value = 4

Page 20: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Pop

• Capture the first value (to return)• Remove the first node (move top to next)

42

17

top

value (4) is returned

Page 21: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Pop

procedure Pop (value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) // Pop an element off the stack

if(top = NIL) then result <- FALSE else result <- TRUE value <- top^.data top <- top^.next endifendprocedure // Pop

Page 22: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Student’s Choice?

DoTrace

SkipTrace

Page 23: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Algorithm Fragment.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

Page 24: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

Page 25: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

OK =N =

Page 26: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

top.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

OK =N =

Page 27: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

OK =N =

Page 28: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

topProcedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- tempendprocedure temp =

OK =N =

Page 29: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

topProcedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- tempendprocedure

OK =N =

temp =

Page 30: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

topProcedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- tempendprocedure

OK =N =

temp =

Page 31: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

topProcedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- tempendprocedure

42

OK =N =

temp =

Page 32: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

topProcedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- tempendprocedure

42

OK =N =

temp =

Page 33: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

topProcedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- tempendprocedure

42

OK =N =

temp =

Page 34: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

topProcedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- tempendprocedure

42

OK =N =

temp =

Page 35: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

42

OK =N =

Page 36: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

42

Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- tempendprocedure

OK =N =

temp =

Page 37: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

42

Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- tempendprocedure

OK =N =

temp =

Page 38: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

42

Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- tempendprocedure

2

OK =N =

temp =

Page 39: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

42

Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- tempendprocedure

2

OK =N =

temp =

Page 40: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

42

Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- tempendprocedure

2

OK =N =

temp =

Page 41: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

42

Procedure Push (value isoftype in Num, top isoftype in/out Ptr toa Node) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- top top <- tempendprocedure

2

OK =N =

temp =

Page 42: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

42

2

OK =N =

Page 43: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

42

2

OK =N =

Procedure Pop value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) if(top = NIL) then result <- FALSE else result <- TRUE value <- top^.data top <- top^.next endifendprocedure

value =result =

Page 44: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

42

2

OK =N =

Procedure Pop value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) if(top = NIL) then result <- FALSE else result <- TRUE value <- top^.data top <- top^.next endifendprocedure

value =result =

Page 45: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

42

2

OK =N =

Procedure Pop value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) if(top = NIL) then result <- FALSE else result <- TRUE value <- top^.data top <- top^.next endifendprocedure

value =result = T

Page 46: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

42

2

OK =N =

Procedure Pop value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) if(top = NIL) then result <- FALSE else result <- TRUE value <- top^.data top <- top^.next endifendprocedure

value = 2result = T

Page 47: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

42

2

OK =N =

Procedure Pop value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) if(top = NIL) then result <- FALSE else result <- TRUE value <- top^.data top <- top^.next endifendprocedure

value = 2result = T

Page 48: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

42

OK =N =

Procedure Pop value isoftype out Num, top isoftype in/out Ptr toa Node, result isoftype out Boolean) if(top = NIL) then result <- FALSE else result <- TRUE value <- top^.data top <- top^.next endifendprocedure

value = 2result = T

Page 49: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

42

OK = TN = 2

Page 50: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

42

OK = TN = 2

2

Page 51: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

42

OK = TN = 2

7

Page 52: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

42

OK = TN = 7

Page 53: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

OK = TN = 42

Page 54: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.top isoftype Ptr toa NodeOK isoftype BooleanN isoftype Numtop <- NILPush(42, top)Push(2, top)Pop(N, top, OK)if(OK) then print(N)endifPush(7, top)Pop(N, top, OK)Pop(N, top, OK).

top

OK = TN = 42

Page 55: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Summary: Stack

• Allow us to model “last-in, first-out” (LIFO) behavior

• Can be implemented using different data types

• Behavior is important (and defines a Stack)– Push to the front– Pop from the front– (from the same end)

Page 56: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Questions?

Page 57: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Queues

Page 58: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Some Examples

• Waiting in line– At the grocery store– At the movies– Printer queue

• Ordering items– Bills to pay–Making pizzas

• We can use a queue to model each of these.

LB

Page 59: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

The Queue

Enqueue

Dequeue

Page 60: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Idea: a “First In, First Out” (FIFO) data structure

Behaviors:• Enqueue: Add to end of queue• Dequeue: Remove from front of queue (and

return that front value)• Front: Return front-most item (but leave it in the

queue)• Is_Full: is it full?• Is_Empty: is it empty?• Initialize: empty queue

Properties of Queues

Page 61: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

The Queue as a Logical Data Structure

• The queue is an idea• It implies a set of logical behaviors• It can be implemented various ways– Using a linked list or a tree or an array

• In this example, we’ll focus on dynamic implementations using dynamic data...

Page 62: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Queues:Dynamic Implementation

A linked list with restricted set of operations to change its state: only modified by adding to one end and deleting from the other.

4 17 42

tailhead

Page 63: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Queue Record Definition

Queue definesa record head, tail isoftype Ptr toa Nodeendrecord // Queue

Page 64: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Defining the Node Type

This is the simple data structure we will use in the following example.

Node definesa record data isoftype Num next isoftype Ptr toa Nodeendrecord // Node

Page 65: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Recall that the same code (with some small modifications) will work with more complex data structures such as is shown here:

Student_Rec definesa Record Name isoftype String SSN isoftype String GPA isoftype Numendrecord // Student_Rec

Node definesa Record data isoftype Student_Rec next isoftype Ptr toa Nodeendrecord // Node

Complex Node Definition

Page 66: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Application Programmer Interfaceprocedure Enqueue (value isoftype in Num, Q isoftype in/out Queue)// Pre: Q is initialized// Purpose: Add a value to the tail// Post: Q has new element at tail

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean)// Pre: Q is initialized// Purpose: Remove first value from Q and// return it via ‘value’ OR indicate// that Q is empty via ‘result’ // Post: element that was at head has been// removed OR (result = FALSE)

Page 67: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Enqueue

• Create a new node with data• Add it to the end– Update pointers as needed

4 17

tailhead

Page 68: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Enqueue

• Create a new node with data• Add it to the end– Update pointers as needed

4 17

tailhead42

temp

Page 69: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Enqueue

• Create a new node with data• Add it to the end– Update pointers as needed

4 17

tailhead42

temp

Page 70: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Enqueue

• Create a new node with data• Add it to the end– Update pointers as needed

4 17

tailhead

42

temp

Page 71: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then // was empty Q.tail <- temp Q.head <- temp else // was not empty Q.tail^.next <- temp Q.tail <- temp endifendprocedure // Enqueue

Page 72: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Dequeue

• Capture the first value (to return)• Remove the first node (move head to next)

4 17

head

42

tail

Page 73: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Dequeue

• Capture the first value (to return)• Remove the first node (move head to next)

value = 4

4 17

head

42

tail

Page 74: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Dequeue

• Capture the first value (to return)• Remove the first node (move head to next)

value = 4

4 17

head

42

tail

Page 75: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Dequeue

• Capture the first value (to return)• Remove the first node (move head to next)

value (4) is returned

17

head

42

tail

Page 76: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

procedure Dequeue (value iot out Num, Q iot in/out Queue, result iot out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure // Dequeue

Page 77: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Initializing the Queue

procedure QInit (Q isoftype out Queue)// Initializes the Q head and tail// to point to nil, setting the Q to// be empty Q.head <- NIL Q.tail <- NILendprocedure // QInit

Page 78: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Student’s Choice

DoTrace

SkipTrace

Page 79: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Algorithm Fragment.myQ isoftype QueueOK isoftype BooleanN isoftype Num

Qinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

Page 80: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

Page 81: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

headtail

Page 82: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif. OK N

Page 83: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif. OK N

Page 84: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif. OK N

procedure Qinit (Q isoftype out Queue) Q.head <- NIL Q.tail <- NILendprocedure

Page 85: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif. OK N

procedure Qinit (Q isoftype out Queue) Q.head <- NIL Q.tail <- NILendprocedure

Page 86: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif. OK N

procedure Qinit (Q isoftype out Queue) Q.head <- NIL Q.tail <- NILendprocedure

Page 87: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif. OK N

Page 88: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif. OK N

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure

Page 89: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

Page 90: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

Page 91: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

Page 92: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

42

Page 93: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

42

Page 94: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

42

Page 95: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

42

Page 96: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

42

Page 97: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

42

Page 98: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

42

Page 99: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

42

OK N

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

Page 100: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

42

OK N

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

Page 101: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

42

OK N

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

2

Page 102: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

42

OK N

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

2

Page 103: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

42

OK N

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

2

Page 104: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

42

OK N

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

2

Page 105: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

42

OK N

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

2

Page 106: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

42

OK N

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

2

Page 107: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

42

OK N

2

Page 108: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

42

OK N

2

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 109: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

42

OK N

2

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 110: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

42

2

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif. OK N OK T N 42

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 111: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

42

OK N

2

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 112: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

42

OK T N 42

2

Peeking backat calling program.

Page 113: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

42

OK N

2

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 114: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

42

OK N

2

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 115: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N

42

OK N

2

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 116: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK N

2

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 117: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK N

2

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 118: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK N

2

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 119: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 42

2

Page 120: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 42

2

Page 121: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 42

2

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

Page 122: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 42

2

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

7

Page 123: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 42

2

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

7

Page 124: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 42

2

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

7

Page 125: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 42

2

procedure Enqueue (value isoftype in Num, Q isoftype in/out Queue) temp isoftype Ptr toa Node temp <- new(Node) temp^.data <- value temp^.next <- NIL if(Q.tail = NIL) then Q.tail <- temp Q.head <- temp else Q.tail^.next <- temp Q.tail <- temp endifendprocedure temp

7

Page 126: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 42

2

7

Page 127: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 2

7

Page 128: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 2

7

Page 129: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 2

7

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 130: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 2

7

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 131: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 2

7

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 132: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 2

7

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 133: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 2

7

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 134: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 2

7

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 135: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 2

7

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 136: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 2

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 137: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 2

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 138: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 7

Page 139: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 7

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 140: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 7

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 141: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 7

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 142: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK N OK T N 7

procedure Dequeue (value isoftype out Num, Q isoftype in/out Queue, result isoftype out Boolean) if(Q.head = NIL) then result <- FALSE else result <- TRUE value <- Q.head^.data Q.head <- Q.head^.next if(Q.head = NIL) then Q.tail <- NIL endif endifendprocedure

Page 143: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK F N OK N 7

Page 144: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK F N OK N 7

myQ is empty

Page 145: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK F N OK N 7

myQ is empty

Page 146: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

.myQ isoftype QueueOK isoftype BooleanN isoftype NumQinit(myQ)Enqueue(42, myQ)Enqueue(2, myQ)Dequeue(N, myQ, OK)Enqueue(7, myQ)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)Dequeue(N, myQ, OK)if(NOT OK) then print(“myQ is empty”)endif.

OK F N OK N 7

myQ is empty

Page 147: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Summary: Queues

• Allow us to model “first-in, first-out” (FIFO) behavior

• Can be implemented using different data types

• Behavior is important (and defines a Queue)– Enqueue to end– Dequeue from front– (or vice-versa – just do at opposite ends)

Page 148: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Questions?

Page 149: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Introduction to Trees

Page 150: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Trees

A non-linear, hierarchical collection with a “one to many” relationships

Page 151: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Visual Representation of a Tree

Trees allow each node to have multiple successors

Parent

Child

Page 152: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Tree Terminology

Parent

Child

Root

Leaf

Page 153: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Tree Terminology

• The first node is called the root.

• Successors are called children

• A parent node points to a child node.

• Nodes with no children are called leaves.

Page 154: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Binary Trees

Binary trees can have at most two successors.

Page 155: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Binary Tree ExampleNo imposed ordering.

25

42 7

105 111312

6817

Page 156: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Structure of a Binary Tree Node

<Type Name> definesa Record data isoftype <type> left_child isoftype Ptr toa <Type Name> right_child isoftype Ptr toa <Type Name>endrecord

left_child right_child

data

Page 157: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Example Definition: Binary Tree Node

Tree_Node definesa Record data isoftype Num left_child isoftype Ptr toa Tree_Node right_child isoftype Ptr toa Tree_Nodeendrecord // Tree_Node

Page 158: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

root 34

25

21 29

45

41 52

Binary Search Trees

For each node, the value stored in it is greater than the value in every node in the left sub-tree and less than the value in every node in the right sub-tree

Page 159: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Can a Tree be a BST and not a BST?

LB

Page 160: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

1100Bob

9494Sal

2102Abe

882Zak

904Ned

7856Tom

1234Ava

LB

Page 161: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Node Depth in a Tree• Measures how far down a node is in the tree.• How many “ancestor” nodes “live above” the

node?

0123

Page 162: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Balanced TreesRoughly symmetrical• There is a difference of a most one level of depth

among the various leaves

Balanced Not Balanced

Page 163: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Summary• Trees allow us to create nonlinear collections

• Binary Trees have at most two successors (children)

• Binary Search Trees impose structure– Every node’s value is greater than all the

values in its left sub-tree– Every node’s value is less than all the

values in its right sub-tree

Page 164: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that

Questions?

Page 165: Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that