1/33
Looks like no tags are added yet.
Recursion
A method for solving problems where a method calls itself
Properties of Recursion
relies on the runtime stack as every call gets its own stack space
all problems can be solvable with iteration (and its usually more efficient)
Parts of recursion
The base case is a problem instance that’s simple or trivial
the recursive step is what recalls the function
Recursion Factorial example
public static int factorial(int n) {
If (n == 0)
return 1;
else return n * factorial(n - 1);
}
Binary Search
A type of Array search where you repeatedly split the array in half until you find the number
Array must be ordered
Big O
Represents an upper bound on the number of operations performed by an algorithm for a sufficiently large input size
Determining asymptotic complexity
As n increases, the highest-order term of a function dominates its value
Ex. 2n+100 is O(n)
Ex. Log3(n) is O(log(n))
Ex. n³ + 100n² + 28 is O(n³)
Analyzing Algorithms
Find the Critical Section (The part of the code that is run the most)
Ignore the less frequently executed parts
Determine how many times the critical section is executed
What are 2 important operations to know?
1 + 2 + 3 +…+ (n-1) + n → n(n + 1) / 2
1 + 2 + 3 +…+ (n-2)+(n-1) → n(n-1) / 2

Types of case analysis
Best Case
Worst Case
Average Case (average and expected)
Amortized Cost
Best Case
Smallest number of operations that it requires
Usually not that useful to know
Ex. First element in a linear search
Worst Case
Largest number of operations that an algorithm requires
Useful as an upper bound on performance
Ex. Last element in a linear search
Average Case
The “Typical amount of cases expected
Normal computed by finding the average case with all cases being the same likelihood
Usually not realistic but easy to compute and compare
The expected case is a weighted average over all possible cases
More accurate harder to compute
Amortized cost
applies to the worst-case sequences of operations
gives average running time per operation
Iterator interface
next() should return some element
hasNext() should return true if any elements haven’t been returned by next() yet
remove() is optional; can be called once after each call to next() to remove the element that next() just returned
How to implement it
add an iterator interclass if the class implements Iterator
Design Patterns
descriptions of reusable solutions to common software design points
captures the experience of experts
Collection interface
The root interface of Java’s collection hierarchy
Collection methods
add
clear
contains
isEmpty
iterator
remove
siF
foreach loop with iterators
You can use a foreach loop for any class that implements Iterable including all Collections
for each loops are handled in the JVM with hasNext() and next()
Stacks and Queues
Both simple data structures
Stacks FILO
Queues FIFO
Stacks are used in the runtime stack
Circular Array for queues uses front = (front + 1) % capacity
Some Design Patterns
Creational: Deal with the best way to create objects
Structural: Ways to bring together groups of objects
Behavioral: Ways for object to communicate and interact
The adaptor pattern
converts existing interfaces to a new interface
Helps match an interface
Increases transparency of classes
Ex: implementing a stack with an already implemented linked list
Binary trees
Tree with at most two children per element
Tree traversal
DFS: visits far ahead then backs up
BFS: visits elements according to how far away they are from the root, using a queue
DFS
Three types
preorder: MLR
In order: LMR, in a binary search tree leads to ordered list
Postorder: LRM
Polymorphic binary search tree
Uses NonEmptyTree and Empty Tree
Can use a singleton design pattern and create only one empty tree object
Binary Search Tree deletion
Perform a search for value X
If x is a leaf remove X
a) replace X with the largest element In its left subtree or the smallest element in its right subtree
Then delete the replacement value from the subtree recursively
Balanced and degenerated binary tree
a balanced binary tree has mostly 2 children per element
A degenerate binary tree has mostly one child per element
Binary tree height
A perfectly balanced tree number of elements is 2^(heights + 1) - 1
Finding the height of a balanced tree is O(log(n))
height of a degenerate tree is O(n)
Binary search tree operations
Lookup(average case)
Balanced tree: O(log(n))
Degenerate tree: O(n)
Insertion(average case)
B tree: O(log(n))
D tree: O(n)
Deletion: same as insertion
Factory Pattern
provides an abstraction for deciding which class should be instantiated based on parameters given
Used when a class cannot anticipate which subclass should be made,
or to separate a family of objects using shared interface
or to hide concrete classes from the client
Decorator pattern
Attaches additional responsibility or functionality to an object
provides a flexible alternative to subclassing
Add new functions to an I object without affecting other object
Make responsibilities easily added and removed dynamically and transparently to the object
Ex: decorator that adds toppings to pixxa
Maker interface
label semantic attributes of a class
Need to indicate attributes of a class
Allows identification of attributes of objects without assuming they are instances of any class