2c03-review - 00014

2c03-review - 00014 - pop(6 pop(2 push(3 push(7 pop(7 pop(3...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
4 BFS means layer by layer starting from the top, i.e. (layer 1) 1 (layer 2) 2 3 (layer 3) 4 5 6 7 DFS means going to leaf starting from the left and than going back to parents and then to unvisited children, in general going around the tree anticlockwise, i.e. 1,2,4,5,3,7 b. FIFO queue for BFS, print the first element from the queue, delete it and insert its sons to the queue in the order from left to right, i.e. 1 => delete 1, insert 2,3 => 2,3 => delete 2, insert 4,5,6 => 3,4,5,6 => delete 3, insert 7 =>delete 3,4,5,6,7 Stack for DFS, to remember the path to know here to return. We print when push is executed. For the example above we have: push(1), push(2), push(4), pop(4), push(5), pop(5), push(6),
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: pop(6), pop(2), push(3), push(7), pop(7), pop(3), pop(1). 5.[10] solution 1 procedure EUCLID(x,y:int;var gcd:int) var z:int; S: STACK of record m,n,z,l:int enf; l:2; begin MAKENULL(S); 1: if x=0 or y=0 then gcd:=0 else begin z:=x mod y; if z=0 then gcd:=y else begin PUSH((x,y,z,2),S); go to 1 end end 2: if not EMPTY(S) then begin (x,y,z,l):=TOP(S); POP(S) end end solution 2: procedure EUCLID (x,y:int; var gcd:int); label 0,1; var z: integer S: stack of record end; begin MAKENULL(S); 0: if x=0 or y=0 then gcd:=0...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online