\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{alicanpayasli} \pdfinfo{ /Title (data-structes-2.pdf) /Creator (Cheatography) /Author (alicanpayasli) /Subject (Data Structes 2 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}{28A315} \definecolor{LightBackground}{HTML}{F1F9F0} \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{Data Structes 2 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{alicanpayasli} via \textcolor{DarkBackground}{\uline{cheatography.com/194447/cs/40853/}}} \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}alicanpayasli \\ \uline{cheatography.com/alicanpayasli} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 17th October, 2023.\\ Updated 17th October, 2023.\\ 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*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Binary Trees}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Tree data structure a hierarchical data model in which data is linked together to form a representative tree. \newline % Row Count 3 (+ 3) {\bf{Binary Trees}} are a type of tree where each node can have at most two children. \newline % Row Count 5 (+ 2) {\bf{Binary Trees Example Code:}} \newline % Row Count 6 (+ 1) typedef struct TreeNode \{ \newline % Row Count 7 (+ 1) int data; \newline % Row Count 8 (+ 1) struct TreeNode* left; \newline % Row Count 9 (+ 1) struct TreeNode* right; \newline % Row Count 10 (+ 1) \} TreeNode; \newline % Row Count 11 (+ 1) TreeNode* createNode(int x) \{ \newline % Row Count 12 (+ 1) TreeNode* t = (TreeNode*) \seqsplit{malloc(sizeof(TreeNode));} \newline % Row Count 14 (+ 2) t-\textgreater{}data = x; \newline % Row Count 15 (+ 1) t-\textgreater{}left = NULL; \newline % Row Count 16 (+ 1) t-\textgreater{}right = NULL; \newline % Row Count 17 (+ 1) return t; \newline % Row Count 18 (+ 1) \} \newline % Row Count 19 (+ 1) void printTree(TreeNode *root) \{ \newline % Row Count 20 (+ 1) if(root != NULL) \{ \newline % Row Count 21 (+ 1) printf("\%d ", root-\textgreater{}data); \newline % Row Count 22 (+ 1) printTree(root-\textgreater{}left); \newline % Row Count 23 (+ 1) printTree(root-\textgreater{}right); \newline % Row Count 24 (+ 1) \}\} \newline % Row Count 25 (+ 1) int main(void) \{ \newline % Row Count 26 (+ 1) TreeNode *root = createNode(5); \newline % Row Count 27 (+ 1) root-\textgreater{}left = createNode(6); \newline % Row Count 28 (+ 1) root-\textgreater{}right = createNode(8); \newline % Row Count 29 (+ 1) printTree(root); \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Binary Trees (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{getch(); \newline % Row Count 1 (+ 1) {\bf{Methods for navigating binary trees:}} \newline % Row Count 2 (+ 1) {\emph{Preorder: Root-Left-Right}} \newline % Row Count 3 (+ 1) {\emph{Inorder: Left-Root-Right}} \newline % Row Count 4 (+ 1) {\emph{Postorder: Left-Right-Root}} \newline % Row Count 5 (+ 1) {\bf{Methods for navigating Example Code:}} \newline % Row Count 6 (+ 1) void preorder(TreeNode *root) \{ \newline % Row Count 7 (+ 1) if(root != NULL) \{ \newline % Row Count 8 (+ 1) printf("\%d ", root-\textgreater{}data); \newline % Row Count 9 (+ 1) preorder(root-\textgreater{}left); \newline % Row Count 10 (+ 1) preorder(root-\textgreater{}right); \newline % Row Count 11 (+ 1) \}\} \newline % Row Count 12 (+ 1) void inorder(TreeNode *root) \{ \newline % Row Count 13 (+ 1) if(root != NULL) \{ \newline % Row Count 14 (+ 1) inorder(root-\textgreater{}left); \newline % Row Count 15 (+ 1) printf("\%d ", root-\textgreater{}data); \newline % Row Count 16 (+ 1) inorder(root-\textgreater{}right); \newline % Row Count 17 (+ 1) \}\} \newline % Row Count 18 (+ 1) void postorder(TreeNode *root) \{ \newline % Row Count 19 (+ 1) if(root != NULL) \{ \newline % Row Count 20 (+ 1) postorder(root-\textgreater{}left); \newline % Row Count 21 (+ 1) postorder(root-\textgreater{}right); \newline % Row Count 22 (+ 1) printf("\%d ", root-\textgreater{}data); \newline % Row Count 23 (+ 1) \}\}% Row Count 24 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Stack Trees}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{A stack tree is a data structure that allows to quickly find the smallest element in a data set. In this data structure, finding the smallest element, deleting the smallest element and adding elements to the tree can be done quickly.A binary tree that satisfies the following two properties is classified as a chunk tree data structure: \newline % Row Count 7 (+ 7) {\bf{1. Tree integrity: }}All levels of the tree, except the last level, must be complete in terms of the nodes they contain. The nodes in the last level of the tree must also be full from left to right. \newline % Row Count 12 (+ 5) {\bf{2. Heap property:}} The value of a node must be less than or equal to the values of its children.% Row Count 14 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Hash Tables}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{It is a data structure that stores data in the form of a key and data pair, allowing insertion, deletion and search operations to be performed very quickly. The general working logic is to store data in an N dimensional array and use a key consisting of a number or string to access the data. For a data to be stored, a key value is sent to the hash function, the value calculated by the function becomes the index where the data will be stored in the array. \newline % Row Count 10 (+ 10) {\bf{Example of a hash function that takes a numeric value as the key:}} \newline % Row Count 12 (+ 2) int hash(int key, int N) \{ \newline % Row Count 13 (+ 1) return key \% N; \newline % Row Count 14 (+ 1) \} \newline % Row Count 15 (+ 1) {\bf{Example of a hash function that takes a string value as the key:}} \newline % Row Count 17 (+ 2) int hash(char key{[}{]}, int M, int N) \{ \newline % Row Count 18 (+ 1) int i, sum = 0; \newline % Row Count 19 (+ 1) for(i=0; i\textless{}M; i++) \{ \newline % Row Count 20 (+ 1) sum += key{[}i{]}; \newline % Row Count 21 (+ 1) \} \newline % Row Count 22 (+ 1) return sum \% N; \newline % Row Count 23 (+ 1) \}% Row Count 24 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Binary Search Trees}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Binary search trees are a special type of binary tree. In this data structure, in addition to the binary tree properties, there is a size-minority relationship between the data in the nodes. \newline % Row Count 4 (+ 4) In order for a tree with binary tree properties to be a binary search tree, each node in the tree node must be greater than all values in its left subtree and less than or equal to all values in its right subtree. \newline % Row Count 9 (+ 5) {\bf{Binary search tree structure and node insertion function example:}} \newline % Row Count 11 (+ 2) typedef struct TreeNode \{ \newline % Row Count 12 (+ 1) int data; \newline % Row Count 13 (+ 1) struct TreeNode *left; \newline % Row Count 14 (+ 1) struct TreeNode *right; \newline % Row Count 15 (+ 1) \} TreeNode; \newline % Row Count 16 (+ 1) TreeNode* insertNode(TreeNode *node, int x) \{ \newline % Row Count 17 (+ 1) if(node == NULL) \{ \newline % Row Count 18 (+ 1) TreeNode *t = (TreeNode *) \seqsplit{malloc(sizeof(TreeNode));} \newline % Row Count 20 (+ 2) t -\textgreater{} data = x; \newline % Row Count 21 (+ 1) t -\textgreater{} left = t -\textgreater{} right = NULL; \newline % Row Count 22 (+ 1) return t; \newline % Row Count 23 (+ 1) \} \newline % Row Count 24 (+ 1) else if(x \textgreater{} node-\textgreater{}data) \{ \newline % Row Count 25 (+ 1) node-\textgreater{}right = insertNode(node-\textgreater{}right, x); \newline % Row Count 26 (+ 1) \} \newline % Row Count 27 (+ 1) else \{ \newline % Row Count 28 (+ 1) node-\textgreater{}left = insertNode(node-\textgreater{}left, x); \newline % Row Count 29 (+ 1) \}\} \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Binary Search Trees (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Example of node extraction function from binary search tree:}} \newline % Row Count 2 (+ 2) typedef struct TreeNode \{ \newline % Row Count 3 (+ 1) int data; \newline % Row Count 4 (+ 1) struct TreeNode *left; \newline % Row Count 5 (+ 1) struct TreeNode *right; \newline % Row Count 6 (+ 1) \} TreeNode; \newline % Row Count 7 (+ 1) TreeNode* findMin(TreeNode *node) \{ \newline % Row Count 8 (+ 1) if(node == NULL) \newline % Row Count 9 (+ 1) return NULL; \newline % Row Count 10 (+ 1) if(node-\textgreater{}left) \newline % Row Count 11 (+ 1) return findMin(node-\textgreater{}left); \newline % Row Count 12 (+ 1) else \newline % Row Count 13 (+ 1) return node; \newline % Row Count 14 (+ 1) \} \newline % Row Count 15 (+ 1) TreeNode* insertNode(TreeNode *node, int x) \{ \newline % Row Count 16 (+ 1) if(node == NULL) \{ \newline % Row Count 17 (+ 1) TreeNode *t = (TreeNode *) \seqsplit{malloc(sizeof(TreeNode));} \newline % Row Count 19 (+ 2) t -\textgreater{} data = x; \newline % Row Count 20 (+ 1) t -\textgreater{} left = t -\textgreater{} right = NULL; \newline % Row Count 21 (+ 1) return t; \newline % Row Count 22 (+ 1) \} \newline % Row Count 23 (+ 1) else if(x \textgreater{} node-\textgreater{}data) \{ \newline % Row Count 24 (+ 1) node-\textgreater{}right = insertNode(node-\textgreater{}right, x); \newline % Row Count 25 (+ 1) \} \newline % Row Count 26 (+ 1) else \{ \newline % Row Count 27 (+ 1) node-\textgreater{}left = insertNode(node-\textgreater{}left, x); \newline % Row Count 28 (+ 1) \}\} \newline % Row Count 29 (+ 1) TreeNode* deleteNode(TreeNode *node, int x) \{ \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Binary Search Trees (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{TreeNode *temp; \newline % Row Count 1 (+ 1) if(node == NULL) \{ \newline % Row Count 2 (+ 1) printf("No node found to delete.\textbackslash{}n"); \newline % Row Count 3 (+ 1) \} \newline % Row Count 4 (+ 1) else if(x \textless{} node-\textgreater{}data) \{ \newline % Row Count 5 (+ 1) node-\textgreater{}left = deleteNode(node-\textgreater{}left, x); \newline % Row Count 6 (+ 1) \} \newline % Row Count 7 (+ 1) else if(x \textgreater{} node-\textgreater{}data) \{ \newline % Row Count 8 (+ 1) node-\textgreater{}right = deleteNode(node-\textgreater{}right, x); \newline % Row Count 9 (+ 1) \} \newline % Row Count 10 (+ 1) else \{ \newline % Row Count 11 (+ 1) if(node-\textgreater{}right \&\& node-\textgreater{}left) \{ \newline % Row Count 12 (+ 1) temp = findMin(node-\textgreater{}right); \newline % Row Count 13 (+ 1) node -\textgreater{} data = temp-\textgreater{}data; \newline % Row Count 14 (+ 1) node -\textgreater{} right = deleteNode(node-\textgreater{}right, temp-\textgreater{}data); \newline % Row Count 16 (+ 2) \} \newline % Row Count 17 (+ 1) else \{ \newline % Row Count 18 (+ 1) temp = node; \newline % Row Count 19 (+ 1) if(node-\textgreater{}left == NULL) \newline % Row Count 20 (+ 1) node = node-\textgreater{}right; \newline % Row Count 21 (+ 1) else if(node-\textgreater{}right == NULL) \newline % Row Count 22 (+ 1) node = node-\textgreater{}left; \newline % Row Count 23 (+ 1) free(temp); \newline % Row Count 24 (+ 1) \}\} \newline % Row Count 25 (+ 1) return node; \newline % Row Count 26 (+ 1) \}% Row Count 27 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}