Midterm Study Guide
Midterm Study Guide
Big Omega: The lower bound for the worst case scenario.
Big Theta: The exact complexity or a useful range of values.
Might need to review the first few lectures; how to find big omega, theta, from a given T(N) function.
Online Algorithms: algos that require only constant space and run in linear time.
O(log(N)) : are algos that take constant time to cut the problem size by a fraction.
List: A linear sequence of arbitrary number of items, of the form A0, A1, A2,... An-1.
● For every element there is only one next element.
● Every element holds its own position.
● For any list(but the empty list which has size 0)
● Ai follows Ai-1 where (i<N)
● Ai-1 precedes Ai (i >0)
● The end and start of a list is NULL
● Implemented with Array
Array operations:
PrintList → O(N)
Find → O(N)
Findkth → O(1)
Insert and Remove → O(N)
● ALL important operations are linear(this is bad)
Linked List: consists of nodes, which are not necessarily adjacent in memory.
● Each node consists of the element(data) and a link to a node containing its successor.
● The last link has a reference null.
PrintList is O(N)
FindKth O(N) (Requires traversing the entire list)
Remove O(1) : Only needs to change the pointer
Insert O(1) : Only needs to change the pointer
Double Linked List: has two pointers per node, one to the next node and one to the previous node.
● Solves the problem of removing the first element.
For a get method it is the opposite; LinkedList get O(N), ArrayList get is O(1)
With Arrays:
● Hold the position front and back, which represent the front and ends of the queue respectively.
● the number of current elements entered into the queue (current size).
● Circular array implementation using MOD is used to prevent index out of bound errors.
Trees: collection of N nodes, one of which is the root, and N-1 edges. They are a one to many
relationship, each node can have multiple roots.
Leaf : nodes with no children.
Siblings: nodes with the same parent.
Path: from node n1 to nk, the sequence of nodes, N1,N2,..Nk, such that Ni is a parent of Ni+1 for 1<= i
<k
● There are unique paths from root to leaf.
Depth: The depth of a node is the number of edges from the node to the tree's root node.
A root node will have a depth of 0.
Height: The height of a node is the number of edges on the longest path from the node to a leaf.
A leaf node will have a height of 0.
How to:
● Keep the children of each node in a linked list of tree nodes.
● Note: Trees lack random access.
BST complexity:
Find O(N)
Insert O(N)
Remove O(N)
● Adding elements via median first, then left media, right media, etc. Creates a balanced tree.
● For a balanced tree, Insert, Del, Find are O(log N) in the average case.
Lazy Delete: each node contains a boolean field, indicating if the node is deleted or not. The nodes are
still part of the tree, they just get ignored.
Heaps allow:
BIG NOTE: Heaps do not require binary tree sorting, sorting is done by level, each level has to
be greater(or less than) than the lower one.
Insert(add)
DeleteMin: finds, returns, and removes the minimum element in the priority queue.
● There is no find operation
● Each element has a “priority”
● the min/max element in each respective type of heap has the priority. (min for min
queues, max for max queues.)
Ex. a Heap with {7,5,6}
deletemin →deletes 5.
deletemin → deletes 6.
Binary min-heap : a binary tree that is completely filled. (With the possible exception of the
bottom level.
● The priority of every(non-root) node is less important than the priority of its parent.
● The key of a node <= the keys of its children
● Note: A heap is NOT a binary search tree.
Insert:
I. Put the new node in the next position on the bottom row.
II. Percolate up to restore heap-order property.
● On average it has been down that heaps use 2.607 comparisons
Heaps complexity:
Delete → O(logN)
Insert → O(logN) but O(1)
Hash Tables: