Red-Black_Tree/Schrick-Noah_Project-Writeup.tex
2022-04-20 22:24:49 -05:00

129 lines
7.2 KiB
TeX

\documentclass{article}
\usepackage{graphicx}
\graphicspath{ {./images/} }
\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage{indentfirst}
\setlength{\parskip}{\baselineskip}%
\title{CS 7353: Analysis of Algorithms Project: Red-Black Tree}
\author{Noah Schrick}
\date{April 21, 2022}
\begin{document}
\maketitle
\tableofcontents
\section{Problem Introduction}
\section{Program Platform and Submission Files}
This problem was solved using C++ on a Linux system.
Attached with the submission is a zip folder that contains:
\begin{itemize}
\item{A CMakeLists.txt file for compiling}
\item{An "images" folder that contains:}
\begin{enumerate}
\item{Various images included in this report}
\end{enumerate}
\item{A "src" folder that contains:}
\begin{enumerate}
\item{A Node.cpp and Node.h file for the Node class and associated functions}
\item{A Red-Black.cpp and Red-Black.h file for the Red-Black Tree class and
associated functions}
\item{The main file}
\end{enumerate}
\item{A "build" folder that contains:}
\begin{enumerate}
\item{A build.sh script to simplify the build process}
\item{A run.sh script to simplify running the program}
\item{Various CMake files}
\item{The compiled binaries for the program and associated libraries}
\end{enumerate}
\item{Various LaTeX files used in the generation of this report.}
\end{itemize}
This program offers no guarantee of functionality on other Operating Systems. Testing was only conducted on the local Linux machine.
\section{Programming Approach}
\subsection{Node Class}
This work approached the problem with a traditional method when working with tree structures by using a series of connected nodes. To achieve this, a ``Node" class was implemented. The Node class contains 5 private members: a pointer to the parent node, a pointer to the left child, a pointer to the right child, an integer key, and an integer color. Public class functions include auxiliary functions to get and set the pointers for the parent and children nodes, get and set the color value, and get the key. The Node class is constructed with an integer key value, the color is initialized to red, and the parent and children node pointers are initialized to null pointers using C++'s ``nullptr" keyword.
To minimize programmer error and to alleviate effort, an enum titled ``colors" was created, with black being set to 0, and red set to 1. Likewise, a color name character array was created, with ``black" in the first position, and red in the second. When getting and setting colors, rather than needing to remember (or look for) the value of red or black, the programmer can simply pass ``red" or ``black", which will then be converted to its proper integer value. Since Red-Black trees use bits to represent the color, this approach was preferred over the utilization of a string member for color.
For debugging purposes, a print function was also created. This function will print the key, color, parent key, left child key, and right child key of a given node.
\subsection{Red-Black Tree Class}
A red-black tree class was created to aid in the red-black tree functionality. It does not contain any data members other than a pointer to root node. This class primarily works to implement auxiliary functions needed to manage the tree, such as inserts, deletions, rotations, and re-colorings.
\subsubsection{Insert}
The insert function takes two arguments, both of which are pointers to nodes. The first argument is to aid in the recursion process, and begins as the pointer to the root node. The second argument is the pointer to the node that will be inserted. For the location of insertion, a recursion process is followed, where the key of the node to be inserted is compared to a current node. The tree is then traversed via recursion based on if the key is greater than or less than the current node. Respectively, the right child or left child of the current node is then passed. After the location is determined and the node is inserted, the cleanup function is called.
\subsubsection{Delete}
The delete function takes two arguments. The first is a pointer to a node, and similar to the insert process, this pointer is used to aid in a recursion process. The second argument is an integer that corresponds to the key of the node to delete. The recursion process is identical to that of the insertion process, with an additional check to see if the key is equal to the key of the current node. If the keys are equal, the deletion process is performed. For the deletion process itself, the approach taken was the successor method as discussed during lecture. The cleanup function was called after the node deletion was completed.
\subsubsection{Tree Cleanup}
The cleanup function was implemented by following the pseudocode of the three cases discussed during lecture, and the function takes a sole argument, which is a pointer to the node currently being examined. The function makes use of a while loop that checks if the current node and its parent are both red. Each case respectively calls the left and right rotate as necessary.
\subsubsection{Display}
Due to the saturated nature of binary tree displays, a novel approach for a print was unlikely. This program implemented an approach for a terminal-based print based on an existing method, and references its origin in the code itself. It makes use of recursion and a custom struct called ``Trunks", that has a pointer member to the previous trunk, and a string member that corresponds to a specific string for a left or right child.
\section{Results}
\subsection{Part 1.B: ``Tree 1"}
\begin{figure}[htp]
\centering
\includegraphics[width=\linewidth]{"./images/b_init_tree.png"}
\vspace{.2truein} \centerline{}
\caption{Part 1.B: Initial Tree After Key Insertions}
\label{fig:b_init}
\end{figure}
\begin{figure}[htp]
\centering
\includegraphics{"./images/b_del_12.png"}
\vspace{.2truein} \centerline{}
\caption{Part 1.B: Tree After Deleting Key 12}
\label{fig:b_12}
\end{figure}
\begin{figure}[htp]
\centering
\includegraphics{"./images/b_ins_32.png"}
\vspace{.2truein} \centerline{}
\caption{Part 1.B: Tree After Inserting Key 32}
\label{fig:b_32}
\end{figure}
\begin{figure}[htp]
\centering
\includegraphics{"./images/b_del_41.png"}
\vspace{.2truein} \centerline{}
\caption{Part 1.B: Tree After Deleting Key 41}
\label{fig:b_41}
\end{figure}
\subsection{Part 1.C: ``Tree 2"}
\begin{figure}[htp]
\includegraphics[width=\linewidth]{"./images/c_init_tree.png"}
\vspace{.2truein} \centerline{}
\caption{Part 1.C: Initial Tree After Key Insertions}
\label{fig:b_init}
\end{figure}
\begin{figure}[htp]
\includegraphics[width=\linewidth]{"./images/c_del_127.png"}
\vspace{.2truein} \centerline{}
\caption{Part 1.C: Tree After Deleting Key 127}
\label{fig:c_127}
\end{figure}
\begin{figure}[htp]
\includegraphics[width=\linewidth]{"./images/c_del_221.png"}
\vspace{.2truein} \centerline{}
\caption{Part 1.C: Tree After Deleting Key 221}
\label{fig:c_221}
\end{figure}
\section{Part 2: Red-Black Discussion}
\end{document}