Java Data Structures Cheat Sheet
Java Data Structures Cheat Sheet
Java Data Structures Cheat Sheet
Defi‐ - Stores data elements based on an Defi‐ - Stores data with nodes that point to Defi‐ - Stores data with key value pairs. -
nitio‐ sequential, most commonly 0 based, nitio‐ other nodes. - Nodes, at its most basic nitio‐ Hash functions accept a key and
n index. - Based on [tuples] n it has one datum and one reference n return an output unique only to that
(http://en.wikipedia.org/wiki/Tuple) from (another node). - A linked list _chains_ specific key. - This is known as
set theory. - They are one of the nodes together by pointing one node's hashing, which is the concept that an
oldest, most commonly used data reference towards another node. input and an output have a one-to-one
structures. correspondence to map information. -
Detai - Designed to optimize insertion and
Detai - Optimal for indexing; bad at deletion, slow at indexing and Hash functions return a unique
ls
ls searching, inserting, and deleting searching. - Doubly linked list has address in memory for that data.
(except at the end). - Linear arrays, or nodes that reference the previous Detai - Designed to optimize searching,
one dimensional arrays, are the most node. - Circularly linked list is simple ls insertion, and deletion. - Hash
basic. - Are static in size, meaning that linked list whose tail, the last node, collisions are when a hash function
they are declared with a fixed size. - references the head, the first node. - returns the same output for two distinct
Dynamic arrays are like one inputs. - All hash functions have this
Stack, commonly implemented with
dimensional arrays, but have reserved problem. - This is often
linked lists but can be made from
space for additional elements. - If a accommodated for by having the hash
arrays too. - Stacks are last in, first
dynamic array is full, it copies it's tables be very large. - Hashes are
out (LIFO) data structures. - Made with
contents to a larger array. - Two important for associative arrays and
a linked list by having the head be the
dimensional arrays have x and y database indexing.
only place for insertion and removal. -
indices like a grid or nested arrays. Queues, too can be implemented with Big- - Indexing: Hash Tables: O(1) -
Big- - Indexing: Linear array: O(1), Dynamic a linked list or an array. - Queues are a O Search: Hash Tables: O(1) - Insertion:
O array: O(1) - Search: Linear array: O(n), first in, first out (FIFO) data structure. effici Hash Tables: O(1)
effici Dynamic array: O(n) - Optimized - Made with a doubly linked list that ency
ency Search: Linear array: O(log n), only removes from head and adds to
Dynamic array: O(log n) - Insertion: tail. Binary Tree
Linear array: n/a Dynamic array: O(n)
Big- - Indexing: Linked Lists: O(n) - Search:
Defi - Is a tree like data structure where
O Linked Lists: O(n) - Optimized Search:
niti every node has at most two children. -
effici Linked Lists: O(n) - Insertion: Linked
on There is one left and right child node.
ency Lists: O(1)
Detai - Designed to optimize searching and Detai - Optimal for searching a tree that is
ls sorting. - A degenerate tree is an ls deeper than it is wide. - Uses a stack
unbalanced tree, which if entirely one- Breadth First Search to push nodes onto. - Because a stack
sided is a essentially a linked list. - is LIFO it does not need to keep track
Defi‐ - An algorithm that searches a tree (or
They are comparably simple to of the nodes pointers and is therefore
nitio‐ graph) by searching levels of the tree
implement than other data structures. - less memory intensive than breadth
n first, starting at the root. - It finds every
Used to make binary search trees. - A first search. - Once it cannot go further
node on the same level, most often
binary tree that uses comparable keys left it begins evaluating the stack.
moving left to right. - While doing this it
to assign which direction a child is. - tracks the children nodes of the nodes Big- - Search: Depth First Search: O(|E| +
Left child has a key smaller than it's on the current level. - When finished O |V|) - E is number of edges - V is
parent node. - Right child has a key examining a level it moves to the left effici number of vertices
greater than it's parent node. - There most node on the next level. - The ency
can be no duplicate node. - Because of bottom-right most node is evaluated
the above it is more likely to be used as last (the node that is deepest and is
Breadth First Search Vs. Depth First Search
a data structure than a binary tree. An farthest right of it's level).
AVL Tree is a balanced binary search - The simple answer to this question is that it
Detai - Optimal for searching a tree that is
tree. -The process for inserting or depends on the size and shape of the tree.
ls wider than it is deep. - Uses a queue to
deleting is the same as in a - For wide, shallow trees use Breadth First
store information about the tree while it
regular(unbalanced) BST, except you Search
traverses a tree. - Because it uses a
have to rebalance after each operation. - For deep, narrow trees use Depth First
queue it is more memory intensive than
A node in an AVL tree is balanced if its Search
depth first search. - The queue uses
balance factor is either -1,0, or 1
more memory because it needs to
Big- - Indexing: Binary Search Tree: O(log Nuances:
stores pointers
O n) - Search: Binary Search Tree: O(log - Because BFS uses queues to store
Big- - Search: Breadth First Search: O(|E| +
effici n) - Insertion: Binary Search Tree: information about the nodes and its children, it
O |V|) - E is number of edges - V is
ency O(log n) could use more memory than is available on
effici number of vertices
ency your computer. (But you probably won't have
The balance factor of a node is the height of
to worry about this.)
its right subtree minus the height of its left
- If using a DFS on a tree that is very deep you
subtree Depth First Search
might go unnecessarily deep in the search. See
Defi - An algorithm that searches a tree (or [xkcd](http://xkcd.com/761/) for more
niti graph) by searching depth of the tree information.
on first, starting at the root. - It traverses left - Breadth First Search tends to be a looping
down a tree until it cannot go further. - algorithm.
Once it reaches the end of a branch it - Depth First Search tends to be a recursive
traverses back up trying the right child algorithm.
of nodes on that branch, and if possible
left from the right children. - When Efficient Sorting Basics
finished examining a branch it moves to
the node right of the root then tries to go
left on all it's children until it reaches the
bottom. - The right most node is
evaluated last (the node that is right of
all it's ancestors).
Defi‐ - A comparison based sorting algorithm Detai - While it has the same Big O as (or - Quicksort is likely faster in practice.
nitio‐ - Divides entire dataset into groups of ls worse in some cases) many other - Merge Sort divides the set into the smallest
n at most two. - Compares each number sorting algorithms it is often faster in possible groups immediately then reconstructs
one at a time, moving the smallest practice than many other sorting the incrementally as it sorts the groupings.
number to left of the pair. - Once all algorithms, such as merge sort. - Know - Quicksort continually divides the set by the
pairs sorted it then compares left most that it halves the data set by the average, until the set is recursively sorted.
elements of the two leftmost pairs average continuously until all the
creating a sorted group of four with the information is sorted. Heap Sort
smallest numbers on the left and the Big- - Best Case Sort: Merge Sort: O(n) -
largest ones on the right. - This process Definitio Sorts using a complete binary Tree.
O Average Case Sort: Merge Sort: O(n
is repeated until there is only one set. n:
effici log n) - Worst Case Sort: Merge Sort:
Detai - This is one of the most basic sorting O(n^2) Details: ArrayList can be used to store a
ency
ls algorithms. - Know that it divides all the Heap
data into as small possible sets then For a node of i:
Bubble Sort
compares them. -Left child: 2i+1
Defi‐ - A comparison based sorting algorithm -Right child: 2i+2
Big- - Best Case Sort: Merge Sort: O(n) -
Average Case Sort: Merge Sort: O(n nitio‐ - It iterates left to right comparing every -Parent: (i - 1)/2
O
log n) - Worst Case Sort: Merge Sort: n couplet, moving the smaller element to
effici Big-O: O(nlogn)
O(n log n) the left. - It repeats this process until it
ency
no longer moves and element to the
left.
Quicksort
Detai - While it is very simple to implement, it
Defi - A comparison based sorting algorithm - ls is the least efficient of these three
niti Divides entire dataset in half by selecting sorting methods. - Know that it moves
on the average element and putting all one space to the right comparing two
smaller elements to the left of the elements at a time and moving the
average. - It repeats this process on the smaller on to left.
left side until it is comparing only two
Big- - Best Case Sort: Merge Sort: O(n) -
elements at which point the left side is
O Average Case Sort: Merge Sort: O(n2)
sorted. - When the left side is finished - Worst Case Sort: Merge Sort: O(n2)
effici
sorting it performs the same operation
ency
on the right side. - Computer
architecture favors the quicksort
process.