Notes on B-tree insertions.

main
Joshua Potter 2024-08-10 14:31:57 -06:00
parent 23635c0326
commit b4327a288c
9 changed files with 210 additions and 22 deletions

View File

@ -186,7 +186,12 @@
"complete-tree.png", "complete-tree.png",
"non-complete-tree.png", "non-complete-tree.png",
"relation-ordering-example.png", "relation-ordering-example.png",
"infinite-cartesian-product.png" "infinite-cartesian-product.png",
"b-tree-full-node.png",
"b-tree-split-node.png",
"b-tree-initial.png",
"b-tree-inserted-b.png",
"b-tree-inserted-q.png"
], ],
"File Hashes": { "File Hashes": {
"algorithms/index.md": "3ac071354e55242919cc574eb43de6f8", "algorithms/index.md": "3ac071354e55242919cc574eb43de6f8",
@ -720,8 +725,10 @@
"_journal/2024-08/2024-08-07.md": "119c052f4109a3e098d825b771af89de", "_journal/2024-08/2024-08-07.md": "119c052f4109a3e098d825b771af89de",
"_journal/2024-08-09.md": "2ce3e0c468f51750d8ad86a19bcc3264", "_journal/2024-08-09.md": "2ce3e0c468f51750d8ad86a19bcc3264",
"_journal/2024-08/2024-08-08.md": "b8211a4c576ff594217e2e9cae9396c0", "_journal/2024-08/2024-08-08.md": "b8211a4c576ff594217e2e9cae9396c0",
"data-structures/b-tree.md": "b6085d090b65e457e29d6d36cf4b226a", "data-structures/b-tree.md": "5f5e6f483ce6beca9fb3a4cd96264e22",
"data-structures/binary-tree.md": "67b0b5b9688faa205983993fe507079a" "data-structures/binary-tree.md": "67b0b5b9688faa205983993fe507079a",
"_journal/2024-08-10.md": "9e4c7c2c0fd9e855d5cf6b5eff18b111",
"_journal/2024-08/2024-08-09.md": "2ce3e0c468f51750d8ad86a19bcc3264"
}, },
"fields_dict": { "fields_dict": {
"Basic": [ "Basic": [

View File

@ -0,0 +1,12 @@
---
title: "2024-08-10"
---
- [x] Anki Flashcards
- [x] KoL
- [ ] OGS
- [ ] Sheet Music (10 min.)
- [ ] Korean (Read 1 Story)
* Read through Chapter 3 of "Modern C".
* Notes on B-tree insertions.

View File

@ -18,6 +18,30 @@ A **B-tree of order $m$** is a tree that satisfies the following properties:
The above is a modification of Knuth's definition in his "Art of Computer Programming" that defines leaves of the tree more consistently with how I use the term elsewhere. It also pulls in concepts from CLRS (such as keys needing to be sorted within nodes). The above is a modification of Knuth's definition in his "Art of Computer Programming" that defines leaves of the tree more consistently with how I use the term elsewhere. It also pulls in concepts from CLRS (such as keys needing to be sorted within nodes).
%%ANKI
Basic
Why is a B-tree named the way it is?
Back: There is no definitive answer.
Reference: Donald Ervin Knuth, _Art of Computer Programming, 3: Sorting and Searching_, 2. ed., 34. (Reading, Mass: Addison-Wesley, 1995).
<!--ID: 1723289256280-->
END%%
%%ANKI
Basic
What was the motivation behind the development of the B-tree?
Back: To find a data structure for efficient search that minimizes disk accesses.
Reference: Donald Ervin Knuth, _Art of Computer Programming, 3: Sorting and Searching_, 2. ed., 34. (Reading, Mass: Addison-Wesley, 1995).
<!--ID: 1723289256283-->
END%%
%%ANKI
Basic
How is the order of a B-tree typically determined?
Back: By choosing a value that best aligns with the size of a memory block.
Reference: Donald Ervin Knuth, _Art of Computer Programming, 3: Sorting and Searching_, 2. ed., 34. (Reading, Mass: Addison-Wesley, 1995).
<!--ID: 1723289256285-->
END%%
%%ANKI %%ANKI
Basic Basic
What hyperparameter is used to define a B-tree? What hyperparameter is used to define a B-tree?
@ -106,14 +130,6 @@ Reference: Donald Ervin Knuth, _Art of Computer Programming, 3: Sorting and Sear
<!--ID: 1723211542052--> <!--ID: 1723211542052-->
END%% END%%
%%ANKI
Basic
*When* does a B-tree gain height?
Back: When the root node is split.
Reference: Donald Ervin Knuth, _Art of Computer Programming, 3: Sorting and Searching_, 2. ed., 34. (Reading, Mass: Addison-Wesley, 1995).
<!--ID: 1723211542058-->
END%%
%%ANKI %%ANKI
Basic Basic
Consider a B-tree of order $7$. How many children $c$ can each non-root node have? Consider a B-tree of order $7$. How many children $c$ can each non-root node have?
@ -172,26 +188,179 @@ END%%
%%ANKI %%ANKI
Basic Basic
Why is a B-tree named the way it is? What is the search runtime of a B-tree of order $m$ and height $h$?
Back: There is no definitive answer. Back: $O(mh)$
Reference: Donald Ervin Knuth, _Art of Computer Programming, 3: Sorting and Searching_, 2. ed., 34. (Reading, Mass: Addison-Wesley, 1995). Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1723289256280--> <!--ID: 1723321489725-->
END%% END%%
%%ANKI %%ANKI
Basic Basic
What was the motivation behind the development of the B-tree? *Why* does searching a B-tree of order $m$ and height $h$ take $O(mh)$ time?
Back: To find a data structure for efficient search that minimizes disk accesses. Back: Each node may have $m - 1$ keys, and we may check $h$ nodes.
Reference: Donald Ervin Knuth, _Art of Computer Programming, 3: Sorting and Searching_, 2. ed., 34. (Reading, Mass: Addison-Wesley, 1995). Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1723289256283--> <!--ID: 1723321489726-->
END%% END%%
%%ANKI %%ANKI
Basic Basic
How is the order of a B-tree typically determined? How many disk accesses are performed when searching a B-tree of order $m$ and height $h$?
Back: By choosing a value that best aligns with the size of a memory block. Back: $O(h)$
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1723321489727-->
END%%
%%ANKI
Basic
*Why* does the number of disk accesses when searching a B-tree of height $h$ equal $O(h)$?
Back: The size of each node presumably corresponds to a block of memory.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1723321489728-->
END%%
%%ANKI
Basic
What is the search runtime of a B-tree of order $m$ containing $n$ keys?
Back: $O(m\log_m{n})$
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1723321489729-->
END%%
%%ANKI
Basic
*Why* does searching a B-tree of order $m$ containing $n$ keys take $O(m\log_m{n})$ time?
Back: Each node may have $m - 1$ keys, and we may check $\log_m{n}$ nodes.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1723321489730-->
END%%
%%ANKI
Basic
How many disk accesses are performed when searching a B-tree of order $m$ containing $n$ keys?
Back: $O(\log_m{n})$
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1723321489731-->
END%%
%%ANKI
Basic
*Why* does the number of disk accesses when searching a B-tree of order $m$ containing $n$ keys equal $O(\log_m{n})$?
Back: The size of each node presumably corresponds to a block of memory.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1723321489732-->
END%%
## Insertions
A node of a B-tree of order $m$ is considered full when it has $m$ children (or equivalently $m - 1$ keys). Insertion operates analagously to a binary tree. If the node the key was inserted into then contains $m$ keys, split the node into two and place the median into the original parent node. This action may propagate upwards. If the root node becomes full, create a new root containing the median of the original root.
%%ANKI
Cloze
A node in a B-tree of order $m$ is considered full when it has {$m - 1$} keys.
Reference: Donald Ervin Knuth, _Art of Computer Programming, 3: Sorting and Searching_, 2. ed., 34. (Reading, Mass: Addison-Wesley, 1995). Reference: Donald Ervin Knuth, _Art of Computer Programming, 3: Sorting and Searching_, 2. ed., 34. (Reading, Mass: Addison-Wesley, 1995).
<!--ID: 1723289256285--> <!--ID: 1723321489733-->
END%%
%%ANKI
Cloze
A node in a B-tree of order $m$ is considered full when it has {$m$} children.
Reference: Donald Ervin Knuth, _Art of Computer Programming, 3: Sorting and Searching_, 2. ed., 34. (Reading, Mass: Addison-Wesley, 1995).
<!--ID: 1723321489734-->
END%%
%%ANKI
Basic
Which key(s) found in B-trees move levels during node splits?
Back: The split node's median key.
Reference: Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1723321489735-->
END%%
%%ANKI
Basic
What does it mean for a B-tree split to be left-biased?
Back: Prefer the median on the LHS.
Reference: Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1723321489736-->
END%%
%%ANKI
Basic
What does it mean for a B-tree split to be right-biased?
Back: Prefer the median on the RHS.
Reference: Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1723321615984-->
END%%
%%ANKI
Basic
Consider splitting a node in a B-tree of order $m$. How many keys are in the split nodes?
Back: $\lfloor (m - 1) / 2 \rfloor$ and $\lceil (m - 1) / 2 \rceil$.
Reference: Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1723321489737-->
END%%
%%ANKI
Basic
*When* does a B-tree gain height?
Back: When the root node is split.
Reference: Donald Ervin Knuth, _Art of Computer Programming, 3: Sorting and Searching_, 2. ed., 34. (Reading, Mass: Addison-Wesley, 1995).
<!--ID: 1723211542058-->
END%%
%%ANKI
Basic
Suppose the following B-tree node is full. What is the result after splitting?
![[b-tree-full-node.png]]
Back:
![[b-tree-split-node.png]]
Reference: Donald Ervin Knuth, _Art of Computer Programming, 3: Sorting and Searching_, 2. ed., 34. (Reading, Mass: Addison-Wesley, 1995).
<!--ID: 1723321489738-->
END%%
%%ANKI
Basic
Consider the following B-tree. What is the result of inserting `B`?
![[b-tree-initial.png]]
Back: Indeterminate. We do not know the order of the tree.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1723321489739-->
END%%
%%ANKI
Basic
Consider the following B-tree of order $6$. What is the result of inserting `B`?
![[b-tree-initial.png]]
Back:
![[b-tree-inserted-b.png]]
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1723321489740-->
END%%
%%ANKI
Basic
Consider the following B-tree of order $6$. What is the result of inserting `Q` (right biased)?
![[b-tree-inserted-b.png]]
Back:
![[b-tree-inserted-q.png]]
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1723321489741-->
END%%
%%ANKI
Basic
What is the insertion runtime of a B-tree of order $m$ and height $h$?
Back: $O(mh)$
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1723321615987-->
END%%
%%ANKI
Basic
How many disk accesses are performed when inserting into a B-tree of order $m$ containing $n$ keys?
Back: $O(\log_m{n})$
Reference: Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).
<!--ID: 1723321615989-->
END%% END%%
## Bibliography ## Bibliography

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB