Prev: W3, Next: W5
Links: Zoom, TopHat (010339, or Form), Calculator:
Slide:



# B Tree Motivation

📗 Following a reference to a tree node requires memory access and memory access operations can be slow on mass storage devices.
➩ Goal: Keep the number of memory access operations low.
📗 B trees: make trees shallower (decrease the number of levels)
➩ Increase the number of values stored in each node.
➩ Increases children of node.
📗 Visualization: Link.



# B Tree

📗 B(alanced) Trees are search trees.
➩ Values are ordered in node.
➩ Children are ordered.
📗 Height \(H\) is the number of nodes on path from root to deepest leaf.
📗 Self-balancing: Height grows in \(O\left(\log N\right)\)
➩ All leaves must be on the same level.
➩ All internal nodes must have (number of values) + 1 children.
➩ Insertions can only happen into leaf nodes.
➩ B trees grow (add a new level) from the root upwards.

# B Tree Example

ID:
📗 [1 point] Search for .

📗 List of nodes:



# 2-3-4 Trees

📗 2-3-4 Trees are B trees with 2-nodes, 3-nodes, and 4-nodes.
➩ 2-node: 1 value, 2 children.
➩ 3-node: 2 values, 3 children.
➩ 4-node: 3 values, 4 children.
📗 Insertion algorithm for new value:
➩ Perform search for value to find leaf to insert into. For every "full" node (with 3 values) on path to and including the leaf during the search: (1) Split node (preemptive split); (2) Continue search at parent but don't split it even if it is now full.
➩ Insert new value into leaf at end of search. 

# 2-3-4 Tree Insert Example

ID:
📗 [1 point] Insert the node .

📗 List of nodes:

# 2-3-4 Tree Insert Another Example

📗 Another example from the campus section: 7, 14, 18, 23, 1, 11, 20, 29, 25, 27, 10.
📗 BT: Link.



# 2-3-4 Tree Deletion [Not on Midterm]

📗 If the node is not a 2-node leaf node, same as BST deletion.
➩ If a sibling is a 3-node or 4-node, do a single rotation.
➩ Else-If the parent is a 3-node or 4-node, merge with a sibling.
📗 Preemptive merge can be done when the root and the two children are all 2-nodes: they can be merged into one single 4-node.



# Readings

➩ 2-3-4 Tree: Link.
➩ Mapping 2-3-4 Trees into Red-Black Trees: Link
➩ GNU Make Manual: Link.



📗 Notes and code adapted from the course taught by Professor Florian Heimerl and Ashley Samuelson.
📗 Please use Ctrl+F5 or Shift+F5 or Shift+Command+R or Incognito mode or Private Browsing to refresh the cached JavaScript.
📗 You can expand all the examples and demos: , or print the notes: .
📗 If there is an issue with TopHat during the lectures, please submit your answers on paper (include your Wisc ID and answers) or this Google Form at the end of the lecture.

Prev: W3, Next: W5





Last Updated: November 25, 2025 at 1:46 AM