The assignment
consists of two parts: (1) coding B-Trees (except delete, but including
B-Tree-Minimum() and
B-Tree-Maximum() ) (15 points),
and (2) a B-tree delete exercise (5 points).
The examples below show the format of the test files and sample output.
After that the implementation of B-trees is discussed.
For these tests the minimum degree,
t
will be 3.
You can use the
command interpreter below (or modify one from a previous lab)
to test your
B-tree implementation using the following test files:
treetest1, and
treetest2.
It isn't necessary to implement the
Print option
to print the keys in order (since it is not that much easier to
implement than Show-Tree(), which is more useful).
Also, it is not necessary to print out a message each time an item
is deleted (except that you might want to do this intially for
debugging).

As mentioned in class, B-trees are balanced trees designed to hold large
amounts of data and designed to work efficiently with disk memory.
You are given ShowTree(), which displays the
structure of a B-tree (rotated 90 degrees to the left).
So you need to implement
the insert, delete, and search operations, and auxiliary
operations such as
Split-Child(),
Merge-Children(),
Borrow-Left(),
Minimum(), etc.Note: Remember, the minimum degree,
t = 3.
Hint: Make your arrays one element larger and ignore index 0,
so that you can use arrays that start with 1, as in the pseudocode.

Here is the overall implementation strategy that I used:
I first implemented the code of
B-Tree-Create() in a constructor, then
B-Tree-Insert() and
Insert-nonfull(),
and
ShowTree(),
and tested them.
Then I implemented and tested B-Tree-Search().
Finally, I implemented deletion one case at a time, implementing
the necessary auxiliary functions as I went.
Unless you do the extra credit in the next lab, you can ignore the
Read-Disk()
and
Write-Disk()
operations (and allocation/deallocation of nodes, though in
practice you should definitely do this).
The following paragraphs contain more details.

Implementation of Insertion, etc.

I implemented B-Tree-Create() and
B-Tree-Insert()
by exactly following the pseudocode in the text.
I simplified
B-Tree-Split-Child()
and B-Tree-Insert-Nonfull())
by using new "helper" rouutines
copy(),
shiftUp(),
and
iLoc()
whose pseudocode is given in the "usefulpseudocode"
for Exercise 18.3-2 below.

For B-Tree-Search(), I first defined a new data type
BTreeLocation, which had two fields,
nodePtr
that points to a BTreeNode
and index that is the index of a key in a node,
and then I followed the pseudocode in the text (returning
(NIL,0) instead of NIL for
an unsuccessful search), but simplified by the "helper" routine
iLoc().

Helpful code:
Here is a
.h file for the BTree class
btree.h.
and skeleton code for the BTree class implementation
btree.cpp.
Here is a command interpreter:
cmdint.cpp.
Here is a "Makefile" that puts it all together:
Makefile.
When you download these files, be sure to remove the ".txt" suffixes
(by moving them to the same file name without the ".txt").

B-Tree-Delete pseudocode:

This is Exercise 18.3-2, page 502.

What To Hand In:

Clearly marked scripts of treetest1
and treetest2 which test insertion.
Outline the nodes and draw the child
pointers by hand on your output.

Well documented code for your B-tree implementation so far
(the rest will be in the next lab assignment).