List Methods
Method
Time Complexity (Big-O)
boolean add(E e)
O(1) amortized, O(n) worst case
void add(int index, E element)
O(n)
void clear()
O(n)
boolean contains(Object o)
O(n)
boolean equals(Object o)
O(n)
E get(int index)
O(1) ArrayList, O(n) LinkedList
int indexOf(Object o)
O(n)
boolean isEmpty()
O(1)
Iterator<E> iterator()
O(1)
int lastIndexOf(Object o)
O(n)
E remove(int index)
O(n)
boolean remove(Object o)
O(n)
E set(int index, E element)
O(1)
int size()
O(1)
default void sort(Comparator<? super E> c)
O(n log n)
A Binary Search Tree must always be sorted.
List<E> subList(int fromIndex, int toIndex)
O(1)
A Binary Search Tree will not always have O(log n) performance when searching
because it can be skewed.
Binary Tree can have duplicates, Binary Search Tree can’t have duplicates
Height of an empty binary tree is 0 (sometimes considered to be -1)
If a call to java.util.Iterator.remove() is not preceded by a call to next(), remove()
will throw a IllegalStateException
Iterator objects throw a NoSuchElementException if they are asked to retrieve
the next element after all elements have been processed.
A balanced binary search tree will have a search Big-O runtime of O(n log n)
Sibling nodes are nodes that share the same parent.
Leaf nodes are nodes with no child nodes.
Unit testing refers to testing the smallest testable piece of software.
Integration testing refers to testing the interaction between different components
or systems to ensure they work together correctly.
System testing refers to evaluating the entire system to verify that it meets
specified requirements.
Acceptance testing refers to validating whether the software meets business
requirements and is ready for release.
A precondition is a statement of any assumptions or constraints on the method
data before the method begins execution.
Stack / Queue methods
Method
Data
Structure
Time
Complexity
E push(E e)
Stack
O(1)
boolean add(E e)
Queue
O(1)
boolean offer(E e)
Queue
O(1)
E pop()
Stack
O(1)
EmptyStackException
E remove()
Queue
O(1)
NoSuchElementException
E poll()
Queue
O(1)
E peek()
Stack
O(1)
EmptyStackException
E element()
Queue
O(1)
NoSuchElementException
E peek()
Queue
O(1)
int search(Object
o)
Stack
O(n)
boolean empty()
Stack
O(1)
boolean isEmpty()
Queue / Stack O(1)
Throws
Binary Tree Type
Description
Full Binary Tree
Every node has either 0 or 2 children.
Perfect Binary Tree
All levels are completely filled, and all leaf nodes are at the same
level.
Complete Binary Tree
All levels are filled except possibly the last, and all nodes are as
far left as possible.
Balanced Binary Tree
A tree where the height difference between the left and right
subtrees of any node is at most 1.
Degenerate /
Pathological Tree
A tree where each parent has only one child, making it essentially
a linked list.
Method
Description
Exceptions Thrown
boolean
hasNext()
Returns true if the iteration has more
elements.
None
E next()
Returns the next element in the
iteration.
NoSuchElementException – if no
more elements are available.
void
remove()
IllegalStateException – if next() has
Removes the last element returned by not been called before remove(), or
next(). Can be called only once per
remove() is called multiple times for
next() call.
one next().
IllegalStateException
In tail recursion, the last thing a method does is call itself.
In head recursion, the first thing a method does is call itself.
A proof by induction can be used to prove that a recursive method is correct.
Infinite recursion will result in a StackOverflowError being thrown.
void
forEachRe
maining(C
onsumer<?
super E>
Performs the given action for each
action)
remaining element.
NullPointerException – if the specified
action is null.
Reading and writing from files
Binary Tree
// Method to write to a file using PrintWriter
public static void writeFile(String filename, String content) {
try (PrintWriter writer = new PrintWriter(filename)) {
writer.println(content);
} catch (IOException e) {
e.printStackTrace();
}
}
Binary Search Tree
Heap
HashMap
// Method to read from a file using Scanner
public static String readFile(String filename) {
StringBuilder content = new StringBuilder();
try (Scanner scanner = new Scanner(new File(filename))) {
while (scanner.hasNextLine()) {
content.append(scanner.nextLine()).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return content.toString();
}
// Method to write binary data using DataOutputStream
public static void writeBinaryFile(String filename) {
try (DataOutputStream dos = new DataOutputStream(new
FileOutputStream(filename))) {
dos.writeInt(12345);
// Writing an integer
dos.writeDouble(3.14159); // Writing a double
dos.writeUTF("Hello, Binary!"); // Writing a string
} catch (IOException e) {
e.printStackTrace();
}
}
// Method to read binary data using DataInputStream
public static void readBinaryFile(String filename) {
try (DataInputStream dis = new DataInputStream(new
FileInputStream(filename))) {
int number = dis.readInt();
// Reading an integer
double pi = dis.readDouble(); // Reading a double
String message = dis.readUTF(); // Reading a string
System.out.println("Read Integer: " + number);
System.out.println("Read Double: " + pi);
System.out.println("Read String: " + message);
} catch (IOException e) {
e.printStackTrace();
}
}
Traversing binary trees
// Preorder Traversal (Root -> Left -> Right)
public static void preorder(TreeNode root) {
if (root == null) return;
System.out.print(root.val + " ");
preorder(root.left);
preorder(root.right);
}
// Inorder Traversal (Left -> Root -> Right)
public static void inorder(TreeNode root) {
if (root == null) return;
inorder(root.left);
System.out.print(root.val + " ");
inorder(root.right);
}
// Postorder Traversal (Left -> Right -> Root)
public static void postorder(TreeNode root) {
if (root == null) return;
postorder(root.left);
postorder(root.right);
System.out.print(root.val + " ");
}
Data Structure
Stack
Queue
Add / Insert
O(1) – push()
O(1) – add()
Remove
O(1) – pop()
O(1) – remove()
Get / Search
O(1) – peek()
O(1) – peek()
O(n) – level
order
O(n) unbalanced
O(log n) = O(h)
- balanced
O(log n)
O(1)
O(n)
O(n)
O(n) unbalanced
O(log n) balanced
O(log n)
O(1)
O(n) unbalanced
O(log n) balanced
O(1) – peek()
O(1)