\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Ieternalleo} \pdfinfo{ /Title (java-data-structures.pdf) /Creator (Cheatography) /Author (Ieternalleo) /Subject (Java Data Structures Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Java Data Structures Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Ieternalleo} via \textcolor{DarkBackground}{\uline{cheatography.com/45716/cs/13401/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Ieternalleo \\ \uline{cheatography.com/ieternalleo} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 6th November, 2017.\\ Updated 6th November, 2017.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{x{1.64241 cm} x{3.33459 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Array}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Definition}} & - Stores data elements based on an sequential, most commonly 0 based, index. - Based on {[}tuples{]}(http://en.wikipedia.org/wiki/Tuple) from set theory. - They are one of the oldest, most commonly used data structures. \tn % Row Count 9 (+ 9) % Row 1 \SetRowColor{white} {\bf{Details}} & - Optimal for indexing; bad at searching, inserting, and deleting (except at the end). - {\bf{Linear arrays}}, or one dimensional arrays, are the most basic. - Are static in size, meaning that they are declared with a fixed size. - {\bf{Dynamic arrays}} are like one dimensional arrays, but have reserved space for additional elements. - If a dynamic array is full, it copies it's contents to a larger array. - {\bf{Two dimensional arrays}} have x and y indices like a grid or nested arrays. \tn % Row Count 28 (+ 19) % Row 2 \SetRowColor{LightBackground} {\bf{Big-O efficiency}} & - Indexing: Linear array: O(1), Dynamic array: O(1) - Search: Linear array: O(n), Dynamic array: O(n) - Optimized Search: Linear array: O(log n), Dynamic array: O(log n) - Insertion: Linear array: n/a Dynamic array: O(n) \tn % Row Count 39 (+ 11) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.64241 cm} x{3.33459 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Linked List}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Definition}} & - Stores data with {\bf{nodes}} that point to other nodes. - Nodes, at its most basic it has one datum and one reference (another node). - A linked list \_chains\_ nodes together by pointing one node's reference towards another node. \tn % Row Count 9 (+ 9) % Row 1 \SetRowColor{white} {\bf{Details}} & - Designed to optimize insertion and deletion, slow at indexing and searching. - {\bf{Doubly linked list}} has nodes that reference the previous node. - {\bf{Circularly linked list}} is simple linked list whose {\bf{tail}}, the last node, references the {\bf{head}}, the first node. - {\bf{Stack}}, commonly implemented with linked lists but can be made from arrays too. - Stacks are {\bf{last in, first out}} (LIFO) data structures. - Made with a linked list by having the head be the only place for insertion and removal. - {\bf{Queues}}, too can be implemented with a linked list or an array. - Queues are a {\bf{first in, first out}} (FIFO) data structure. - Made with a doubly linked list that only removes from head and adds to tail. \tn % Row Count 37 (+ 28) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.64241 cm} x{3.33459 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Linked List (cont)}} \tn % Row 2 \SetRowColor{LightBackground} {\bf{Big-O efficiency}} & - Indexing: Linked Lists: O(n) - Search: Linked Lists: O(n) - Optimized Search: Linked Lists: O(n) - Insertion: Linked Lists: O(1) \tn % Row Count 6 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.64241 cm} x{3.33459 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{{\bf{Hash Map}}}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Definition}} & - Stores data with key value pairs. - {\bf{Hash functions}} accept a key and return an output unique only to that specific key. - This is known as {\bf{hashing}}, which is the concept that an input and an output have a one-to-one correspondence to map information. - Hash functions return a unique address in memory for that data. \tn % Row Count 13 (+ 13) % Row 1 \SetRowColor{white} {\bf{Details}} & - Designed to optimize searching, insertion, and deletion. - {\bf{Hash collisions}} are when a hash function returns the same output for two distinct inputs. - All hash functions have this problem. - This is often accommodated for by having the hash tables be very large. - Hashes are important for associative arrays and database indexing. \tn % Row Count 27 (+ 14) % Row 2 \SetRowColor{LightBackground} {\bf{Big-O efficiency}} & - Indexing: Hash Tables: O(1) - Search: Hash Tables: O(1) - Insertion: Hash Tables: O(1) \tn % Row Count 32 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.64241 cm} x{3.33459 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Binary Tree}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Definition}} & - Is a tree like data structure where every node has at most two children. - There is one left and right child node. \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} {\bf{Details}} & - Designed to optimize searching and sorting. - A {\bf{degenerate tree}} is an unbalanced tree, which if entirely one-sided is a essentially a linked list. - They are comparably simple to implement than other data structures. - Used to make {\bf{binary search trees}}. - A binary tree that uses comparable keys to assign which direction a child is. - Left child has a key smaller than it's parent node. - Right child has a key greater than it's parent node. - There can be no duplicate node. - Because of the above it is more likely to be used as a data structure than a binary tree. An {\bf{AVL Tree}} is a balanced binary search tree. -The process for inserting or deleting is the same as in a regular(unbalanced) {\bf{BST}}, except you have to rebalance after each operation. A node in an AVL tree is balanced if its {\bf{balance factor}} is either {\bf{-1,0,}} or {\bf{1}} \tn % Row Count 39 (+ 34) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.64241 cm} x{3.33459 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Binary Tree (cont)}} \tn % Row 2 \SetRowColor{LightBackground} {\bf{Big-O efficiency}} & - Indexing: Binary Search Tree: O(log n) - Search: Binary Search Tree: O(log n) - Insertion: Binary Search Tree: O(log n) \tn % Row Count 5 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The {\bf{balance factor}} of a node is the height of its right subtree minus the height of its left subtree} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Search Basics}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.64241 cm} x{3.33459 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Breadth First Search}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Definition}} & - An algorithm that searches a tree (or graph) by searching levels of the tree first, starting at the root. - It finds every node on the same level, most often moving left to right. - While doing this it tracks the children nodes of the nodes on the current level. - When finished examining a level it moves to the left most node on the next level. - The bottom-right most node is evaluated last (the node that is deepest and is farthest right of it's level). \tn % Row Count 18 (+ 18) % Row 1 \SetRowColor{white} {\bf{Details}} & - Optimal for searching a tree that is wider than it is deep. - Uses a queue to store information about the tree while it traverses a tree. - Because it uses a queue it is more memory intensive than {\bf{depth first search}}. - The queue uses more memory because it needs to stores pointers \tn % Row Count 30 (+ 12) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.64241 cm} x{3.33459 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Breadth First Search (cont)}} \tn % Row 2 \SetRowColor{LightBackground} {\bf{Big-O efficiency}} & - Search: Breadth First Search: O(|E| + |V|) - E is number of edges - V is number of vertices \tn % Row Count 4 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.64241 cm} x{3.33459 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Depth First Search}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Definition}} & - An algorithm that searches a tree (or graph) by searching depth of the tree first, starting at the root. - It traverses left down a tree until it cannot go further. - Once it reaches the end of a branch it traverses back up trying the right child of nodes on that branch, and if possible left from the right children. - When 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). \tn % Row Count 22 (+ 22) % Row 1 \SetRowColor{white} {\bf{Details}} & - Optimal for searching a tree that is deeper than it is wide. - Uses a stack to push nodes onto. - Because a stack is LIFO it does not need to keep track of the nodes pointers and is therefore less memory intensive than breadth first search. - Once it cannot go further left it begins evaluating the stack. \tn % Row Count 34 (+ 12) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.64241 cm} x{3.33459 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Depth First Search (cont)}} \tn % Row 2 \SetRowColor{LightBackground} {\bf{Big-O efficiency}} & - Search: Depth First Search: O(|E| + |V|) - E is number of edges - V is number of vertices \tn % Row Count 4 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Breadth First Search Vs. Depth First Search}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{- The simple answer to this question is that it depends on the size and shape of the tree. \newline % Row Count 2 (+ 2) - For wide, shallow trees use Breadth First Search \newline % Row Count 4 (+ 2) - For deep, narrow trees use Depth First Search% Row Count 5 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Nuances:}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{- Because BFS uses queues to store information about the nodes and its children, it could use more memory than is available on your computer. (But you probably won't have to worry about this.) \newline % Row Count 4 (+ 4) - If using a DFS on a tree that is very deep you might go unnecessarily deep in the search. See {[}xkcd{]}(http://xkcd.com/761/) for more information. \newline % Row Count 7 (+ 3) - Breadth First Search tends to be a looping algorithm. \newline % Row Count 9 (+ 2) - Depth First Search tends to be a recursive algorithm.% Row Count 11 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Efficient Sorting Basics}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.64241 cm} x{3.33459 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Merge Sort}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Definition}} & - A comparison based sorting algorithm - Divides entire dataset into groups of at most two. - Compares each number one at a time, moving the smallest number to left of the pair. - Once all pairs sorted it then compares left most elements of the two leftmost pairs creating a sorted group of four with the smallest numbers on the left and the largest ones on the right. - This process is repeated until there is only one set. \tn % Row Count 17 (+ 17) % Row 1 \SetRowColor{white} {\bf{Details}} & - This is one of the most basic sorting algorithms. - Know that it divides all the data into as small possible sets then compares them. \tn % Row Count 23 (+ 6) % Row 2 \SetRowColor{LightBackground} {\bf{Big-O efficiency}} & - Best Case Sort: Merge Sort: O(n) - Average Case Sort: Merge Sort: O(n log n) - Worst Case Sort: Merge Sort: O(n log n) \tn % Row Count 28 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.64241 cm} x{3.33459 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Quicksort}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Definition}} & - A comparison based sorting algorithm - Divides entire dataset in half by selecting the average element and putting all smaller elements to the left of the average. - It repeats this process on the left side until it is comparing only two elements at which point the left side is sorted. - When the left side is finished sorting it performs the same operation on the right side. - Computer architecture favors the quicksort process. \tn % Row Count 17 (+ 17) % Row 1 \SetRowColor{white} {\bf{Details}} & - While it has the same Big O as (or worse in some cases) many other sorting algorithms it is often faster in practice than many other sorting algorithms, such as merge sort. - Know that it halves the data set by the average continuously until all the information is sorted. \tn % Row Count 28 (+ 11) % Row 2 \SetRowColor{LightBackground} {\bf{Big-O efficiency}} & - Best Case Sort: Merge Sort: O(n) - Average Case Sort: Merge Sort: O(n log n) - Worst Case Sort: Merge Sort: O(n\textasciicircum{}2) \tn % Row Count 33 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.64241 cm} x{3.33459 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Bubble Sort}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Definition}} & - A comparison based sorting algorithm - It iterates left to right comparing every couplet, moving the smaller element to the left. - It repeats this process until it no longer moves and element to the left. \tn % Row Count 9 (+ 9) % Row 1 \SetRowColor{white} {\bf{Details}} & - While it is very simple to implement, it is the least efficient of these three sorting methods. - Know that it moves one space to the right comparing two elements at a time and moving the smaller on to left. \tn % Row Count 18 (+ 9) % Row 2 \SetRowColor{LightBackground} {\bf{Big-O efficiency}} & - Best Case Sort: Merge Sort: O(n) - Average Case Sort: Merge Sort: O(n\textasciicircum{}2) - Worst Case Sort: Merge Sort: O(n\textasciicircum{}2) \tn % Row Count 23 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Merge Sort vs. QuickSort}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{- Quicksort is likely faster in practice. \newline % Row Count 1 (+ 1) - Merge Sort divides the set into the smallest possible groups immediately then reconstructs the incrementally as it sorts the groupings. \newline % Row Count 4 (+ 3) - Quicksort continually divides the set by the average, until the set is recursively sorted.% Row Count 6 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.09494 cm} x{3.88206 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Heap Sort}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{Definition:} & Sorts using a complete binary Tree. \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{Details:} & {\bf{ArrayList}} can be used to store a Heap \{\{nl\}\} For a node of {\bf{i}}:\{\{nl\}\} -Left child: 2{\bf{i}}+1 \{\{nl\}\} -Right child: 2{\bf{i}}+2\{\{nl\}\} -Parent: ({\bf{i}} - 1)/2 \tn % Row Count 8 (+ 6) % Row 2 \SetRowColor{LightBackground} Big-O: & O(nlogn) \tn % Row Count 9 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}