Project 2, Square Lists: Addendum
Here are some ways we could improve the square list data structure. In particular, we can make addFirst(), removeFirst(), addLast() and removeLast() run in O(1) amortized time instead of O(√n) amortized time. Implementing these features would make the running time of the [] operator O(√n) amortized time instead of O(√n) worst case time.
However, do not submit code with these features as they will totally mess up the grading.
- Early empty list deletion:
- As soon as you notice that an inner list is empty, delete it. Don't wait for consolidate().
- Don't make long lists:
- In addFirst() and addLast(), start a new inner list if the length of the first or last inner list will exceed √n after insertion.
- Early splits:
- Whenever we perform an operation on an inner list that involves looking through the list (for example, a search, but not size()), split the list if the length is already greater than 1.5 √n. Since we are looking through the list already, we can do the split now instead of later.
- Early merge:
- Whenever we remove an item from an inner list, check to see if the inner list is now a short list. If the list is now short and an adjacent inner list is also short, we can merge those two now instead of waiting for consolidate(). This will only take an additional O(1) time.
- Delayed Consolidation:
- Don't consolidate after every operation that changes the length of the list. Wait for the number of inner lists to exceed 5 √n. It costs us O( √n) time just to look through the top-level list. We shouldn't do this for every addFirst() or addLast(), especially if we don't make long lists. Wait until enough short lists pile up and then merge them in one sweep.
- Delayed Splits:
- Long lists don't bother us unless we have to search through them. So, during consolidate, just ignore the long lists. The long lists will be split when we work with them if we implement early splits. This actually turns early splits into delayed splits, so we should adjust the threshold for early/delayed splits back to 2 √n.