You are on page 1of 20

Midterm Review

ERIN KEITH

12_COMPLEXITY 1
Topics
1. Define ADT
◦ What are their purposes
2. What are the C++ tools we use to implement data structures and how do they
support the above purposes?
3. What are the two approaches to storage in Data Structure implementations?
◦ What is the difference in implementations for these approaches
4. Implement a function from a data structure, using each approach
◦ explain the similarities and differences
5. implement a function from 2 data structures, using the same approach
◦ explain the similarities and differences
6. Given a problem, which data structure would you use to solve it and why
◦ write a main driver to solve the problem using that data structure
7. Run time complexity

12_COMPLEXITY 2
Abstract Data Types
In a sentence or two, describe the main purpose of ADTs.
◦ They define a class of abstract objects which is
completely characterized by the operations available on
those objects.
List the categories of operations for ADTs.
1. Creators
2. Producers
3. Observers
4. Mutators

3
ADT Operations
Lists Queue
◦ isEmpty ◦ isEmpty
◦ insert ◦ enqueue
◦ remove ◦ dequeue
Stacks ◦ peekFront
◦ isEmpty
◦ push
◦ peek
◦ pop

4
ADTs Attributes
Lists Queue
◦ position ◦ front
Stacks ◦ back
◦ top

Use the attributes and operations to describe what makes the


ADT unique.

5
Example: Bag
◦ Holds an unordered collection of objects.
◦ Since there’s no order, either a head pointer (for linked)
or array and itemCount (for array) will suffice as
attributes.
◦ Behaviors
◦ Get the number of current items
◦ Check if bag is empty
◦ Add an item to bag
◦ Remove an item from the bag
◦ Empty the bag
◦ Get the number of specific items
◦ Find an item in bag
◦ See each item in bag
◦ since there’s no order, you can’t “grab” an item out

6
C++ Tools
How do we save a collection of objects?
Templates!

How do we translate these behaviors to an


implementation?
Interfaces!

7
Data Structure
Implementations
Operations
C++ Class
Behaviors
Templates Interfaces Implementations
Axioms

Array
Data
ADT
Structure
Linked

8
Implement a function
template<class ItemType> template<class ItemType>
vector<ItemType> ArrayBag<ItemType>::toVector() const{ vector<ItemType> LinkedBag<ItemType>::toVector() const{
vector<ItemType> bagContents; vector<ItemType> bagContents;

for (int i = 0; i < itemCount; i++){ Node<ItemType>* curPtr = headPtr;


bagContents.push_back(items[i]);
} for (int i = 0; i < itemCount && curPtr != nullptr;
i++){
return bagContents; bagContents.push_back(curPtr->getItem());
} curPtr = curPtr->getNext();
}

return bagContents;
}

The array implementation allows for iterating over the array


from the 0 index to itemCount – 1. The linked
implementation must iterate over the chain by using the
getNext() function on each node.

9
Implement a function
template<class ItemType> template<class ItemType>
bool ArrayBag<ItemType>::add(const ItemType& newEntry){ bool ArrayList<ItemType>::insert(int newPos, const
bool hasRoom = itemCount < maxItems; ItemType& newEntry){
bool canInsert = itemCount < maxItems
if(hasRoom){ && newPos >= 1 && newPos <= itemCount
items[itemCount] = newEntry; + 1;
itemCount++;
} if(canInsert){
for(int pos = itemCount; pos >= newPos; pos--){
return hasRoom; items[pos + 1] = items[pos];
} }
items[newPos] = newEntry;
itemCount++;
}

return canInsert;
}

While these both use a simple index to add items to the collection, the
array implementation for a bag can accommodate adding the entry to
the end every time. For an array list, items at the end need to be shuffled
over to make room for the new item.

10
Palindrome
A string where letters read same left to right and
right to left.

11
Palindrome
A string where letters read same left to right and
right to left.
◦ As you traverse it from left to right, add each
character to a queue and a stack.
◦ Compare the characters at the top/front.
◦ If they’re the same, pop/dequeue and continue.
Stack
o
Queue t a c o
c
a
t

12
Complexity
int count = 0;
for(int i = 0; i < N; i++)
for(int j = 0; j < i; j++)
count++;

13
Complexity
int count = 0;
for(int i = 0; i < N; i++)
for(int j = 0; j < i; j++)
count++;

This is the action we


must count

14
Complexity
How many times will count++; run ?
When i = 0, 0
When i = 1, 1
When i = 2, 2
0 + 1 + 2 + … + (N – 1) = N * (N – 1) / 2
O(n2)

15
Complexity
while(low <= high){
mid = (low + high)/2;
if(target < list[mid])
high = mid – 1;
else if(target > list[mid])
low = mid + 1;
else break;
}

16
Complexity
How many times will the loop run ?
https://hackernoon.com/what-does-the-time-complexity-o-log-n-ac
tually-mean-45f94bb5bfbf

O(logn)

17
Complexity
int count = 0;
for(int i = N; i > 0; i /= 2)
for(int j = 0; j < i; j++)
count++;

18
Complexity
How many times will count++; run ?
When i = N, N
When i = N/2, N/2
When i = N/4, N/4
N + N/2 + N/4 + … + 1 = 2 * N
O(n)

19
Next Class
Midterm
Textbook:
• Chapters 1, 6, 8, 10, & 13
Internet:
• See Module Pages

12_COMPLEXITY 20

You might also like