This repository has been archived on 2022-05-18. You can view files and clone it, but cannot push or open issues or pull requests.
thesis/fi-pdflatex.tex
Matej Focko 585b609c4e
feat: add latest version of the thesis
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-01-23 17:02:17 +01:00

281 lines
12 KiB
TeX
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% I, the copyright holder of this work, release this work into the
%% public domain. This applies worldwide. In some countries this may
%% not be legally possible; if so: I grant anyone the right to use
%% this work for any purpose, without any conditions, unless such
%% conditions are required by law.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[
printed, %% The `digital` option enables the default options for the
%% digital version of a document. Replace with `printed`
%% to enable the default options for the printed version
%% of a document.
color, %% Uncomment these lines (by removing the %% at the
%% beginning) to use color in the digital version of your
%% document
table, %% The `table` option causes the coloring of tables.
%% Replace with `notable` to restore plain LaTeX tables.
oneside, %% The `twoside` option enables double-sided typesetting.
%% Use at least 120 g/m² paper to prevent show-through.
%% Replace with `oneside` to use one-sided typesetting;
%% use only if you dont have access to a double-sided
%% printer, or if one-sided typesetting is a formal
%% requirement at your faculty.
nolof, %% The `lof` option prints the List of Figures. Replace
%% with `nolof` to hide the List of Figures.
nolot, %% The `lot` option prints the List of Tables. Replace
%% with `nolot` to hide the List of Tables.
%% More options are listed in the user guide at
%% <http://mirrors.ctan.org/macros/latex/contrib/fithesis/guide/mu/fi.pdf>.
]{fithesis3}
%% The following section sets up the locales used in the thesis.
\usepackage[resetfonts]{cmap} %% We need to load the T2A font encoding
\usepackage[T1,T2A]{fontenc} %% to use the Cyrillic fonts with Russian texts.
\usepackage[
main=english, %% By using `czech` or `slovak` as the main locale
%% instead of `english`, you can typeset the thesis
%% in either Czech or Slovak, respectively.
english, german, russian, czech, slovak %% The additional keys allow
]{babel} %% foreign texts to be typeset as follows:
%%
%% \begin{otherlanguage}{german} ... \end{otherlanguage}
%% \begin{otherlanguage}{russian} ... \end{otherlanguage}
%% \begin{otherlanguage}{czech} ... \end{otherlanguage}
%% \begin{otherlanguage}{slovak} ... \end{otherlanguage}
%%
%% For non-Latin scripts, it may be necessary to load additional
%% fonts:
\usepackage{paratype}
\def\textrussian#1{{\usefont{T2A}{PTSerif-TLF}{m}{rm}#1}}
%%
%% The following section sets up the metadata of the thesis.
\thesissetup{
date = \the\year/\the\month/\the\day,
university = mu,
faculty = fi,
type = bc,
author = Matej Focko,
gender = m,
advisor = {prof. RNDr. Ivana Černá, CSc.},
title = {Rank-Balanced Trees},
TeXtitle = {Rank-Balanced Trees},
keywords = {algorithms, data structures, rank, trees, balanced trees, study material, visualization, ...},
TeXkeywords = {algorithms, data structures, rank, trees, balanced trees, study material, visualization, \ldots},
abstract = {%
In the thesis I demonstrate usage of rank for implementing balanced binary-search trees
and algorithms related to a specific rank-balanced tree, the weak AVL tree. First part
of the thesis consists of description of the rank-balanced tree followed by comparison
to other balanced trees that can be implemented using rank, diagrams and pseudo-codes related
to weak AVL tree. I also present an implementation of weak AVL tree in Python, tested with
property-based testing. The final part of the thesis is web-page that allows performing operations
on the weak AVL tree with animations and step-by-step walk-through of pseudo-code.
},
thanks = {%
\textit{TBD}
},
bib = example.bib,
%% Uncomment the following line (by removing the %% at the
%% beginning) and replace `assignment.pdf` with the filename
%% of your scanned thesis assignment.
%% assignment = assignment.pdf,
}
\usepackage{makeidx} %% The `makeidx` package contains
\makeindex %% helper commands for index typesetting.
%% These additional packages are used within the document:
\usepackage{paralist} %% Compact list environments
\usepackage{amsmath} %% Mathematics
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{url} %% Hyperlinks
\usepackage{markdown} %% Lightweight markup
\usepackage{tabularx} %% Tables
\usepackage{tabu}
\usepackage{booktabs}
\usepackage[vlined,longend,linesnumbered]{algorithm2e}
\usepackage{listings} %% Source code highlighting
\lstset{
basicstyle = \ttfamily,
identifierstyle = \color{black},
keywordstyle = \color{blue},
keywordstyle = {[2]\color{cyan}},
keywordstyle = {[3]\color{olive}},
stringstyle = \color{teal},
commentstyle = \itshape\color{magenta},
breaklines = true,
}
\usepackage{floatrow} %% Putting captions above tables
\floatsetup[table]{capposition=top}
\SetKwProg{Fn}{function}{ is}{end}
\SetKwProg{Proc}{procedure}{ is}{end}
\begin{document}
\chapter*{Introduction}
\addcontentsline{toc}{chapter}{Introduction}
\textit{TBD}
\chapter{Rank-balanced trees}
In comparison to nodes in binary search trees, nodes in rank-balanced trees contain one more information and that is \textit{rank}. Each type of tree that can be implemented using this representation, e.g. red-black, 2-3-4, AVL or WAVL, has specific set of rules that ensure the resulting tree is balanced.
\section{Terminology related to rank-balanced trees}
In the text and pseudo-codes I adopt these functions or properties:
\begin{itemize}
\item $r(x)$ or $x.rank$ returns rank of a node; in case of $r(x)$ there is a special case: $r(nil) = -1$
\item $parent(x)$ or $x.parent$ returns parent of a node; analogically for left and right children of a node
\item \textit{rank-difference} of \textit{x} is $r(parent(x)) - r(x)$
\item $x$ is an \textit{i-child} if its rank-difference is $i$
\item $x$ is an $(i, j)$ node if its left and right children have $i$ and $j$ rank-differences respectively
\end{itemize}
\section{Weak AVL trees}
Based on rank rules for implementing red-black tree and AVL tree, \textit{Haeupler et al.} present new rank rule: \\ \textbf{Weak AVL Rule}: All rank differences are 1 or 2, and every leaf has rank 0.
\chapter{Operations on weak AVL trees}
\section{Using bottom-up rebalancing}
\subsection{\texttt{insert}}
Inserting values into WAVL tree is equivalent to inserting values into regular binary-search tree, which is followed up by rebalancing that ensures rank rules hold.
Rebalancing of the tree is equivalent to rebalancing of AVL tree and is executed in a following way:
As a first step it is checked whether parent of a current (inserted) node is (0, 1) or (1, 0)-node, i.e. parent node was leaf node before. In that case parent node's rank is promoted and the process continues by parent node becoming the current node. (After the initial check we can encounter such nodes as a result of promoting the parent.)
Once the node does not have a parent (is root node) or is not (0, 1) or (1, 0) node, we might end up in a situation where the rank rule does not hold \textbf{and} we cannot fix it with promotion.
\begin{algorithm}
\Proc{$\texttt{insert}(tree, key)$}{
$insertedNode \gets Node(key)$\;
\If{$tree.root = nil$}{
$tree.root \gets insertedNode$\;
\Return\;
}
\BlankLine
$parent \gets \texttt{findParentNode}(key, tree.root)$\;
$insertedNode.parent \gets parent$\;
\BlankLine
\eIf{$key < parent.key$}{
$parent.left \gets insertedNode$\;
}{
$parent.right \gets insertedNode$\;
}
\BlankLine
$\texttt{bottomUpInsertRebalance}(tree, insertedNode)$\;
}
\caption{Insert operation on binary search tree}
\end{algorithm}
\begin{algorithm}
\Fn{$\texttt{findParentNode}(key, node)$}{
$childNode \gets node$\;
\BlankLine
\While{$childNode \neq nil$}{
$node \gets childNode$\;
\eIf{$key < node.key$}{
$childNode \gets node.left$\;
}{
$childNode \gets node.right$\;
}
}
\BlankLine
\Return{node}\;
}
\caption{Helper function that returns parent for newly inserted node}
\end{algorithm}
Bottom-up rebalancing after the insertion into WAVL tree is identical to AVL tree insertion:
\begin{algorithm}
\Proc{$\texttt{bottomUpInsertRebalance}(tree, node)$}{
\While{$node.parent \neq nil \land (node.parent\, is\, (0, 1)\, or\, (1, 0))$}{
$\texttt{promote}(node.parent)$\;
$node \gets node.parent$\;
}
\BlankLine
\lIf{$node.parent = nil$}{\Return}
\BlankLine
$rotatingAroundRoot \gets node.parent.parent = nil$\;
$newRoot \gets node.parent$\;
\BlankLine
\lIf{$node\, is\, not\, \text{0-child}$}{\Return}
\BlankLine
\eIf{$node = node.parent.left$}{
$newRoot \gets \texttt{fix0Child}(node, node.right, \texttt{rotateLeft}, \texttt{rotateRight})$\;
}{
$newRoot \gets \texttt{fix0Child}(node, node.left, \texttt{rotateRight}, \texttt{rotateLeft})$\;
}
\BlankLine
\lIf{$rotatingAroundRoot$}{$tree.root \gets newRoot$}
}
\caption{Algorithm containing bottom-up rebalancing after insertion}
\end{algorithm}
\begin{algorithm}
\Proc{$\texttt{fix0Child}(x, y, rotateToLeft, rotateToRight)$}{
$z \gets x.parent$\;
$newRoot \gets x.parent$\;
\BlankLine
\uIf{$y = nil \lor y\, is\, \text{2-child}$}{
$newRoot \gets rotateToRight(z)$\;
\BlankLine
$\texttt{demote}(z)$\;
}
\ElseIf{$y\, is\, \text{1-child}$}{
$rotateToLeft(x)$\;
$newRoot \gets rotateToRight(z)$\;
\BlankLine
$\texttt{promote}(y)$\;
$\texttt{demote}(x)$\;
$\texttt{demote}(z)$\;
}
\BlankLine
\Return{$newRoot$}\;
}
\caption{Generic algorithm for fixing 0-child after insertion}
\end{algorithm}
\subsection{\texttt{delete}}
\section{Using top-down rebalancing}
\subsection{\texttt{insert}}
\subsection{\texttt{delete}}
\section{Using rebalancing with promotion}
\subsection{\texttt{insert}}
\subsection{\texttt{delete}}
\section{Implementation of other balanced trees using rank}
To show that using rank is mostly an implementation detail, I will describe AVL and red-black tree implementation using rank.
\subsection{AVL tree}
\textbf{AVL Rule}: Every node is (1, 1) or (1, 2). \\
In case of AVL trees rank represents height.
\textit{TBD}
\subsection{Red-black tree}
\textbf{Red-Black Rule}: All rank differences are 0 or 1, and no parent of a 0-child is a 0-child. \\
In case of red-black trees rank represents number of black nodes on a path from the node to a leaf (excluding the node itself). Based on that we can discuss the \textit{Red-Black Rule} in detail:
\begin{enumerate}
\item \textit{All rank differences are 0 or 1} \\
in case the current node is black the rank difference must be 1, since we have one more black node on the path from the parent to the leaves than from the current node \\
in case the current node is red the rank difference must be 0, since from parent the count of black nodes on the path to leaves has not changed \\
and finally all other differences are invalid, since by adding a node to the beginning of a path to the leaf we can either add red node (0-child) or black node (1-child), i.e. there is one more black node on the path or not which implies the change can be only 0 or 1
\item \textit{no parent of a 0-child is a 0-child} \\
this part of the rule ensures there are no two consecutive red nodes (0-child node is equivalent to red node)
\end{enumerate}
\appendix %% Start the appendices.
\chapter{An appendix}
Here you can insert the appendices of your thesis.
\end{document}