AmortizedAnalysis

We assume pop throws an exception if stack is empty

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ______________________________________________________________________________________________________________________________________________________________________________________________________ Exceptions. We assume POP throws an exception if stack is empty. 30 Multipop stack Goal. Support operations on a set of n elements: ・PUSH(S, x): push object x onto stack S. ・POP(S): remove and return the most-recently added object. ・MULTIPOP(S, k): remove the most-recently added k objects. Theorem. Starting from an empty stack, any intermixed sequence of n PUSH, POP, and MULTIPOP operations takes O(n2) time. Pf. overly pessimistic upper bound ・Use a singly-linked list. ・PoP and PUSH take O(1) time each. ・MULTIPOP takes O(n) time. ▪ top ● 1 ● 4 ● 1 ● 3 ● 31 Multipop stack: aggregate method Goal. Support operations on a set of n elements: ・PUSH(S, x): push object x onto stack S. ・POP(S): remove and return the most-recently added object. ・MULTIPOP(S, k): remove the most-recently adde...
View Full Document

This document was uploaded on 02/05/2014.

Ask a homework question - tutors are online