Download as pdf or txt
Download as pdf or txt
You are on page 1of 48

2-3 Trees

2-3 Trees

Giving credit where credit is due:


Most of slides for this lecture are based on
slides taken from internet.
I just have modified a few of them
2-3 Trees
Features
 Search tree
 Each internal node has either 2 or 3 children
 A node with k items has k + 1 children
 All leaves are at the same level
 Deletion and insertion operations take place at leaf level
Example of 2-3 Tree

Note : The number of children of all internal


nodes are one more than the number of values
stored in that node
2-3 Trees
A node with zero or two children is called a 2-node
A node with zero or three children is called a 3-node
A 2-node contains one element and either has no children or two
children
Elements of the left sub-tree less than the element
Elements of the right sub-tree greater than or equal to the element
A 3-node contains two elements, one designated as the smaller and one
as the larger
If a 3-node has children then
Elements of the left sub-tree are less than the smaller element
The smaller element is less than or equal to the elements of the
middle sub-tree
Elements of the middle sub-tree are less then the larger element
The larger element is less than or equal to the elements of the right
sub-tree
2-3 Trees with Ordered Nodes
2-node 3-node

• leaf node can be either a 2-node or a 3-node


2-3 Tree vs. Binary Tree
A 2-3 tree is not a binary tree since a node in the 2-3
tree can have three children.
But a 2-3 tree does resemble a full binary tree.
If a 2-3 tree does not contain 3-nodes, it is like a
complete binary tree since all its internal nodes have
two children and all its leaves are at the same level.
2-3 Tree vs. Binary Tree
If a 2-3 tree does have three children, the tree will
contain more nodes than a full binary tree of the same
height.
Therefore, a 2-3 tree of height h has at least 2(h+1)-1
nodes.
In other words, a 2-3 tree with N nodes never has
height greater then log (N+1), the minimum height of
a binary tree with N nodes.
Node Representation of 2-3 Trees
typedef treeNode* Tree;
struct treeNode
{ treeItemType SmallItem,LargeItem;
Tree LChildPtr, MChildPtr, RChildPtr;
};
2-3 Trees Insertion
To insert an item, say key, into a 2-3 tree
1. Locate the leaf at which the search for key would
terminate
2. If leaf is null (only happens when root is null), add new
root to tree with item
3. If leaf has one item insert the new item key into the leaf
4. If the leaf contains 2 items, split the leaf into 2 nodes
n1 and n2
2-3 Trees Insertion
When an internal node would contain 3 items
1. Split the node into two nodes
2. Accommodate the node’s children
When the root contains three items
1. Split the root into 2 nodes
2. Create a new root node
3. The tree grows in height
2-3 Trees Insertion
Inserting Items

An initially empty 2-3 tree after adding (a) 60 and (b) 50; (c), (d) adding
20 causes the 3-node to split
Inserting Items

The 2-3 tree after adding (a) 80; (b) 90; (c) 70
Inserting Items

Adding 55 to the 2-3 tree causes


a leaf and then the root to split
Inserting Items

The 2-3 tree after adding (a) 10; (b), (c) 40


Inserting Items

The 2-3 tree after adding 35


Inserting Items
Insert 39, 38,..32 in following tree
Inserting Items
Insert 38

divide leaf
insert in leaf and move middle result
value up to parent
Inserting Items
Insert 37
Inserting Items
Insert 36

divide leaf
insert in leaf and move middle
value up to parent

overcrowded
node
Inserting Items
... still inserting 36

divide overcrowded node,


move middle value up to parent, result
attach children to smallest and largest
Inserting Items
After Insertion of 35, 34, 33
Inserting Items
How do we insert 32?
Inserting Items
Final Result
Insertion Exercise
Insert 32, 57, 25
Insertion Exercise
Insert(3) and (45) in below tree

20 80

5 30 70 90 100

2 4 10 15 23 25 40 50 75 85 95 110 120
Deletion Algorithm I
Deleting item I:

1. Locate node n, which contains item I (may be null if no


item)
2. If node n is not a leaf  swap I with inorder successor
 deletion always begins at a leaf
3. If leaf node n contains another item, just delete item I
else
try to redistribute nodes from siblings (see next slide)
if not possible, merge node (see next slide)
Deletion Algorithm II
Redistribution
A sibling has 2
items:
 redistribute item
between siblings
and
parent

Merging
No sibling has 2 items:
 merge node
 move item from
parent
to sibling
Deletion Algorithm III
Redistribution
Internal node n has no
item left
 redistribute

Merging
Redistribution not
possible:
 merge node
 move item from
parent
to sibling
 adopt child of n

If n's parent ends up without item, apply process recursively


Deletion Algorithm IV
If merging process reaches the root and root is without item
 delete root
Deleting Items
Delete 70

70

80
Deleting Items
Deleting 70: swap 70 with inorder successor (80)
Deleting Items
Deleting 70: ... get rid of 70
Deleting Items
Result
Deleting Items
Delete 100
Deleting Items
Deleting 100
Deleting Items
Result
Deleting Items
Delete 80
Deleting Items
Deleting 80 ...
Deleting Items
Deleting 80 ...
Deleting Items
Deleting 80 ...
Deleting Items
Final Result

comparison with
binary search tree
Remove(80)
Step 1: Exchange 80 with in-order successor.
45
20
85

3 5 30 70 90 100

2 4 10 25 40 50 75 80 95 110 120
Remove(80);
Redistribute

45
20
85

3 5 30 70 95 110

2 4 10 25 40 50 75 90 100 120
Deletion Excercise
Delete 80 , 70 and 75 from following tree
45
20
80

3 5 30 70 90 100

2 4 10 25 40 50 75 85 95 110 120
Operations of 2-3 Trees

Balanced search trees are found in many flavors and


have been the major data structure used for
structures called dictionaries, where insertion,
deletion, and searching must take place. In 1970,
John Hopcroft introduced 2-3 search trees as an
improvement on existing balanced binary trees. Later
they were generalized to B-trees by Bayer and
McCreight. B-trees were then simplified by Bayer to
form red-black trees.

You might also like