B-tree: insert and rearrange: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
(10 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:Videos]] | |||
{{#ev:youtube|https://www.youtube.com/watch?v=vbRZ8h6ROYc|500|right||frame}} | |||
[[Category:B-Tree]] | [[Category:B-Tree]] | ||
[[Category:Algorithm]] | [[Category:Algorithm]] | ||
Line 7: | Line 9: | ||
'''Auxiliary data:''' | '''Auxiliary data:''' | ||
# Three pointers <math>p, p_1, p_2 </math> | # Three pointers <math>p, p_1, p_2 </math> of type "pointer to B-tree node". | ||
# A current key <math> K' \in K</math> | # A current key <math> K' \in K</math> | ||
Line 29: | Line 31: | ||
== Induction Step == | == Induction Step == | ||
=== Abstract view: === | |||
# | # If the node is not full, the key can be inserted, and we are done. | ||
# If the | ## If the new key is larger than all keys stored in the node, it can be appended. | ||
# | ## Otherwise, is must override one specific slot. For that, first the contents of this slot and of each slot to its right must be moved one position further right (the corresponding values and successor pointers have to be moved as well). Then the new (key,value)-pair may be safely inserted. | ||
# Otherwise, | # On the other hand, if the node is indeed full, the key cannot be inserted. In this case: | ||
## The current node <math>N</math> will be split into two nodes, <math>N_1</math> and <math>N_2</math>. | |||
## <math>N_1</math> will contain all keys strictly less than the median of { <math>p.keys[1], ... , p.keys[p.n], K'</math> } | |||
## Analogously, <math>N_2</math> will contain all keys strictly greater than the median of { <math>p.keys[1], ... , p.keys[p.n], K'</math> }. | |||
## Next we try to | |||
## If <math>K'</math> is that median itself, nothing is to be done on the current node. | |||
## Otherwise, the median of <math>\{p.keys[1], \dots , p.keys[p.n], K'\}</math> is in one of the slots of the node. This slot is overridden (by moving the contents of each slot to the right one position further left). The previous contents of this slot is the new <math>K'</math> . If the current node is the root, which we next try to insert in the predecessor of | |||
=== Implementation: === | |||
# If | # If <math>p.n = 2M</math> | ||
# | ## If <math>p.keys[M] < K' < p.keys[M + 1]</math>, break the iteration and continue the loop. | ||
# | ## Otherwise: <math> x:= M</math> if <math> K' < p.keys[M]</math>, and <math> x:= M+1</math> otherwise. | ||
# | ## Set <math> K'' := p.keys[x]</math>. | ||
# Otherwise | ## For <math> j \in \{x+1, \dots, p.n \}</math> (in this order), set <math> p.keys [j-1]:= p.keys[j] </math> and <math>p. successor[j-1]:= p.successors[j]</math>. | ||
# | # If <math> p.keys[p.n] < K'</math>, insert the new (key, value)-pair at position <math>p.keys[p.n+1</math> | ||
# Otherwise: | |||
## Let <math>i \in \{ 1, \dots , p.n \} </math> be minimal subject to <math>K' < p.keys[i]</math> | |||
## For <math> j \in \{ p.n, \dots , i \} </math> (in this order), set <math> p.keys[j+1]:= p.keys[j]. </math> | |||
# If <math> p.n < 2M</math>, set <math> p.n:= p.n+1</math> and terminate the algorithm. | |||
# Otherwise, set <math>K':= K''</math>. | |||
=== Correctness: === | |||
Easy to see. | |||
== | == Pseudocode == | ||
== Complexity == |
Latest revision as of 23:19, 19 June 2015
General Information
Algorithmic problem: Text here
Type of algorithm: Text
Auxiliary data:
- Three pointers [math]\displaystyle{ p, p_1, p_2 }[/math] of type "pointer to B-tree node".
- A current key [math]\displaystyle{ K' \in K }[/math]
Abstract View
Invariant: Before and after each iteration, [math]\displaystyle{ p }[/math] points to a node [math]\displaystyle{ N }[/math] such that the following holds:
- In the case [math]\displaystyle{ p.n \lt 2M }[/math], all implementation invariants of B-trees are maintained if the following [math]\displaystyle{ K' }[/math] can be inserted at some position in [math]\displaystyle{ N }[/math] such that after insertion.
- In the case [math]\displaystyle{ p.n = 2M }[/math], imagine that we could extend [math]\displaystyle{ p }[/math].keys by one more slot (and, consequently, extend .successors by one more slot). Then [math]\displaystyle{ K' }[/math] can be inserted in [math]\displaystyle{ N }[/math] at a position such that - except for the statement about the size of [math]\displaystyle{ N }[/math] - all implementation invariants of B-trees are again maintained after insertion.
Variant: The height level of the node to which [math]\displaystyle{ p }[/math] points is decreased by [math]\displaystyle{ 1 }[/math].
Break condition: [math]\displaystyle{ p.N \lt 2M }[/math] or (that is, inclusive-or) [math]\displaystyle{ p }[/math] points to the root.
Induction Basis
Abstract view:
- [math]\displaystyle{ p }[/math] is set to the input pointer.
- [math]\displaystyle{ p_1 }[/math] and [math]\displaystyle{ p_2 }[/math] are void initially.
Implementation: Obvious.
Proof: The requirement on the input pointer immediately implies the invariants.
Induction Step
Abstract view:
- If the node is not full, the key can be inserted, and we are done.
- If the new key is larger than all keys stored in the node, it can be appended.
- Otherwise, is must override one specific slot. For that, first the contents of this slot and of each slot to its right must be moved one position further right (the corresponding values and successor pointers have to be moved as well). Then the new (key,value)-pair may be safely inserted.
- On the other hand, if the node is indeed full, the key cannot be inserted. In this case:
- The current node [math]\displaystyle{ N }[/math] will be split into two nodes, [math]\displaystyle{ N_1 }[/math] and [math]\displaystyle{ N_2 }[/math].
- [math]\displaystyle{ N_1 }[/math] will contain all keys strictly less than the median of { [math]\displaystyle{ p.keys[1], ... , p.keys[p.n], K' }[/math] }
- Analogously, [math]\displaystyle{ N_2 }[/math] will contain all keys strictly greater than the median of { [math]\displaystyle{ p.keys[1], ... , p.keys[p.n], K' }[/math] }.
- Next we try to
- If [math]\displaystyle{ K' }[/math] is that median itself, nothing is to be done on the current node.
- Otherwise, the median of [math]\displaystyle{ \{p.keys[1], \dots , p.keys[p.n], K'\} }[/math] is in one of the slots of the node. This slot is overridden (by moving the contents of each slot to the right one position further left). The previous contents of this slot is the new [math]\displaystyle{ K' }[/math] . If the current node is the root, which we next try to insert in the predecessor of
Implementation:
- If [math]\displaystyle{ p.n = 2M }[/math]
- If [math]\displaystyle{ p.keys[M] \lt K' \lt p.keys[M + 1] }[/math], break the iteration and continue the loop.
- Otherwise: [math]\displaystyle{ x:= M }[/math] if [math]\displaystyle{ K' \lt p.keys[M] }[/math], and [math]\displaystyle{ x:= M+1 }[/math] otherwise.
- Set [math]\displaystyle{ K'' := p.keys[x] }[/math].
- For [math]\displaystyle{ j \in \{x+1, \dots, p.n \} }[/math] (in this order), set [math]\displaystyle{ p.keys [j-1]:= p.keys[j] }[/math] and [math]\displaystyle{ p. successor[j-1]:= p.successors[j] }[/math].
- If [math]\displaystyle{ p.keys[p.n] \lt K' }[/math], insert the new (key, value)-pair at position [math]\displaystyle{ p.keys[p.n+1 }[/math]
- Otherwise:
- Let [math]\displaystyle{ i \in \{ 1, \dots , p.n \} }[/math] be minimal subject to [math]\displaystyle{ K' \lt p.keys[i] }[/math]
- For [math]\displaystyle{ j \in \{ p.n, \dots , i \} }[/math] (in this order), set [math]\displaystyle{ p.keys[j+1]:= p.keys[j]. }[/math]
- If [math]\displaystyle{ p.n \lt 2M }[/math], set [math]\displaystyle{ p.n:= p.n+1 }[/math] and terminate the algorithm.
- Otherwise, set [math]\displaystyle{ K':= K'' }[/math].
Correctness:
Easy to see.