11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 4.7 Projects 1414.16 Re-implement function fibr from Exercise 2.11, using a stack to replacethe recursive call as described in Section 4.2.4.4.17 Write a recursive algorithm to compute the value of the recurrence rel<strong>at</strong>ionT(n) = T(⌈n/2⌉) + T(⌊n/2⌋) + n; T(1) = 1.Then, rewrite your algorithm to simul<strong>at</strong>e the recursive calls with a stack.4.18 Let Q be a non-empty queue, <strong>and</strong> let S be an empty stack. Using only thestack <strong>and</strong> queue ADT functions <strong>and</strong> a single element variable X, write analgorithm to reverse the order of the elements in Q.4.19 A common problem for compilers <strong>and</strong> text editors is to determine if theparentheses (or other brackets) in a string are balanced <strong>and</strong> properly nested.For example, the string “((())())()” contains properly nested pairs of parentheses,but the string “)()(” does not, <strong>and</strong> the string “())” does not containproperly m<strong>at</strong>ching parentheses.(a) Give an algorithm th<strong>at</strong> returns true if a string contains properly nested<strong>and</strong> balanced parentheses, <strong>and</strong> false otherwise. Use a stack to keeptrack of the number of left parentheses seen so far. Hint: At no timewhile scanning a legal string from left to right will you have encounteredmore right parentheses than left parentheses.(b) Give an algorithm th<strong>at</strong> returns the position in the string of the first offendingparenthesis if the string is not properly nested <strong>and</strong> balanced.Th<strong>at</strong> is, if an excess right parenthesis is found, return its position; ifthere are too many left parentheses, return the position of the first excessleft parenthesis. Return −1 if the string is properly balanced <strong>and</strong>nested. Use a stack to keep track of the number <strong>and</strong> positions of leftparentheses seen so far.4.20 Imagine th<strong>at</strong> you are designing an applic<strong>at</strong>ion where you need to performthe oper<strong>at</strong>ions Insert, Delete Maximum, <strong>and</strong> Delete Minimum. Forthis applic<strong>at</strong>ion, the cost of inserting is not important, because it can be doneoff-line prior to startup of the time-critical section, but the performance ofthe two deletion oper<strong>at</strong>ions are critical. Repe<strong>at</strong>ed deletions of either kindmust work as fast as possible. Suggest a d<strong>at</strong>a structure th<strong>at</strong> can support thisapplic<strong>at</strong>ion, <strong>and</strong> justify your suggestion. Wh<strong>at</strong> is the time complexity foreach of the three key oper<strong>at</strong>ions?4.21 Write a function th<strong>at</strong> reverses the order of an array of n items.4.7 Projects4.1 A deque (pronounced “deck”) is like a queue, except th<strong>at</strong> items may be added<strong>and</strong> removed from both the front <strong>and</strong> the rear. Write either an array-based orlinked implement<strong>at</strong>ion for the deque.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!