Stacks queues introduction to trees
This presentation is the property of its rightful owner.
Sponsored Links
1 / 165

Stacks Queues Introduction to Trees PowerPoint PPT Presentation


  • 44 Views
  • Uploaded on
  • Presentation posted in: General

Lecture 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 last ‘rush’ item I gave you on hold; this is more important, so rush it out first.”

Download Presentation

Stacks Queues Introduction to Trees

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Stacks queues introduction to trees

Lecture 11

StacksQueuesIntroduction to Trees


Stacks

Stacks


An everyday example

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).


In general

LB

In general...

  • A stack can keep track of, “Where was I?”

    • Activation Stack

    • Compilers

      if

      if

      if

      endif

      endif

      if

  • Cafeterias use stacks: Plates, trays...


The stack

The Stack

Push

Pop


Properties

Properties

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


The stack as a logical data structure

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...


Stacks dynamic implementation

Stacks:Dynamic Implementation

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

top

4

17

42


Defining the node type

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 Node

endrecord // Node


Complex node definition

Complex Node Definition

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 Num

endrecord // Student_Rec

Node definesa Record

data isoftype Student_Rec

next isoftype Ptr toa Node

endrecord // Node


Application programmer interface

Application Programmer Interface

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


Stacks queues introduction to trees

Push

  • Create new node

  • Add it to the front

top

17

42


Stacks queues introduction to trees

Push

  • Create new node

  • Add it to the front

temp

top

42

4

17

?


Stacks queues introduction to trees

Push

  • Create new node

  • Add it to the front

temp

top

42

4

17


Stacks queues introduction to trees

Push

  • Create new node

  • Add it to the front

top

4

42

17


Stacks queues introduction to trees

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 <- temp

endprocedure // Push


Stacks queues introduction to trees

Pop

  • Capture the first value (to return)

  • Remove the first node (move top to next)

top

4

42

17


Stacks queues introduction to trees

Pop

  • Capture the first value (to return)

  • Remove the first node (move top to next)

top

4

42

value = 4

17


Stacks queues introduction to trees

Pop

  • Capture the first value (to return)

  • Remove the first node (move top to next)

top

4

42

value = 4

17


Stacks queues introduction to trees

Pop

  • Capture the first value (to return)

  • Remove the first node (move top to next)

top

42

17

value (4) is returned


Stacks queues introduction to trees

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

endif

endprocedure // Pop


Student s choice

Student’s Choice?

Do

Trace

Skip

Trace


Algorithm fragment

Algorithm Fragment

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.


Stacks queues introduction to trees

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.


Stacks queues introduction to trees

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK =

N =


Stacks queues introduction to trees

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK =

N =


Stacks queues introduction to trees

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK =

N =


Stacks queues introduction to trees

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

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 <- temp

endprocedure

temp =

OK =

N =


Stacks queues introduction to trees

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

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 <- temp

endprocedure

temp =

OK =

N =


Stacks queues introduction to trees

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

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 <- temp

endprocedure

temp =

OK =

N =


Stacks queues introduction to trees

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

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 <- temp

endprocedure

temp =

OK =

N =


Stacks queues introduction to trees

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

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 <- temp

endprocedure

temp =

OK =

N =


Stacks queues introduction to trees

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

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 <- temp

endprocedure

temp =

OK =

N =


Stacks queues introduction to trees

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

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 <- temp

endprocedure

temp =

OK =

N =


Stacks queues introduction to trees

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK =

N =


Stacks queues introduction to trees

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

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 <- temp

endprocedure

temp =

OK =

N =


Stacks queues introduction to trees

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

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 <- temp

endprocedure

temp =

OK =

N =


Stacks queues introduction to trees

2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

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 <- temp

endprocedure

temp =

OK =

N =


Stacks queues introduction to trees

2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

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 <- temp

endprocedure

temp =

OK =

N =


Stacks queues introduction to trees

2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

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 <- temp

endprocedure

temp =

OK =

N =


Stacks queues introduction to trees

2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

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 <- temp

endprocedure

temp =

OK =

N =


Stacks queues introduction to trees

2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK =

N =


Stacks queues introduction to trees

2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

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

endif

endprocedure

value =

result =

OK =

N =


Stacks queues introduction to trees

2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

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

endif

endprocedure

value =

result =

OK =

N =


Stacks queues introduction to trees

2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

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

endif

endprocedure

value =

result = T

OK =

N =


Stacks queues introduction to trees

2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

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

endif

endprocedure

value = 2

result = T

OK =

N =


Stacks queues introduction to trees

2

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

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

endif

endprocedure

value = 2

result = T

OK =

N =


Stacks queues introduction to trees

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

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

endif

endprocedure

value = 2

result = T

OK =

N =


Stacks queues introduction to trees

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK = T

N = 2


Stacks queues introduction to trees

42

2

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK = T

N = 2


Stacks queues introduction to trees

7

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK = T

N = 2


Stacks queues introduction to trees

42

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK = T

N = 7


Stacks queues introduction to trees

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK = T

N = 42


Stacks queues introduction to trees

top

.

top isoftype Ptr toa Node

OK isoftype Boolean

N isoftype Num

top <- NIL

Push(42, top)

Push(2, top)

Pop(N, top, OK)

if(OK) then

print(N)

endif

Push(7, top)

Pop(N, top, OK)

Pop(N, top, OK)

.

OK = T

N = 42


Summary stack

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)


Questions

Questions?


Queues

Queues


Some examples

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


The queue

The Queue

Dequeue

Enqueue


Properties of queues

Properties of Queues

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


The queue as a logical data structure

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...


Queues dynamic implementation

head

tail

17

42

4

Queues:Dynamic Implementation

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


Queue record definition

Queue Record Definition

Queue definesa record

head,

tail isoftype Ptr toa Node

endrecord // Queue


Defining the node type1

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 Node

endrecord // Node


Complex node definition1

Complex Node Definition

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 Num

endrecord // Student_Rec

Node definesa Record

data isoftype Student_Rec

next isoftype Ptr toa Node

endrecord // Node


Application programmer interface1

Application Programmer Interface

procedure 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)


Enqueue

Enqueue

  • Create a new node with data

  • Add it to the end

    • Update pointers as needed

head

tail

17

4


Enqueue1

Enqueue

  • Create a new node with data

  • Add it to the end

    • Update pointers as needed

temp

42

head

tail

17

4


Enqueue2

Enqueue

  • Create a new node with data

  • Add it to the end

    • Update pointers as needed

temp

42

head

tail

17

4


Enqueue3

Enqueue

  • Create a new node with data

  • Add it to the end

    • Update pointers as needed

tail

head

temp

42

17

4


Stacks queues introduction to trees

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

endif

endprocedure // Enqueue


Dequeue

Dequeue

  • Capture the first value (to return)

  • Remove the first node (move head to next)

head

tail

42

17

4


Dequeue1

Dequeue

  • Capture the first value (to return)

  • Remove the first node (move head to next)

    value = 4

head

tail

42

17

4


Dequeue2

Dequeue

  • Capture the first value (to return)

  • Remove the first node (move head to next)

    value = 4

head

tail

42

17

4


Dequeue3

Dequeue

  • Capture the first value (to return)

  • Remove the first node (move head to next)

    value (4) is returned

head

tail

42

17


Stacks queues introduction to trees

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

endif

endprocedure // Dequeue


Initializing the queue

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 <- NIL

endprocedure // QInit


Student s choice1

Student’s Choice

Do

Trace

Skip

Trace


Algorithm fragment1

Algorithm Fragment

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.


Stacks queues introduction to trees

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.


Stacks queues introduction to trees

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

head

tail


Stacks queues introduction to trees

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.


Stacks queues introduction to trees

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.


Stacks queues introduction to trees

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

procedure Qinit

(Q isoftype out Queue)

Q.head <- NIL

Q.tail <- NIL

endprocedure


Stacks queues introduction to trees

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

procedure Qinit

(Q isoftype out Queue)

Q.head <- NIL

Q.tail <- NIL

endprocedure


Stacks queues introduction to trees

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

procedure Qinit

(Q isoftype out Queue)

Q.head <- NIL

Q.tail <- NIL

endprocedure


Stacks queues introduction to trees

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.


Stacks queues introduction to trees

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

temp

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

temp

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

temp

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

temp

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

temp

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

temp

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

temp

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

temp

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

temp

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

OK

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.


Stacks queues introduction to trees

42

temp

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

temp

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

2

temp

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

2

temp

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

2

temp

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

2

temp

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

2

temp

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

2

temp

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

2

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.


Stacks queues introduction to trees

42

2

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

2

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

2

OK

OK

T

N

N

42

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

2

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

2

OK

OK

T

N

N

42

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

Peeking back

at calling program.


Stacks queues introduction to trees

42

2

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

2

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

42

2

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

2

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

2

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

2

OK

OK

N

N

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

2

OK

OK

T

N

N

42

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.


Stacks queues introduction to trees

2

OK

OK

T

N

N

42

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.


Stacks queues introduction to trees

2

temp

OK

OK

T

N

N

42

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

2

7

temp

OK

OK

T

N

N

42

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

2

7

temp

OK

OK

T

N

N

42

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

2

7

temp

OK

OK

T

N

N

42

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

2

7

temp

OK

OK

T

N

N

42

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

2

7

OK

OK

T

N

N

42

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.


Stacks queues introduction to trees

7

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.


Stacks queues introduction to trees

7

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.


Stacks queues introduction to trees

7

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

7

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

7

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

7

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

7

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

7

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

7

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

OK

OK

T

N

N

2

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

OK

OK

T

N

N

7

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.


Stacks queues introduction to trees

OK

OK

T

N

N

7

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

OK

OK

T

N

N

7

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

OK

OK

T

N

N

7

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

OK

OK

T

N

N

7

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.

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

endif

endprocedure


Stacks queues introduction to trees

OK

OK

F

N

N

7

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.


Stacks queues introduction to trees

myQ is empty

OK

OK

F

N

N

7

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.


Stacks queues introduction to trees

myQ is empty

OK

OK

F

N

N

7

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.


Stacks queues introduction to trees

myQ is empty

OK

OK

F

N

N

7

.

myQ isoftype Queue

OK isoftype Boolean

N 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

.


Summary queues

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)


Questions1

Questions?


Introduction to trees

Introduction to Trees


Trees

Trees

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


Visual representation of a tree

Visual Representation of a Tree

Trees allow each node to have multiple successors

Parent

Child


Tree terminology

Tree Terminology

Root

Parent

Child

Leaf


Tree terminology1

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.


Binary trees

Binary Trees

Binary trees can have at most two successors.


Binary tree example

Binary Tree Example

No imposed ordering.

25

42

7

12

3

105

111

17

68


Structure of a binary tree node

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

data

left_child

right_child


Example definition binary tree node

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_Node

endrecord // Tree_Node


Binary search trees

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

root

34

25

45

21

29

41

52


Can a tree be a bst and not a bst

LB

Can a Tree be a BST and not a BST?


Stacks queues introduction to trees

1100

9494

2102

882

904

7856

1234

Bob

Sal

Abe

Zak

Ned

Tom

Ava

LB


Node depth in a tree

Node Depth in a Tree

  • Measures how far down a node is in the tree.

  • How many “ancestor” nodes “live above” the node?

0

1

2

3


Balanced trees

Balanced Trees

Roughly symmetrical

  • There is a difference of a most one level of depth among the various leaves

Balanced

Not Balanced


Summary

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


Questions2

Questions?


  • Login