Final Report

This commit is contained in:
Noah L. Schrick 2022-04-21 00:26:09 -05:00
parent c8de715ebe
commit c883bafa85
9 changed files with 112 additions and 66 deletions

View File

@ -4,26 +4,28 @@
\@writefile{toc}{\contentsline {section}{\numberline {3}Programming Approach}{3}{}\protected@file@percent }
\@writefile{toc}{\contentsline {subsection}{\numberline {3.1}Node Class}{3}{}\protected@file@percent }
\@writefile{toc}{\contentsline {subsection}{\numberline {3.2}Red-Black Tree Class}{3}{}\protected@file@percent }
\@writefile{toc}{\contentsline {subsubsection}{\numberline {3.2.1}Constructing the Problem}{3}{}\protected@file@percent }
\@writefile{toc}{\contentsline {subsubsection}{\numberline {3.2.2}Generating the Solution}{3}{}\protected@file@percent }
\@writefile{toc}{\contentsline {subsubsection}{\numberline {3.2.3}Printing the solution}{3}{}\protected@file@percent }
\newlabel{sec:print}{{3.2.3}{3}}
\@writefile{toc}{\contentsline {section}{\numberline {4}Results}{3}{}\protected@file@percent }
\@writefile{toc}{\contentsline {subsection}{\numberline {4.1}Part 1.B: ``Tree 1"}{3}{}\protected@file@percent }
\@writefile{lof}{\contentsline {figure}{\numberline {1}{\ignorespaces Part 1.B: Initial Tree After Key Insertions}}{3}{}\protected@file@percent }
\newlabel{fig:b_init}{{1}{3}}
\@writefile{lof}{\contentsline {figure}{\numberline {2}{\ignorespaces Part 1.B: Tree After Deleting Key 12}}{4}{}\protected@file@percent }
\newlabel{fig:b_12}{{2}{4}}
\@writefile{lof}{\contentsline {figure}{\numberline {3}{\ignorespaces Part 1.B: Tree After Inserting Key 32}}{4}{}\protected@file@percent }
\newlabel{fig:b_32}{{3}{4}}
\@writefile{lof}{\contentsline {figure}{\numberline {4}{\ignorespaces Part 1.B: Tree After Deleting Key 41}}{5}{}\protected@file@percent }
\newlabel{fig:b_41}{{4}{5}}
\@writefile{toc}{\contentsline {subsection}{\numberline {4.2}Part 1.C: ``Tree 2"}{5}{}\protected@file@percent }
\@writefile{toc}{\contentsline {subsubsection}{\numberline {3.2.1}Insert}{4}{}\protected@file@percent }
\@writefile{toc}{\contentsline {subsubsection}{\numberline {3.2.2}Delete}{4}{}\protected@file@percent }
\@writefile{toc}{\contentsline {subsubsection}{\numberline {3.2.3}Tree Cleanup}{4}{}\protected@file@percent }
\@writefile{toc}{\contentsline {subsubsection}{\numberline {3.2.4}Display}{4}{}\protected@file@percent }
\@writefile{toc}{\contentsline {section}{\numberline {4}Results}{4}{}\protected@file@percent }
\@writefile{lof}{\contentsline {figure}{\numberline {1}{\ignorespaces Part 1.B: Initial Tree After Key Insertions}}{5}{}\protected@file@percent }
\newlabel{fig:b_init}{{1}{5}}
\@writefile{toc}{\contentsline {section}{\numberline {5}Part 2: Red-Black Discussion}{5}{}\protected@file@percent }
\@writefile{lof}{\contentsline {figure}{\numberline {5}{\ignorespaces Part 1.C: Initial Tree After Key Insertions}}{6}{}\protected@file@percent }
\newlabel{fig:b_init}{{5}{6}}
\@writefile{lof}{\contentsline {figure}{\numberline {6}{\ignorespaces Part 1.C: Tree After Deleting Key 127}}{7}{}\protected@file@percent }
\newlabel{fig:c_127}{{6}{7}}
\@writefile{lof}{\contentsline {figure}{\numberline {7}{\ignorespaces Part 1.C: Tree After Deleting Key 221}}{8}{}\protected@file@percent }
\newlabel{fig:c_221}{{7}{8}}
\gdef \@abspage@last{8}
\@writefile{toc}{\contentsline {subsection}{\numberline {5.1}Question 2.1}{5}{}\protected@file@percent }
\@writefile{lof}{\contentsline {figure}{\numberline {2}{\ignorespaces Part 1.B: Tree After Deleting Key 12}}{6}{}\protected@file@percent }
\newlabel{fig:b_12}{{2}{6}}
\@writefile{lof}{\contentsline {figure}{\numberline {3}{\ignorespaces Part 1.B: Tree After Inserting Key 32}}{6}{}\protected@file@percent }
\newlabel{fig:b_32}{{3}{6}}
\@writefile{lof}{\contentsline {figure}{\numberline {4}{\ignorespaces Part 1.B: Tree After Deleting Key 41}}{7}{}\protected@file@percent }
\newlabel{fig:b_41}{{4}{7}}
\@writefile{toc}{\contentsline {subsection}{\numberline {5.2}Question 2.2}{7}{}\protected@file@percent }
\@writefile{lof}{\contentsline {figure}{\numberline {5}{\ignorespaces Part 1.C: Initial Tree After Key Insertions}}{8}{}\protected@file@percent }
\newlabel{fig:b_init}{{5}{8}}
\@writefile{lof}{\contentsline {figure}{\numberline {6}{\ignorespaces Part 1.C: Tree After Deleting Key 127}}{9}{}\protected@file@percent }
\newlabel{fig:c_127}{{6}{9}}
\@writefile{lof}{\contentsline {figure}{\numberline {7}{\ignorespaces Part 1.C: Tree After Deleting Key 221}}{10}{}\protected@file@percent }
\newlabel{fig:c_221}{{7}{10}}
\@writefile{lof}{\contentsline {figure}{\numberline {8}{\ignorespaces Part 2.1: Example Tree}}{11}{}\protected@file@percent }
\newlabel{fig:2_1}{{8}{11}}
\gdef \@abspage@last{11}

View File

@ -1,4 +1,4 @@
This is pdfTeX, Version 3.141592653-2.6-1.40.23 (TeX Live 2021/Arch Linux) (preloaded format=pdflatex 2022.4.17) 20 APR 2022 19:19
This is pdfTeX, Version 3.141592653-2.6-1.40.23 (TeX Live 2021/Arch Linux) (preloaded format=pdflatex 2022.4.17) 21 APR 2022 00:25
entering extended mode
restricted \write18 enabled.
%&-line parsing enabled.
@ -129,78 +129,87 @@ LaTeX Font Info: External font `cmex10' loaded for size
[1
{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}]
<./images/b_init_tree.png, id=11, 441.65pt x 241.90375pt>
{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}] [2] [3]
<./images/b_init_tree.png, id=21, 441.65pt x 241.90375pt>
File: ./images/b_init_tree.png Graphic file (type png)
<use ./images/b_init_tree.png>
Package pdftex.def Info: ./images/b_init_tree.png used on input line 64.
Package pdftex.def Info: ./images/b_init_tree.png used on input line 75.
(pdftex.def) Requested size: 345.0pt x 188.97575pt.
[2]
<./images/b_del_12.png, id=16, 230.8625pt x 179.67125pt>
[4]
<./images/b_del_12.png, id=25, 230.8625pt x 179.67125pt>
File: ./images/b_del_12.png Graphic file (type png)
<use ./images/b_del_12.png>
Package pdftex.def Info: ./images/b_del_12.png used on input line 72.
Package pdftex.def Info: ./images/b_del_12.png used on input line 83.
(pdftex.def) Requested size: 230.86192pt x 179.67079pt.
<./images/b_ins_32.png, id=17, 268.00125pt x 193.72375pt>
<./images/b_ins_32.png, id=26, 268.00125pt x 193.72375pt>
File: ./images/b_ins_32.png Graphic file (type png)
<use ./images/b_ins_32.png>
Package pdftex.def Info: ./images/b_ins_32.png used on input line 80.
Package pdftex.def Info: ./images/b_ins_32.png used on input line 91.
(pdftex.def) Requested size: 268.00058pt x 193.72327pt.
<./images/b_del_41.png, id=18, 276.03125pt x 173.64874pt>
<./images/b_del_41.png, id=27, 276.03125pt x 173.64874pt>
File: ./images/b_del_41.png Graphic file (type png)
<use ./images/b_del_41.png>
Package pdftex.def Info: ./images/b_del_41.png used on input line 88.
Package pdftex.def Info: ./images/b_del_41.png used on input line 99.
(pdftex.def) Requested size: 276.03056pt x 173.64832pt.
<./images/c_init_tree.png, id=19, 410.53375pt x 654.445pt>
<./images/c_init_tree.png, id=28, 410.53375pt x 654.445pt>
File: ./images/c_init_tree.png Graphic file (type png)
<use ./images/c_init_tree.png>
Package pdftex.def Info: ./images/c_init_tree.png used on input line 96.
Package pdftex.def Info: ./images/c_init_tree.png used on input line 106.
(pdftex.def) Requested size: 345.0pt x 549.96973pt.
LaTeX Warning: Float too large for page by 50.36794pt on input line 100.
LaTeX Warning: Float too large for page by 50.36794pt on input line 110.
<./images/c_del_127.png, id=20, 392.46625pt x 637.38126pt>
<./images/c_del_127.png, id=29, 392.46625pt x 637.38126pt>
File: ./images/c_del_127.png Graphic file (type png)
<use ./images/c_del_127.png>
Package pdftex.def Info: ./images/c_del_127.png used on input line 103.
Package pdftex.def Info: ./images/c_del_127.png used on input line 113.
(pdftex.def) Requested size: 345.0pt x 560.33316pt.
LaTeX Warning: Float too large for page by 60.73137pt on input line 107.
LaTeX Warning: Float too large for page by 60.73137pt on input line 117.
<./images/c_del_221.png, id=21, 374.39874pt x 611.28375pt>
<./images/c_del_221.png, id=30, 374.39874pt x 611.28375pt>
File: ./images/c_del_221.png Graphic file (type png)
<use ./images/c_del_221.png>
Package pdftex.def Info: ./images/c_del_221.png used on input line 110.
Package pdftex.def Info: ./images/c_del_221.png used on input line 120.
(pdftex.def) Requested size: 345.0pt x 563.3113pt.
LaTeX Warning: Float too large for page by 63.7095pt on input line 114.
LaTeX Warning: Float too large for page by 63.7095pt on input line 124.
[3 <./images/b_init_tree.png>] [4 <./images/b_del_12.png> <./images/b_ins_32.pn
g>] [5 <./images/b_del_41.png>] [6 <./images/c_init_tree.png>] [7 <./images/c_d
el_127.png>] [8 <./images/c_del_221.png>] (./Schrick-Noah_Project-Writeup.aux)
<./images/2_1.png, id=31, 429.605pt x 586.19pt>
File: ./images/2_1.png Graphic file (type png)
<use ./images/2_1.png>
Package pdftex.def Info: ./images/2_1.png used on input line 132.
(pdftex.def) Requested size: 345.0pt x 470.75049pt.
[5 <./images/b_init_tree.png>] [6 <./images/b_del_12.png> <./images/b_ins_32.pn
g>] [7 <./images/b_del_41.png>] [8 <./images/c_init_tree.png>] [9 <./images/c_d
el_127.png>] [10 <./images/c_del_221.png>] [11 <./images/2_1.png>]
(./Schrick-Noah_Project-Writeup.aux)
LaTeX Warning: There were multiply-defined labels.
)
Here is how much of TeX's memory you used:
1480 strings out of 478276
25161 string characters out of 5853013
317990 words of memory out of 5000000
19701 multiletter control sequences out of 15000+600000
473849 words of font info for 43 fonts, out of 8000000 for 9000
1490 strings out of 478276
25344 string characters out of 5853013
318990 words of memory out of 5000000
19709 multiletter control sequences out of 15000+600000
474195 words of font info for 44 fonts, out of 8000000 for 9000
1141 hyphenation exceptions out of 8191
55i,6n,62p,229b,233s stack positions out of 5000i,500n,10000p,200000b,80000s
55i,7n,62p,828b,235s stack positions out of 5000i,500n,10000p,200000b,80000s
{/usr/share/texmf-dist/fonts/enc/dvips/cm-super/cm-super-ts1.enc}</usr/share/
texmf-dist/fonts/type1/public/amsfonts/cm/cmbx10.pfb></usr/share/texmf-dist/fon
ts/type1/public/amsfonts/cm/cmbx12.pfb></usr/share/texmf-dist/fonts/type1/publi
c/amsfonts/cm/cmr10.pfb></usr/share/texmf-dist/fonts/type1/public/amsfonts/cm/c
mr12.pfb></usr/share/texmf-dist/fonts/type1/public/amsfonts/cm/cmr17.pfb></usr/
share/texmf-dist/fonts/type1/public/cm-super/sfrm1000.pfb>
Output written on Schrick-Noah_Project-Writeup.pdf (8 pages, 167396 bytes).
c/amsfonts/cm/cmmi10.pfb></usr/share/texmf-dist/fonts/type1/public/amsfonts/cm/
cmr10.pfb></usr/share/texmf-dist/fonts/type1/public/amsfonts/cm/cmr12.pfb></usr
/share/texmf-dist/fonts/type1/public/amsfonts/cm/cmr17.pfb></usr/share/texmf-di
st/fonts/type1/public/amsfonts/cm/cmsy10.pfb></usr/share/texmf-dist/fonts/type1
/public/amsfonts/cm/cmti10.pfb></usr/share/texmf-dist/fonts/type1/public/cm-sup
er/sfrm1000.pfb>
Output written on Schrick-Noah_Project-Writeup.pdf (11 pages, 223598 bytes).
PDF statistics:
69 PDF objects out of 1000 (max. 8388607)
39 compressed objects within 1 object stream
94 PDF objects out of 1000 (max. 8388607)
54 compressed objects within 1 object stream
0 named destinations out of 1000 (max. 500000)
36 words of extra memory for PDF output out of 10000 (max. 10000000)
41 words of extra memory for PDF output out of 10000 (max. 10000000)

Binary file not shown.

View File

@ -16,6 +16,7 @@
\tableofcontents
\section{Problem Introduction}
Red-Black trees are binary search trees that contain an additional one-bit field that denotes the color of a node as either ``red" or ``black". These trees have additional structural requirements that must be followed, as well as the traditional binary tree requirements. Red-Black trees are also balanced, so a height is guaranteed as a function of \textit{n}. A full description of Red-Black trees were discussed in lecture, and the algorithms textbook by Cormen also gives a full work-through of the data structure. The main advantage of Red-Black trees is that the worst-case for searching, inserting, and deleting is $\mathcal{O}({log(n)})$. For this project, an implementation of Red-Black is provided in C++, with results shown for given problems.
\section{Program Platform and Submission Files}
This problem was solved using C++ on a Linux system.
@ -68,7 +69,7 @@ The cleanup function was implemented by following the pseudocode of the three ca
\subsubsection{Display}
\section{Results}
\subsection{Part 1.B: ``Tree 1"}
As a note, the ``.'' represents the right child, and the ` represents the left child. The terminal-based print does appear to merge together, so in some subtrees it may appear that there are two red nodes next to each other. However, despite this first-glance look, it is verifiable that there are no two red nodes next to each other.
\begin{figure}[htp]
\centering
\includegraphics[width=\linewidth]{"./images/b_init_tree.png"}
@ -101,7 +102,6 @@ The cleanup function was implemented by following the pseudocode of the three ca
\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{}
@ -124,5 +124,18 @@ The cleanup function was implemented by following the pseudocode of the three ca
\end{figure}
\section{Part 2: Red-Black Discussion}
\subsection{Question 2.1}
When inserting a node into a red-black tree and then immediately deleting the same node, the red-black tree before the first insertion is not necessarily the same as the tree after the deletion. An example of this can be seen in Figure \ref{fig:2_1}. When inserting node 100 into the tree, it gets inserted as the right child of node 28. Since both node 28 and node 100 are red, it causes a rotation in the tree. After the node is deleted, the tree does not get rotated back, so the tree now has node 15 as the left child of node 28, instead of node 28 being the right child of node 15.
\begin{figure}[htp]
\centering
\includegraphics[width=\linewidth]{"./images/2_1.png"}
\vspace{.2truein} \centerline{}
\caption{Part 2.1: Example Tree}
\label{fig:2_1}
\end{figure}
\subsection{Question 2.2}
Maintaining the black height of nodes during the insertion process does not adversely affect the asymptotic performance of the process. Both Case 1 and Case 2 do not change the black height, so no work must be performed in these cases. In Case 1, black height is also not directly changed. In Case 1, the black node may shift down, but the height itself does not change. However, as a result of Case 1, we may color root as red, which then gets corrected to black. This part does change the black height. However, recoloring is constant time, and the black height only needs to be altered for the nearest neighbors. Similar to the rotation process, there is no need to walk down the entirety of the tree; only the nearest children may need an adjustment, which is constant time.
\end{document}

View File

@ -3,10 +3,11 @@
\contentsline {section}{\numberline {3}Programming Approach}{3}{}%
\contentsline {subsection}{\numberline {3.1}Node Class}{3}{}%
\contentsline {subsection}{\numberline {3.2}Red-Black Tree Class}{3}{}%
\contentsline {subsubsection}{\numberline {3.2.1}Constructing the Problem}{3}{}%
\contentsline {subsubsection}{\numberline {3.2.2}Generating the Solution}{3}{}%
\contentsline {subsubsection}{\numberline {3.2.3}Printing the solution}{3}{}%
\contentsline {section}{\numberline {4}Results}{3}{}%
\contentsline {subsection}{\numberline {4.1}Part 1.B: ``Tree 1"}{3}{}%
\contentsline {subsection}{\numberline {4.2}Part 1.C: ``Tree 2"}{5}{}%
\contentsline {subsubsection}{\numberline {3.2.1}Insert}{4}{}%
\contentsline {subsubsection}{\numberline {3.2.2}Delete}{4}{}%
\contentsline {subsubsection}{\numberline {3.2.3}Tree Cleanup}{4}{}%
\contentsline {subsubsection}{\numberline {3.2.4}Display}{4}{}%
\contentsline {section}{\numberline {4}Results}{4}{}%
\contentsline {section}{\numberline {5}Part 2: Red-Black Discussion}{5}{}%
\contentsline {subsection}{\numberline {5.1}Question 2.1}{5}{}%
\contentsline {subsection}{\numberline {5.2}Question 2.2}{7}{}%

Binary file not shown.

BIN
images/2_1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

View File

@ -59,4 +59,25 @@ int main(int argc, char *argv[])
tree2.del(tree2.get_root(), 221);
tree2.display(tree2.get_root(), nullptr, false);
std::cout << "" << std::endl;
RB tree3 = RB();
std::vector<int> part_2 {9, 12, 15, 28, 10};
for (int k : part_2){
Node* newnode = new Node(k);
tree3.ins(tree3.get_root(), newnode);
}
std::cout << "Initial Part 2.1 Tree:" << std::endl;
tree3.display(tree3.get_root(), nullptr, false);
std::cout << "" << std::endl;
std::cout << "Inserting 100 into Tree 3" << std::endl;
Node* newnode1 = new Node(100);
tree3.ins(tree3.get_root(), newnode1);
tree3.display(tree3.get_root(), nullptr, false);
std::cout << "" << std::endl;
std::cout << "Removing 100" << std::endl;
tree3.del(tree3.get_root(), 100);
tree3.display(tree3.get_root(), nullptr, false);
}