B-tree: insert and rearrange: Difference between revisions
(Created page with "Category:B-Tree Category:Algorithm == General Information == '''Algorithmic problem:''' Text here '''Type of algorithm:''' Text '''Auxiliary data:''' # Three po...") |
No edit summary |
||
Line 7: | Line 7: | ||
'''Auxiliary data:''' | '''Auxiliary data:''' | ||
# Three pointers | # Three pointers <math>p, p_1, p_2 </math> or type "pointer to B-tree node". | ||
# A current key <math> K' \in K</math> | # A current key <math> K' \in K</math> | ||
== Abstract View == | == Abstract View == | ||
'''Invariant:''' Before and after each iteration | '''Invariant:''' Before and after each iteration, <math>p</math> points to a node <math>N</math> such that the following holds: | ||
# In the case <math>p.n < 2M</math>, all implementation invariants of B-trees are maintained if the following <math>K'</math> can be inserted at some position in <math>N</math> such that after insertion. | |||
# the searched key is in the [[Directed Tree#Ranges of Search Tree Nodes|range]] of '''''N'''''. | # the searched key is in the [[Directed Tree#Ranges of Search Tree Nodes|range]] of '''''N'''''. | ||
Revision as of 16:19, 30 September 2014
General Information
Algorithmic problem: Text here
Type of algorithm: Text
Auxiliary data:
- Three pointers [math]\displaystyle{ p, p_1, p_2 }[/math] or 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.
- the searched key is in the range of N.
Variant: p is redirected from the current node N to some child of the current node.
Break condition:
- p points to a leaf of the B-tree or (that is, inclusive-or)
- the searched key is in the node to which p points.
Induction Basis
Abstract view: p is initialized so as to point to the root of the B-tree.
Implementation: Obvious.
Proof: Obvious.
Induction Step
Abstract view:
- Let N denote the node to which p currently points.
- If the searched key is in N, terminate the algorithm and return true.
- Otherwise, if N is a leaf, terminate the algorithm and return false.
- Otherwise, let p point the child of N such that the searched key is in the range of that child
Implementation:
- If K is one of the values [math]\displaystyle{ p.keys[1],\dots,p.keys[p.n] }[/math], terminate the algorithm and return true.
- If [math]\displaystyle{ p.children[0] = void }[/math] (that is, the current node is a leaf), terminate the algorithm and return false.
- If [math]\displaystyle{ K \lt p.keys[1] }[/math] set [math]\displaystyle{ p := p.children[p.n] }[/math].
- Otherwise, if [math]\displaystyle{ K \gt p.keys[p.n]\lt /math set \lt math\gt p := p.children[p.n] }[/math].
- Otherwise, there is exactly one [math]\displaystyle{ i \in \{1,\dots,p.n-1\} }[/math] such that [math]\displaystyle{ p.keys[i] \lt K \lt p.keys[i+1] }[/math].
- Set [math]\displaystyle{ p := p.children[i] }[/math].
Correctness: Obvious.
Complexity
Statement: The asymptotic complexity is in [math]\displaystyle{ \Theta(\log n) }[/math] in the worst case.
Proof: Follows immediately from the fact that the height of B-tree with n nodes is in [math]\displaystyle{ \Theta(\log n) }[/math] (cf. the remark clause of the B-Trees page).