2 - Abstract Data Types, Dynamic Arrays, Amortized Analysis Flashcards
(31 cards)
What is abstraction? Give three examples of abstraction from real life.
Abstraction is the process of trying to identify the most important qualities of an object or model. (e.g. table of contents, menu, class schedule)
What is information hiding? How is it related to abstraction?
Information hiding is purposely not including some information as a way of controlling complexity.
How is encapsulation related to abstraction? Explain how a function can be viewed as a type of encapsulation. What information is being hidden or abstracted away?
Encapsulation is placing items into a unit, or capsule. The outside is the task being performed, and the inside is how that task is performed.
What makes an ADT description abstract? How is it different from a function signature or an interface?
The description just provides a metaphor for how it behaves.
An interface, on the other hand, lists out exactly the functions that a file may have.
Come up with another example from everyday life that illustrates the behavior of each of the six classic abstractions (bag, stack, queue, deque, priority queue, map).
Bag: Adding a value, asking if a value is there, and removing a value. (like box of candy)
Set: In addition to bag operations, no element may appear more than once. (like a VIP party)
Stack: LIFO, remembers the order (like an ordered bus).
Queue: FIFO, remembers the order (like a line)
Deque: Insert/remove from either end, but not the middle (like a bus with two doors).
Priority queue: maintains value in order of importance. (like triage)
Map: maintains pairs of elements (each unique key is matched to a corresponding value).
What collection is important, and why? Is order important? Is time of insertion important?
- names of students enrollled
- files being held until can be printed on printer
- URLs for recently visited web pages in browser
- names of patients waiting for operating room in ER
- names and associated employee records in company database
- Set - don’t care about order, but can only enroll once
- Queue - first in, first out
- Stack - want the most recent to be on top
- Priority queue - make sure the most urgent is taken care of first
- Map - must have unique key corresponding to that employee
In what ways is a set similar to a bag? In what ways are they different?
Both are simply unordered lists that allow you to see whether something is there.
They are different in that a bag can contain the SAME thing, while a set must contain unique things.
In what ways is a priority queue similar to a queue? In what ways are they different?
Both place some priority on time as a negative factor.
However, for a queue, relative time is the only factor. For a priority queue, there may be other factors.
What is a dynamic array?
A dynamic array is one that automatically resizes as elements are added on to ensure that the array does not overflow.
What does the term capacity refer to in a dynamic array? What does the term size refer to?
The capacity is how much memory is currently allocated, but the size is how much of that memory is actually storing information.
Can you describe the set of legitimate subscript positions in a dynamic array? Is this different from the set of legal positions in an ordinary array?
In a dynamic array, the legitimate subscript positions are only ones that are within the size or one after the array.
In a normal array, you can’t just add onto the end of the array.
How does the add function in a dynamic array respond if the user enters more values than the current capacity of the array?
The add function doubles the size of the array.
Suppose the user enters 17 numbers into a dynamic array that was initialized with a capacity of 5? What will be the final length of the data array? How many times will it have been expanded?
Once you add onto a 5-sized array, it’ll double into a 10. Once you add onto a 10-sized array, it’ll double into a 20-sized array.
What is the algorithmic execution time for the _dyArrayDoubleCapcity, if n represents the size of the final data array?
First, a new array is created (1). Next, the old values are copied into the new array (n/2). Next, the free statement releases old memory (1). Finally, the pointer is changed to reference the array. (1).
Overall, it’s just n.
Based on your answer to the previous question, what is the worst case algorithmic complexity of the function dyArrayAdd?
Since there’s only one for loop, the copying of the array, it would be O(n).
What are the defining characteristics of the stack abstraction?
- Push(new Entry) - places new element on the top
- Pop () - removes the topmost item
- Top() - returns, but does not remove, the topmost item
- isEmpty() - determines whether the stack is empty
Explain the meaning of the term LIFO. Explain why FILO might have been equally appropriate.
LIFO means last in, first out. This just refers to the top of stack being the last “in”, but the first to leave.
It’s the same as FILO, or first in, last out, which refers to the bottom of the stack instead.
Give some examples of stacks found in real life.
Back and forward buttons in a web browser, buffered character input (backspace).
Explain how the use of an activation record stack simplifies the allocation of memory for program variables. Explain how an activation record stack makes it possible to perform memory allocation for recursive problems.
For example, if you have a recursive function, you need an activation record every time the function p is created so you know what is being passed and when finally it pops back, so that you can recover each space.
Explain the following postfix polish expression:
a. 2 3 + 5 9 - *
b. 2 3 5 9 + - *
a. (2+3) pushed back onto stack, then (5-9) pushed onto stack, so * pops both off and 5 * -4 = -20.
b. (5+9)=14 pushed back onto stack, then (3-14) = -11 pushed back onto stack, then 2*-11 = -22.
How is the memory representation of a linked list different from that of a Dynamic Array?
The dynamic array uses a large block of memory (much less common, but much more work to be performed).
The linked list allocates a new link EVERY time, but it’s only a single element.
What information is stored in a link?
Each link points to the next node, which contains a data value and a link to the following node.
How do you access the first element in a linked list? How would you get to the second element?
The first element in a linked list is just the head of the list. You would following the link in the head to get to the second element in the list.
In the last link, what value is stored in the next field?
Nothing - it’s a null pointer.