Skip to content

Commit 85fc199

Browse files
committed
2019-2-14-b-tree: +b-tree-predecessor solution
1 parent bc683f9 commit 85fc199

File tree

7 files changed

+87
-8
lines changed

7 files changed

+87
-8
lines changed
104 KB
Binary file not shown.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
% b-tree-predecessor.tex
2+
3+
\begin{algorithm}[H]
4+
% \caption{Predecessor of a given key in B-tree.}
5+
% \label{b-tree-predecessor}
6+
\begin{algorithmic}[1]
7+
\Procedure{B-Tree-Predecessor}{$T, x, i$} \Comment{\purple{$x.key_{i}$ in B-tree $T$}}
8+
\If{$x.\leaf = 0$}
9+
\State \Return the rightmost key in $x.c_{i}$
10+
\ElsIf{$i \ge 2$} \Comment{$x.\leaf = 1$}
11+
\State \Return $(x, i-1)$
12+
\Else \Comment{\red{$x.\leaf = 1 \land i = 1$}}
13+
\State $y \gets x.p$
14+
\While{\red{$y \neq T.root \land x = y.c_{1}$}}
15+
\Comment{\blue{exit: $y = T.root \lor x \neq y.c_{1}$}}
16+
\State $x \gets y$
17+
\State $y \gets y.p$
18+
\EndWhile
19+
20+
\If{\red{$x = y.c_{1}$}} \Comment{\teal{$y = T.root \land x = y.c_{1}$}}
21+
\State \Return ``no predecessor''
22+
\Else \Comment{\teal{$x \neq y.c_{1}$}}
23+
\State $j \gets 2$
24+
\While{$y.c_{j} \neq x$}
25+
\State $j \gets j + 1$
26+
\EndWhile
27+
\State \Return $(y, j-1)$ \Comment{\purple{$x = y.c_{j}$}}
28+
\EndIf
29+
\EndIf
30+
\EndProcedure
31+
\end{algorithmic}
32+
\end{algorithm}
23.2 KB
Loading
27 KB
Loading

2019/2019-2/2019-2-14-b-tree/parts/b-tree-bst.tex

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,67 @@
1919
%%%%%%%%%%%%%%%
2020
\begin{frame}{}
2121
\begin{exampleblock}{Predecessor (TC 18.2-3)}
22-
Explain how to find the \red{predecessor of a given key} stored in a B-tree.
22+
Explain how to find the \red{predecessor of a given key $(x, i)$} stored in a B-tree.
2323
\end{exampleblock}
2424

25-
\fig{width = 0.75\textwidth}{figs/b-tree-example}
25+
\pause
26+
\fig{width = 1.00\textwidth}{figs/b-tree-37-char}
27+
\begin{center}
28+
\teal{B-tree with 37 characters.}
29+
\end{center}
30+
31+
\pause
32+
\[
33+
\red{x.\leaf = 0}
34+
\]
35+
36+
\pause
37+
\vspace{-0.30cm}
38+
\[
39+
\blue{H \qquad N} \qquad \teal{S \qquad V} \qquad \purple{Q}
40+
\]
41+
42+
\pause
43+
\vspace{0.20cm}
44+
\begin{center}
45+
find the \red{rightmost key} in $x.c_{i}$
46+
\end{center}
2647
\end{frame}
2748
%%%%%%%%%%%%%%%
2849

2950
%%%%%%%%%%%%%%%
3051
\begin{frame}{}
52+
\[
53+
(x, i)
54+
\]
55+
\fig{width = 1.00\textwidth}{figs/b-tree-37-char}
56+
57+
\[
58+
\red{x.\leaf = 1}
59+
\]
60+
61+
\vspace{-0.30cm}
62+
\[
63+
\blue{P \qquad U \qquad} \teal{T\qquad O \qquad} \purple{A}
64+
\]
65+
66+
\pause
67+
\[
68+
i \ge 2 \implies (x, i-1)
69+
\]
70+
\[
71+
i = 1 \implies \text{ find $(y, j)$ such that $x$ is the \red{leftmost key} in $y.c_{j + 1}$}
72+
\]
73+
74+
\pause
75+
\begin{center}
76+
$A$ is the \purple{only} key which has no predecessor.
77+
\end{center}
3178
\end{frame}
3279
%%%%%%%%%%%%%%%
3380

3481
%%%%%%%%%%%%%%%
3582
\begin{frame}{}
83+
\input{algs/b-tree-predecessor}
3684
\end{frame}
3785
%%%%%%%%%%%%%%%

2019/2019-2/2019-2-14-b-tree/parts/b-tree-insertion.tex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@
9797
\Big( \left\lfloor \frac{n - T_{i}}{2^{i}} \right\rfloor + 2 \Big) \\}
9898
\onslide<5->{&= 1 + \sum_{i = 1}^{\infty} \teal{[T_{i} \le n]}
9999
\Big( \left\lfloor \frac{n - 2^{i+1} - i + 1}{2^{i}} \right\rfloor \Big) \\}
100-
\onslide<6->{&= 1 + \sum_{i = 1}^{2^{i+1} + i - 1 \le n}
100+
\onslide<6->{&= 1 + \sum_{\substack{i \ge 1 \\ 2^{i+1} + i - 1 \le n}}
101101
\left\lfloor \frac{n - i + 1}{2^{i}} \right\rfloor \\}
102-
\onslide<7->{&= 1 + \sum_{\red{i = 0}}^{2^{i+2} + i \le n}
102+
\onslide<7->{&= 1 + \sum_{\substack{\red{i \ge 0} \\ 2^{i+2} + i \le n}}
103103
\left\lfloor \frac{n - i}{2^{i + 1}} \right\rfloor}
104104
\end{align*}
105105
\end{frame}

2019/2019-2/2019-2-14-b-tree/preamble.tex

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,11 @@
6666
\end{figure}
6767
}
6868

69-
\usepackage{tikz}
69+
\usepackage{tikz, adjustbox}
7070
\usetikzlibrary{positioning, mindmap, shadows, calc}
7171

72-
\newcommand{\titletext}{2-11 Heapsort}
73-
\usepackage{media9}
74-
\usepackage{multimedia}
72+
\newcommand{\titletext}{2-14 B-Trees}
73+
\newcommand{\leaf}{\textsl{leaf}}
7574

7675
\newcommand{\thankyou}{
7776
\begin{frame}[noframenumbering]{}

0 commit comments

Comments
 (0)