17 Out to Disk

Version 6.1.11

Handout:

17.1 Interacting with Files

This section surveys some of the useful classes for reading and writing "disk" files*, in the java.io package. (* or in Unix/Linux, just files, which could include any kind of device, disk or otherwise)

17.2 Compression

(Skip)

17.3 External Sorting

17.4 B-Trees

B-Tree Insertion

Example 1: Insertion

Insert 8 into a B-tree of order 5. See the following figures.

In the examples of insertion and deletion, we assume that the order of the B-tree is 5, so a maximal node has 5 subtrees and 4 keys, and a minimal node has 3 subtrees and 2 keys. ?'s represent values or subtrees (possibly empty) which don't matter.

PNG preview and link to SVG diagram for initial tree

(1a) Initial B-tree, before inserting 8.

PNG preview and link to SVG diagram after inserting 8 in the leaf

(1b) After inserting 8 in the leaf (4, 5, 6, 7). The leaf is now too full.

PNG preview and link to SVG diagram after splitting the leaf

(1c) After splitting the leaf into (4, 5), 6, and (7, 8), and absorbing 6 into its parent. Now the parent node is too full.

PNG preview and link to SVG diagram after splitting parent

(1d) After splitting the parent node into (2, 3), 6, and (9, 10), and absorbing 6 into the root.

PNG preview and link to SVG diagram after splitting root

(1e) After splitting the root. The tree is now one level higher.

B-Tree Deletion

Example 2. Deletion, Case 2.c.i

Delete F from a B-tree of order 5. See figures below.

PNG preview and link to SVG diagram for initial tree

(2a) Initial B-tree.

PNG preview and link to SVG diagram for tree after removing F.

(2b) After removal of F. The removal sucks E down to fill the place of F, and sucks D up to take the place of E.

Example 3. Deletion, Case 2.c.ii

Delete F from a B-tree of order 5. See figures below.

PNG preview and link to SVG diagram for initial tree

(3a) Initial B-tree

PNG preview and link to SVG diagram for tree after removing F.

(3b) After removing F. The removal sucks E down to fill the place of F. Since (A, D) is so light, it is sucked all the way up and down.

Exercise

From the B-tree (of order 5? order 4?) shown below, delete these values, in order:

  1. 50 (easy)
  2. 80 (replace with 90 and rotate)
  3. 110 (merge with left sibling, then rotate their parent and its right sibling)

PNG preview and link to SVG diagram for exercise

Some Interesting Low-Order B-trees

Example 5

Convert a 2-3-4 tree to red-black. See figures below.

PNG preview and link to SVG diagram for 2-3-4 tree

(5a) The initial 2-3-4 tree.

                   +-----+
                   |b7   |
                   | \   | 
                   |  r14|
                   +-+---+
        __________/  |    \_______________
       /             |                    \
      +---+          +---+                 +----------+
      |b2 |          |b10|                 |   b18    |
      |\  |          |   |                 |  /   \   |
      | r5|          |   |                 |r16    r21|
      +-+-+          +---+                 +---+--+---+
  ___/ /   \        /     \              _/    |   \  \______
 /    /     \      /       \            /      |    \        \
+--+ +----+ +--+  +----+   +--------+  +---+   +---+ +-----+  +---------+
|b1| |b3  | |b6|  |b8  |   |  b12   |  |b15|   |b17| |b19  |  |  b23    |
|  | | \  | |  |  | \  |   | /  \   |  |   |   |   | | \   |  | /   \   |
|  | |  r4| |  |  |  r9|   |r11  r13|  |   |   |   | |  r20|  |r22   r24|
+--+ +----+ +--+  +----+   +--------+  +---+   +---+ +-----+  +---------+

(5b) After replacing each 2-3-4 node with a small red-black tree.

A node with two keys, such as (7, 14), can be done two ways:

b7                      b14
 \            or        /
 r14                   r7
             b7
     _______/ \__________________
    /                            \
   b2                            r14
  / \__              ___________/   \____ 
 /     \            /                    \
b1      r5         b10                    b18         
      _/  \     ___/  \_               __/  \_____ 
     /     |   /        \             /           \
     b3    b6  b8        b12         r16           r21       
      \         \       /  \        _/ \        ___/ \____ 
       \         \     /    \      /    \      /          \
        r4        r9   r11   r13   b15   b17   b19         b23
                                                \         _/ \_ 
                                                 \       /     \
                                                 r20    r22   r24

(5c) The red-black tree that remains, after removing the 2-3-4 scaffolding.

Additional References for B-trees

Footnotes


  1. Revision history:
    • Version 6.1.1, 2010 Dec 8. Fixed "numbering" of sublists.
    • Version 6.1, 2010 Dec 6. Added and fixed links.
    • Version 6, 2010 Dec 2. Converted to markdown.
    • Version 5, 2009 Dec 5. Converted most ASCII figures to structview.
    • Version 4, 2009 Nov 30. Partly converted to RST.
    • Version 3, 2008 Nov 17.
    • Version 2, 2007 Nov 26: HTMLized; need to check a couple of references.
    • Version 1, 2006 Dec 4: plain text.