?? page345.html
字號:
<HTML>
<HEAD>
<TITLE>Inserting Items into a B-Tree</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF">
<img src="cover75.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cover75.gif" alt="Logo" align=right>
<b>Data Structures and Algorithms
with Object-Oriented Design Patterns in C++</b><br>
<A NAME="tex2html6189" HREF="page346.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page346.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html6187" HREF="page340.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page340.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html6181" HREF="page344.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page344.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html6191" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html6192" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <BR><HR>
<H2><A NAME="SECTION0011720000000000000000">Inserting Items into a B-Tree</A></H2>
<P>
The algorithm for insertion into a B-Tree begins
as do all the other search tree insertion algorithms:
To insert item <I>x</I>,
we begin at the root and conduct a search for it.
Assuming the item is not already in the tree,
the unsuccessful search will terminate at a leaf node.
This is the point in the tree at which the <I>x</I> is inserted.
<P>
If the leaf node has fewer than <I>M</I>-1 keys in it,
we simply insert the item in the leaf node and we are done.
E.g., consider a leaf node with <I>n</I><I><</I><I>M</I> subtrees and <I>n</I>-1 keys of the form
<P> <IMG WIDTH=386 HEIGHT=16 ALIGN=BOTTOM ALT="displaymath65686" SRC="img1393.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1393.gif" ><P>
For every new key inserted in the node,
a new subtree is required too.
In this case because <I>T</I> is a leaf,
all its subtrees are empty trees.
Therefore, when we insert item <I>x</I>,
we really insert the pair of items <IMG WIDTH=35 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline65706" SRC="img1394.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1394.gif" >.
Suppose the key to be inserted falls between <IMG WIDTH=10 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline64406" SRC="img1228.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1228.gif" > and <IMG WIDTH=28 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline65710" SRC="img1395.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1395.gif" >,
i.e., <IMG WIDTH=93 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline64484" SRC="img1237.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1237.gif" >.
When we insert the pair <IMG WIDTH=35 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline65706" SRC="img1394.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1394.gif" > into <I>T</I>
we get the new leaf <I>T</I>' given by
<P> <IMG WIDTH=478 HEIGHT=17 ALIGN=BOTTOM ALT="displaymath65687" SRC="img1396.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1396.gif" ><P>
<P>
What happens when the leaf is full?
I.e., suppose we wish to insert the pair,
<IMG WIDTH=35 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline65706" SRC="img1394.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1394.gif" > into a node <I>T</I> which already has <I>M</I>-1 keys.
Inserting the pair in its correct position gives a result of the form
<P> <IMG WIDTH=377 HEIGHT=17 ALIGN=BOTTOM ALT="displaymath65688" SRC="img1397.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1397.gif" ><P>
However, this is not a valid node in a B-tree of order <I>M</I>
because it has <I>M</I>+1 subtrees and <I>M</I> keys.
The solution is to split node <I>T</I>' in half as follows
<P> <IMG WIDTH=500 HEIGHT=42 ALIGN=BOTTOM ALT="eqnarray21956" SRC="img1398.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1398.gif" ><P>
Note, <IMG WIDTH=18 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline65734" SRC="img1399.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1399.gif" > is a valid B-tree node because it contains
<IMG WIDTH=43 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline65628" SRC="img1383.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1383.gif" > subtrees and <IMG WIDTH=71 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline65656" SRC="img1386.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1386.gif" > keys.
Similarly, <IMG WIDTH=18 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline65740" SRC="img1400.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1400.gif" > is a valid B-tree node because it contains
<IMG WIDTH=84 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline65742" SRC="img1401.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1401.gif" > subtrees and <IMG WIDTH=112 HEIGHT=26 ALIGN=MIDDLE ALT="tex2html_wrap_inline65744" SRC="img1402.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1402.gif" > keys.
Note that there is still a key left over,
namely <IMG WIDTH=42 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline65746" SRC="img1403.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1403.gif" >.
<P>
There are now two cases to consider--either <I>T</I> is the root or it is not.
Suppose <I>T</I> is not the root.
Where we once had the single node <I>T</I>,
we now have the two nodes, <IMG WIDTH=18 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline65734" SRC="img1399.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1399.gif" > and <IMG WIDTH=18 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline65740" SRC="img1400.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1400.gif" >,
and the left-over key, <IMG WIDTH=42 HEIGHT=23 ALIGN=MIDDLE ALT="tex2html_wrap_inline65746" SRC="img1403.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1403.gif" >.
This situation is resolved as follows:
First, <IMG WIDTH=18 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline65734" SRC="img1399.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1399.gif" > replaces <I>T</I> in the parent of <I>T</I>.
Next, we take the pair <IMG WIDTH=83 HEIGHT=25 ALIGN=MIDDLE ALT="tex2html_wrap_inline65766" SRC="img1404.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1404.gif" >
and recursively insert it in the parent of <I>T</I>.
<P>
Figure <A HREF="page345.html#figbtree2" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page345.html#figbtree2"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A> illustrates this case for a B-tree of order three.
Inserting the key 6 in the tree causes the leaf node to overflow.
The leaf is split in two.
The left half contains key 5; and the right, key 7; and key 6 is left over.
The two halves are re-attached to the parent in the appropriate place
with the left-over key between them.
<P>
<P><A NAME="22216"> </A><A NAME="figbtree2"> </A> <IMG WIDTH=575 HEIGHT=502 ALIGN=BOTTOM ALT="figure21969" SRC="img1405.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1405.gif" ><BR>
<STRONG>Figure:</STRONG> Inserting Items into a B-Tree (Insert 6)<BR>
<P>
<P>
If the parent node fills up,
then it too is split and the two new nodes are inserted in the grandparent.
This process may continue all the way up the tree to the root.
What do we do when the root fills up?
When the root fills, it is also split.
However, since there is no parent into which to insert the two new children,
a new root is inserted above the old root.
The new root will contain exactly two subtrees and one key,
as allowed by Definition <A HREF="page340.html#defnbtree" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page340.html#defnbtree"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>.
<P>
Figure <A HREF="page345.html#figbtree3" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page345.html#figbtree3"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A> illustrates this case for a B-tree of order three.
Inserting the key 3 in the tree causes the leaf node to overflow.
Splitting the leaf and reattaching it causes the parent to overflow.
Similarly, splitting the parent and reattaching it causes the grandparent
to overflow but the grandparent is the root.
The root is split and a new root is added above it.
<P>
<P><A NAME="22657"> </A><A NAME="figbtree3"> </A> <IMG WIDTH=575 HEIGHT=833 ALIGN=BOTTOM ALT="figure22221" SRC="img1406.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img1406.gif" ><BR>
<STRONG>Figure:</STRONG> Inserting Items into a B-Tree (Insert 3)<BR>
<P>
<P>
Notice that the height of the B-tree only increases when the root node splits.
Furthermore, when the root node splits,
the two halves are both attached under the new root.
Therefore, the external nodes all remain at the same depth,
as required by Definition <A HREF="page340.html#defnbtree" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page340.html#defnbtree"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A>.
<P>
<BR> <HR>
<UL>
<LI> <A NAME="tex2html6193" HREF="page346.html#SECTION0011721000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page346.html#SECTION0011721000000000000000">Implementation</A>
<LI> <A NAME="tex2html6194" HREF="page347.html#SECTION0011722000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page347.html#SECTION0011722000000000000000">Running Time Analysis</A>
</UL>
<HR><A NAME="tex2html6189" HREF="page346.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page346.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html6187" HREF="page340.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page340.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html6181" HREF="page344.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page344.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html6191" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html6192" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <P><ADDRESS>
<img src="bruno.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/bruno.gif" alt="Bruno" align=right>
<a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html">Copyright © 1997</a> by <a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.
</ADDRESS>
</BODY>
</HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -