Test 3 Flashcards
is one of the most commonly used data structures in computer science
A stack
The stack’s storage policy is
Last-In, First Out , or LIFO
What methods can Stacks use?
boolean empty() E peek() E pop() E push( E obj )
a string that reads identically in either direction, letter by letter (ignoring case)
Palindrome
When analyzing arithmetic expressions, it is important to determine whether an expression is balanced with respect to parentheses.
The problem is further complicated if braces or brackets are used in conjunction with parentheses
The solution is to use:
stacks!
PITFALL of stacks: attempting to pop an empty stack will:
throw an Emptystackexception . You can guard against this by either testing for an empty stack or catching the exception
Stacks are so common in computing , that Java included. A Stack class in its early versions:
import java.util.Stack; Stack myStack = new Stack();
A queue differs from a stack in one important way:
a queue a is FIFO list - First-In, First-Out
Operating systems use queues to:
keep track of tasks waiting for a scarce resource
ensure that the tasks are carried out in FIFO order
Queue uses methods:
boolean offer(E item) E remove() E peek() E element()
The __________ class provides methods for inserting and removing elements at either end of a linked list, which means all _______ methods can be implemented easily
Linkedlist
Queue
can be used to generate simple solutions to problems that are difficult to solve by other means
Recursion
reduces a problem into one or more simpler versions of itself
Recursion
I’m recursive strategy there must be _______________ (the _________), for a small value of n, that can be solved directly
at least one case
base case
In recursive strategy a problem of a given size n can be reduced to smaller versions of the same problem (____________)
recursive case(s)
The process of returning from recursive calls and computing the partial results is called
unwinding the recursion
Java maintains a ___________ on which it saves new information in the form of an _____________
run-time stack
activation frame
The activation frame contains storage for:
method arguments
local variables ( if any)
the return address of the instruction that called the method
Whenever a new method is called ( recursive not ), a new activation frame is:
pushed onto the run-time stack
Mathematicians often use recursive definitions of formulas that lead naturally to recursive algorithms
Examples include :
factorials
powers
greatest common divisors (gcd)
Known as infinite recursion: the program will eventually throw:
Stack OverflowError exception
In the factorial method, you could throw an ________________ if n is negative
IllegalArgumentException
In iteration, a loop condition determines:
whether to repeat the loop body or exit the loop
A recursive algorithm may be simpler than an iterative algorithm and thus:
easier to write, code, debug, and read
Recursive methods often have __________________ relative to their iterative counterparts
slower execution times
The overhead for loop repetition is _______ than the overhead for a method call and return
smaller
If it is easier to design a solution using recursion , then:
you should code it as a recursive method
The reduction in efficiency does not outweigh:
the advantage of readable code