Assignment 9 -- due Thursday, November 7 at the beginning of lab
CS 5521 Fall Semester, 2013
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 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).

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. 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, and search operations (and delete in Lab 10), and auxiliary operations such as Split-Child(), Minimum(), Maximum(), (and Merge-Children(), Borrow-Left(), etc. for Lab 10)
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 "useful pseudocode" for Exercise 18.3-2 below.

Implementation of ShowTree()

Here is some pseudocode for ShowTree():

```ShowTree(x, depth)   // x points to root of subtree of given depth
if not x.leaf
ShowTree( x.c_(x.n+1), depth + 1 )
for i = x.n downto 1
print x.key_i right-justified in a field of (depth * 6) + 7
if not x.leaf
ShowTree( x.c_i, 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), 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:

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 502. Here is some useful pseudocode.

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