{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

2c03-review - 00007 - PUSH(RIGHT-SIBLING(v,T,S...

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

View Full Document Right Arrow Icon
1 SOLUTIONS TO THE MIDTERM TEST 1.[7] function PREVIOUS(p, L) : position; var q : position; begin q := FIRST(L); while q END(L) { or q nil } do if NEXT(q,L) = p then return(q) else q:=NEXT(q); write(“invalid position”) end; More informal description is English is also O.K., if similar to the idea described above. 2.[20] a.[15] procedure DFS( T : tree ); var v : node; S : STACK of nodes; begin v := ROOT(T); MAKENULL(S); PUSH(v,S); while not EMPTY(S) do begin v := TOP(S); visit v {or print v}; POP(S);
Image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: PUSH(RIGHT-SIBLING(v,T),S); PUSH(LEFTMOST-CHILD(v,T),S) end end The description can be less formal. But the operations LEFTMOST-CHILD, RIGHT-SIBLING, TOP, POP, PUSH must be explicitly used. b.[5] T(n) = O(n), in the worst case the entire tree must be searched. 3.[10] Run Knuth-Morris-Pratt with pattern=B and text=AA. Complexity of KMP is O(length(text)+length(pattern)), in this case lenght(A)=length(B)=n, so the complexity is O(2n+n) = O(3n) = O(n)....
View Full Document

{[ snackBarMessage ]}