Assignment 9 -- due Monday, November 9 at the beginning of lab
CS 4521 Fall Semester, 2009
20 Points

Topic: B-trees

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 modify your command interpreter from previous labs 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).

Here is an example using treetest1:

```              treetest1 commands
+------------------------------------------------+
| # create tree (done in a constructor)          |
| c						 |
|                                                |
| # insert 10 items                              |
| i 372                                          |
| i 245                                          |
| i 491                                          |
| i 474                                          |
| i 440                                          |
| i 122                                          |
| i 418                                          |
| i 125                                          |
| i 934                                          |
| i 752                                          |
| 						 |
| # Show tree structure                          |
| S                                              |
| 						 |
| # search for item 122 in tree                  |
| s 122                                          |
| 						 |
| # search for item 441 not in tree              |
| s 441                                          |
| 						 |
| # end of test                                  |
+------------------------------------------------+
```
The output from treetest1 is below:
```       treetest1 script using B-trees
+-------------------------------------------------------+
| # create tree  (done in a constructor)                |
| # insert 10 items                                     |
| # Show tree structure                                 |
|                                                       |
| Structure of Btree (rotated 90 degrees to the left):  |
|                                                       |
|           934                                         |
|           752                                         |
|           491                                         |
|           474                                         |
|     440                                               |
|           418                                         |
|           372                                         |
|           245                                         |
|           125                                         |
|           122                                         |
|                                                       |
| # search for item 122 in tree                         |
| Key 122 found at index 1 in node:                     |
|                                                       |
| Leaf = True, n = 5                                    |
| Keys: 122 125 245 372 418                             |
|                                                       |
| # search for item 441 not in tree                     |
|                                                       |
| # end of test                                         |
+-------------------------------------------------------+
```

Discussion of B-tree Implementation:

As mentioned in class, B-trees are balanced trees designed to hold large amounts of data and designed to work efficiently with disk memory. In addition to the standard insert, delete, and search operations, you need to implement ShowTree(), which displays the structure of a B-tree (rotated 90 degrees to the left), 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 ShowTree(), and tested them. Then I implemented and tested B-Tree-Search(). Finally, I implemented insertion 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() (along with B-Tree-Split-Child() and B-Tree-Insert-Nonfull()) by exactly following the pseudocode in the text.

Implementation of ShowTree()

Here is some pseudocode for ShowTree():

```ShowTree(x, depth)   |> x points to root of subtree of given depth
if not leaf[x] then
ShowTree( c_(n[x]+1)[x], depth + 1 )
for i <- n[x] downto 1 do
print key_i[x] right-justified in a field of (depth * 6) + 4
if not leaf[x] then
ShowTree( c_i[x], depth + 1 )
```

Implementation of the Search Operation

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).

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 452.

What To Hand In:

1. Clearly marked scripts of treetest1 and treetest2 which test insertion. Outline the nodes and draw the child pointers by hand on your output.
2. Well documented code for your B-tree implementation so far (the rest will be in the next lab assignment).
3. Do Exercise 18.3-2, page 452. Here is some useful pseudocode.

Page URL: http://www.d.umn.edu /~ddunham/cs4521f09/assignments/a9/assignment.html
Page Author: Doug Dunham