23 T
REES
Contents
z
Introduction
{
Test Yourself #1
z
2

3 Tree Operations
{
lookup
{
insert
Test Yourself #2
{
delete
Test Yourself #3
z
2

3 Tree Summary
z
Summary of Binary

Search Trees vs 2

3 Trees
Introduction
Recall that, for binarysearch trees, although the averagecase times for the lookup, insert, and delete
methods are all O(log N), where N is the number of nodes in the tree, the worstcase time is O(N).
We can
guarantee
O(log N) time for all three methods by using a
balanced
tree  a tree that always
has height O(log N) instead of a binarysearch tree.
A number of different balanced trees have been defined, including
AVL trees
,
redblack trees
, and
B trees
. You might learn about the first two in an algorithms class, and the third in a database class.
Here we will look at yet another kind of balanced tree called a
23 Tree
.
The important idea behind all of these trees is that the insert and delete operations may
restructure
the tree to keep it balanced. So lookup, insert, and delete will always be logarithmic in the number of
nodes, but insert and delete may be more complicated than for binarysearch trees.
The important facts about a 23 tree are:
z
Every nonleaf node has either 2 or 3 children.
z
All leaves are at the same depth.
z
Information (keys and associated data) is stored
only
at leaves (internal nodes are for
organization only).
z
Keys at leaves are ordered left to right.
z
In addition to child pointers, each internal node stores:
{
the value of the max key in the
left
subtree (leftMax)
{
the value of the max key in the
middle
subtree (middleMax)
z
If a node only has 2 children, they are left and middle (not left and right) children.
As for binary search trees, the same values can usually be represented by more than one tree. Here
are three different 23 trees that all store the values 2,4,7,10,12,15,20,30:
Page 1 of 6
23 Trees
2008/3/27
http://pages.cs.wisc.edu/~cs3671/topics/BalancedSearchTrees/
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document