CS 61B: Lecture 27
Monday, October 30, 2006
234 TREES
===========
A 234 tree is a perfectly balanced tree.
It has a big advantage over regular
binary search trees:
because the tree is perfectly balanced, find, insert, and
remove operations take O(log n) time, even in the worst case.
234 trees are thus named because every node has 2, 3, or 4 children, except
leaves, which are all at the bottom level of the tree.
Each node stores 1, 2,
or 3 entries, which determine how other entries are distributed among its
children's subtrees.
Each internal (nonleaf) node has one more child than keys.
For example, a
node with keys [20, 40, 50] has four children.
Eack key k in the subtree
rooted at the first child satisfies k <= 20; at the second child,
20 <= k <= 40; at the third child, 40 <= k <= 50; and at the fourth child,
k >= 50.
WARNING:
The algorithms for insertion and deletion I'll discuss today are
different from those discussed by Goodrich and Tamassia.
The text presents
"bottomup" 234 trees, so named because the effects of node splits at the
bottom of the tree can work their way back up toward the root.
I'll discuss
"topdown" 234 trees, in which insertion and deletion finish at the leaves.
Topdown 234 trees are usually faster than bottomup ones.
Goodrich and
Tamassia call 234 trees "(2, 4) trees".
234 trees are a type of Btree, which you may learn about someday in
connection with fast disk access for database systems.
[1]
Object find(Object k);
Finding an entry is straightforward.
==========
Start at the root.
At each node,
+20 40 50+
check for the key k; if it's not
/==========\
present, move down to the
//
/
\
\\
appropriate child chosen by



=======
comparing k against the keys.
14
32
43
+70 79+
Continue until k is found,



=======
or k is not found at a
/
\
/
\
/
\
/

\
leaf node.
For example,
       ==== 
find(74) visits the
10 18 25 33 42 47 57 62 66 +74+ 81
doublelined boxes at right.        ==== 
Incidentally, you can define an inorder traversal on 234 trees analogous to
that on binary trees, and it visits the keys in sorted order.
±
[2]
void insert(Object k, Object e);
insert(), like find(), walks down the tree in search of the key k.
If it finds
an entry with key k, it proceeds to that entry's "left child" and continues.
Unlike find(), insert() sometimes modifies
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '09
 Lee
 Binary Search

Click to edit the document details