Review Sheet
1 Java
1.1 Basics
- primitive types
- operators
- Strings
main
- if/else
- while
- for
- arrays
1.2 Object-oriented programming
- terminology
- class
- object
- field
- method
- constructor
- accessor
- mutator
- access modifiers (private, public)
- controls what code can use a field or method
- static
- method or field associated with a class instead of an individual object
- interfaces
- declares public methods, but does not implement
- a class
implements
an interface, must provide all the methods declared in that interface
- a class can implement multiple interfaces
- an interface
extends
another interface, inheriting its methods and adding more
- a class
extends
another class to inherit its public methods and fields
- use sparingly, frequently complicates program design without much benefit
2 Abstract Data Types
- Stack
- last-in-first-out
- important operations: push, pop
- Queue
- first-in-first-out
- important operations: add (enqueue), remove (dequeue)
- Deque
- short for double-ended queue
- supports both LIFO and FIFO behavior
- Map
- associates unique keys with values
- important operations: put, get, contains
- Set
- same as map, except only keys, no values
- Priority Queue
- keeps track of the single minimum (or maximum) element
- important operations: add, removeMin (or removeMax)
- Graph
- set of vertices V
- set of edges E, each of which is a pair of adjacent vertices
- properties
- directed/undirected
- weighted/unweighted
- sparse/dense
- connected/unconnected
- cyclic/acyclic
3 Data Structures
- Array
- Good choice for Stack and binary heap
- Also useful for adjacency list and adjacency matrix
- Constant-time random access
- Linear-time to add or remove anywhere except the end
- Have to shift over to make room or to fill in
- Extensible array (Java's
ArrayList
)
- Uses an array internally, doubling its size as needed
- Averages out to constant time adding to the end
- Linked list
- Singly-linked list
- Good choice for Queue (Java's
LinkedList
)
- Head, tail, each node has a next reference
- Constant-time adding and removing from the front, constant-time adding to the end
- Doubly-linked list
- Good choice for Deque (Java's
LinkedList
)
- Head, tail, each node has a next and previous reference
- Adding dummy nodes simplifies implementation
- Like singly-linked, but also constant-time removing from the end
- Hash table
- Good choice for Map and Set (Java's
HashMap
and HashSet
)
- Constant time insert, delete, contains
- Stores unique keys (with associated values if implementing a Map)
- Uses internal array, doubling in size as needed
- Number of elements in the table divided by the array size is the load factor
- Hashing: turn object into array index
- Uses
hashCode
method in Java
- Collision resolution: what to do when to objects hash to the same index
- separate chaining
- linear probing
- quadratic probing
- Binary Search Tree
- Good choice for Map and Set (Java's
TreeMap
and TreeSet
)
- If keys are comparable
- And you need operations related to the sorted order of the keys
- Trees have nodes with children
- Binary tree nodes have 0 to 2 children
- BST property is that left children are less, right chrildren are greater
- Logarithmic time insert, delete, contains
- Linear time sorted order (in-order traversal)
- Efficiently supports operations such as getting the nearest key or all the keys in a range
- Performance depends on tree being balanaced
- Self-balancing trees such as the AVL tree or Red-Black tree ensure this (Java uses Red-Black)
- k-d tree is a BST-like structure for k-dimensional data (Java library does not implement)
- Binary min-heap (Java's
PriorityQueue
)
- A binary tree with two properties:
- Structure property: must be a complete binary tree (full except for bottom row), therefore is always balanced
- Heap property: node must be smaller than its children
- Graph data structures (Java library does not implement)
- Adjacency list
- Array of linked lists (or equivalent), storing each node's neighbors
- Good for sparse graphs
- Adjacency matrix
- 2D array where each cell indicates whether an edge exists between those two vertices
- Good for dense graphs
4 Algorithms
- Sorting (Java provides
Arrays.sort
and Collections.sort
)
- Many different algorithms
- Mergesort recursively subdivides the list, sorts both parts, and then merges them together
- \(O(n\log n)\), the best any comparison sort can do
- Searching
- Linear search
- Binary search
- Data must be sorted
- Repeatedly restrict search to half the remaining items based on comparison with middle value
- Recursive backtracking
- Make a choice (or return if at a dead end)
- Recursively explore solutions involving that choice
- Unmake the choice
- Useful for exploring all possible solutions while not wasting time on dead end states
- Graph search
- Breadth-first search (BFS)
- Uses a Queue to store pending nodes
- can find shortest paths in terms of number of edges from a source
- Depth-first search (DFS)
- Uses a Stack to store pending nodes (or uses recursive calls)
- Shortest paths
- Dijkstra's is a greedy algorithm that finds all the shortest paths from a source node in a weighted graph
- So long as no weights are negative
- Bellman-Ford can handle negative weights
- Use a Priority Queue to store pending nodes (because we want to select the closest one each time)
- Minimum spanning tree
- Subset of edges that connects the graph with the minimum total weight
- Prim's algorithm very similar to Dijkstra's, except selecting by just the connecting edge weight instead of the path weight
- Kruskal's algorithm processes edges in order of weight