Data Structure
Lecture: Multi-Way Trees

Review Binary Search Trees:
Binary search tree are organized:

BST node: key  = k*
       /      \
     Tleft     Tright
     k<=k*   k*<=k
Lets call k* the pivot.  The text does not use this terminology, I have made it up for now.  The pivot organizes the key below the node by keys less than or greater then the pivot value.  For a binary tree each node has only one pivot because there are only two sub-trees.

Multi-Way Trees: page 404
Multi-way search trees have 2 or more children and store items = (k, e).

Definitions:
v is a node of an ordered tree.
v is a d-node if v has d children

The ordered tree, T, is a multi-way search tree iff

If we continue to call the keys stored at a node of a multi-way tree pivots, then the search tree can still be organized by the pivots:
d-Node: keys: k1<=k2<=k3<=...<=kd-1
   /   |               |      \
T    T2     ...      Td-1     Td
k<=k1  k<=k2           k<=kd-1  kd-1<=k
We see that the number of sub-trees, children, of a node is one more then the degree of the node, the number of pivots, Items, of the node.

Again external nodes are place holders and can be programed away.

Proposition: A multi-way tree storing n items has n + 1 external nodes.

This is just like for binary trees.

Data Structure:
At each internal node a multi-way search tree, T with v at each internal node of T. The items stored at an node maybe:

List
Sequence
Tree
etc.
It is a good idea for the container ot be an ordered dictionary. The multi-way tree is a compound data type, divided into primary and secondary data structure.  In this case a dictionary of dictionaries.

We store at d-node v of T the items (k1, e1, v1), (k2, e2, v2), ... , (kd-1, ed-1, vd-1), (inf, null, vd).  This gives us access to the children.

Illustrate: page 405(a)

Searching:
Searching for key, k, in search tree T we process a d-node v.

We find the smallest key, ki >= k among the d keys at v.
if k < ki then continue the search in the dictionary of vi.
else k = ki then the search terminates.
Illustrate: page 405

The space requirement is O(n).

The time spent searching is dependent on the secondary structure.  Say the primary structure, the multi-way tree, has height h. Say dmax is the maximum d-node size.  If the secondary structure is ordered structure that the search through the secondary structure is O(lg dmax)  and the total search is O(h lg dmax).  If the secondary structure is unordered then the search through the secondary structure is O(dmax) and the total search is O(h dmax).
 

2-3-4 Trees: page 408
2-3-4 tree keeps the secondary data structure small;  dmax = 4.  So every node has at most 4 children.

Properties:

Consequentially: Illustrate: page 408 Insertion:
Insert new item (k, x) into T
Search for k terminates at external node z with parent v.
Insert a new item (k, x, w) into v along with the additional external node w.
This may cause an overflow, in other words v becomes a 5-node with keys k1 < k2 < k3 < k4 .
If overflow then split  v
Replace v with two nodes v' and v''.
v' a 3-node with children v1, v2, v3 storing k1 and k2
v'' is a 2-node with children v4 and v5 storing k4
If v is root then make new root u
u is parent of v in either case
Insert k3 into u and make v' and v'' children of u
Illustrate: page 411

Note that inserting the middle key, k3, into the parent u may cause u to overflow resulting in a cascading split.
A single split is O(1) but cascading split may take O(lg n).

Note the tree grows up through the root.

Removal:
Remove item with key k from tree T.

Search for k terminates at z such that ki = k
If z does not have external children then we must swap ki with node v that has only external children
Find the right most node v in subtree rooted at ith child of z (v has only external node children)
Swap the item ki at z with the last item v.  Note we will remove item ki so the ordering will be OK
Else v = z    // z originally only had external node children

Remove the item with k from v
This may cause and underflow in other words  v becomes a 1-node.

If underflow then

If v's adjacent sibling, w, is 3-node or greater then transfer
move a key of u to v
move a key of w to parent u.
// which keys depends on which sibling we transfer from
If v's adjacent sibling is 2-node then fusion
merge v with adjacent sibling, w, making v'
move a key from the parent u to v'
// which keys depends on which sibling we fussing to
Check for underflow at u.
If root underflow then replace root with u (delete root)
Transfers and fusion are O(1) but cascading underflow may take O(lg n)

Note that the cascading underflow causes the tree to shrink at the root.

Fusion does not seem like a Fusion in a 2-3-4 tree because the node v is empty.  For example in a (3, 5) tree then the node that has underflow is not empty and could be fussed.  The essence of Fusion is that we are reducing the number children of the parent node, u, so we must remove a key from the parent and but it into the new node representing the fusion, v'.

Because the tree grows and shrink at the root then depth property is maintain => balance tree => O(lg n) methods

Performance:
 
 

Operation Time
size, isEmpty O(1)
findElement, insertItem, removeElement O(log n)
findAllElements, reomveAllElements O(log n + s)
where s is the size of the iterator