review problems. what is the big o? i

24
Review Problems

Upload: terence-stafford

Post on 19-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Review Problems. What is the Big O? i

Review Problems

Page 2: Review Problems. What is the Big O? i

Review Problems

• What is the Big O?

i <- N

j <- 1

loop

exitif(i <= 0)

loop

exitif(j > M)

j <- j + 1

endloop

i < i - 1

endloop

Page 3: Review Problems. What is the Big O? i

Review problems

• Circle and Identify the 3 parts of recursion:

Function Fact returnsa Num(N iot in Num)

if(N = 0) then

Fact returns 1

else

Fact returns N * Fact(N - 1)

endif

endfunction // Fact

Page 4: Review Problems. What is the Big O? i

Review problems

• Circle and Identify the 3 parts of recursion:

Function Fact returnsa Num(N iot in Num)

if(N = 0) then

Fact returns 1

else

Fact returns N * Fact(N - 1)

endif

endfunction // Fact

Check for termination

Call self

Move onestep closer

Page 5: Review Problems. What is the Big O? i

Review Problems

• Recall that a leaf is a node in a binary tree with no children.

• Write a module that when passed a pointer to a binary tree will return the number of leaves.

• The module should use recursion

Page 6: Review Problems. What is the Big O? i

Leaves

Function Leaves returnsa Num

(current iot Ptr toa TNode)

if(current = NIL) then

Leaves returns 0

elseif(current^.left = NIL AND

current^.right = NIL) then

Leaves returns 1

else

Leaves returns Leaves(current^.right) +

Leaves(current^.left)

endif

endfunction // Leaves

Page 7: Review Problems. What is the Big O? i

Review Problems

• How many “time chunks” will be required to run this algorithm on 2 processors?

S1

S4

S2

S5

S8

S3

S6

S9

S7

I II III

Page 8: Review Problems. What is the Big O? i

Review Problems

S1

S4

S2

S5

S8

S3

S6

S9

S7

I II

Page 9: Review Problems. What is the Big O? i

Review Problems

• Write a module to convert an unsorted linked list to a sorted linked list.

• Use data structure conversion as opposed to a sort algorithm such as Bubble Sort or Merge Sort

Page 10: Review Problems. What is the Big O? i

Solution

• Recall that for this type problem you will typically need three modules:– A standard linked list AddInOrder module– A modified linked list traversal module

• modified to keep track of pointer to new list

– A startup module

Page 11: Review Problems. What is the Big O? i

Review problems

Algorithm Pain

a,b,c iot Char

a <- ‘b’

b <- ‘c’

c <- ‘a’

Agony(c,a,’b’)

print(a,c,b)

b <- funky(a,c)

print(a,b,c)

endalgorithm

Procedure Agony(a iot in/out Char, b iot out Char, c iot in Char)

t iot Charif(c = ‘c’) then

c <- ‘d’t <- bb <- aa <- t

elseb <- aa <- ‘b’

endifendprocedureFunction funky returnsa Char

(x,y isoftype in Char)if(x = y) then

funky returns ‘a’else

finky returns ‘b’endif

endfunction

Page 12: Review Problems. What is the Big O? i

Review problems

• Write a vector class• It should be generic and support (at least) the

following methods in the public section• AddToEnd• AddAt(nth)• Remove(nth)• Size• Get(nth)

Page 13: Review Problems. What is the Big O? i

class Vector(DT)

public

Procedure AddToEnd(din iot in DT)

// PPP

Procedure AddAt(nth iot in Num, din iot in DT)

// PPP

Procedure Remove(nth iot in Num)

// PPP

Function Size returnsa Num()

// PPP

Function Get returnsa DT(nth iot in Num)

// PPP

Procedure Initialize()

// PPP

Page 14: Review Problems. What is the Big O? i

protected

Node definesa record

data iot DT

next iot Ptr toa Node

endrecord

head isoftype Ptr toa Node

count isoftype Num

Procedure AddToEnd(din iot in DT)

AddToEndHelper(head, din)

endprocedure // AddToEnd

Page 15: Review Problems. What is the Big O? i

Procedure AddToEndHelper

(cur iot in/out Ptr toa Node, din iot in DT)

// PPP

if(cur = NIL) then

cur = new(Node)

cur^.data <- din

cur^.next <- NIL

count <- count + 1

else

AddToEndHelper(cur^.next, din)

endif

endprocedure // AddToEndHelper

Procedure AddAt(nth iot in Num, din iot in DT)

AddAtHelper(head, nth, din, 1)

endprocedure // AddAt

Page 16: Review Problems. What is the Big O? i

Procedure AddAtHelper(cur iot in/out Ptr toa Node,

nth iot in Num, din iot in DT,kount iot in Num)

// PPP

temp iot Ptr toa Node

if(cur = NIL OR nth = kount) then

temp <- new(Node)

temp^.data <- din

temp^.next <- cur

cur <- temp

count <- count + 1

else

AddAtHelper(cur^.next, nth, din, kount + 1)

endif

endprocedure // AddAtHelper

Page 17: Review Problems. What is the Big O? i

Procedure Remove(nth iot in Num)

RemoveHelper(head, nth, 1)

endprocedure // Remove

Procedure RemoveHelper(cur iot in/out Ptr toa Node,

nth iot in Num, kount iot in Num)

// PPP

if(cur <> NIL) then

if(nth = kount) then

cur <- cur^.next

count <- count - 1

else

RemoveHelper(cur^.next, nth, kount + 1)

endif

endif

endprocedure // RemoveHelper

Page 18: Review Problems. What is the Big O? i

Function Size returnsa Num() Size returns count endfunction // Size

Function Get returnsa DT(nth iot in Num) Get returns GetHelper(head, nth, kount) endfunction

Function GetHelper returnsa DT (cur iot in Ptr toa Node, nth, kount iot in Num) // Precon: User must not request item > Size ** // PP if(nth = kount) then GetHelper returns cur^.data else GetHelper returns GetHelper (cur^.next, nth, kount + 1) endif endfunction // GetHelper

Page 19: Review Problems. What is the Big O? i

Procedure Initialize()

head <- NIL

count <- 0

endprocedure // Initialize

endclass // Vector

Page 20: Review Problems. What is the Big O? i

Review problems• Use the generic Vector class you just wrote to write a

baseball roster program.• It should manage baseball player records consisting of

– Name– Position

• It should support the following operations– Add a player– Remove a player– Add a player at position N– Print a roster (only if there are 9 players otherwise print

an error message)• Assume that the record is named Player• Assume that you have modules called

– Procedure GetPlayer(data isoftype out Player)– Procedure PrintPlayer(data isoftype in Player)

Page 21: Review Problems. What is the Big O? i

Procedure Menu(Choice iot out Num)

print(“1-Add a player”)

print(“2-Remove a player”)

print(“3-Add a player at position N”)

print(“4-Print a roster”)

print(“5-Quit”)

read(Choice)

endprocedure // Menu

Player definesa record

Name iot String

Position iot String

endrecord // Player

Procedure GetPlayer(data isoftype out Player)

Procedure PrintPlayer(data isoftype in Player)

TEAMSIZE is 9

Page 22: Review Problems. What is the Big O? i

Algorithm Roster uses Vector(DT)

Team isoftype Vector(Player) // Make the Vector!!! Choice iot Num

Loop Menu(Choice) exitif(Choice = 5) if(Choice = 1) then Add(Team) elseif(Choice = 2) then Remove(Team) elseif(Choice = 3) then AddAt(Team) elseif(Choice = 4) then PrntRoster(Team) endifendalgorithm // Roster

Page 23: Review Problems. What is the Big O? i

Procedure Add(Team iot in/out Vector(Player)) temp iot Player GetPlayer(temp) Team.AddToEnd(temp)endprocedure // Add

procedure Remove(Team iot in/out Vector(Player)) i iot Num print(“Line number to remove?”) read(i)

Team.Remove(i)endprocedure // Remove

Procedure AddAt(Team iot in/out Vector(Player)) i iot Num temp iot Player print(“Enter at what line number”) GetPlayer(temp) Team.AddAt(i, temp)endprocedure // AddAt

Page 24: Review Problems. What is the Big O? i

Procedure PrntRoster(Team iot in/out Vector(Player)))i iot Num

if(Team.Size() <> TEAMSIZE) print(“Wrong size team”) else i < 1 Loop exitif(i > TEAMSIZE) PrintPlayer(Team.Get(i)) i <- i + 1 endloop endifendprocedure // PrntRoster