Chapter19 BalancedSearchTrees
Chapter19 BalancedSearchTrees
CENG 214
Algorithms &
Data Structures 2
Introduction
1. Balanced Search Trees
2. 2-3 Trees
3. 2-3-4 Trees
4. Red-Black Trees
5. AVL Trees
Alg.-2 2/112
1. Balanced Search Trees
• Height of binary search tree
– Sensitive to order of additions and removals
Alg.-2 3/112
Balanced Search Trees
• The tallest and shortest binary search trees
containing the same data
Alg.-2 4/112
2-3 Trees
2. 2-3 Trees
• A 2-3 tree is a tree in which each internal node (non-
leaf) has either two or three children and all leaves are
at the same level.
• For example, the figure shows a 2-3 tree of height 3
Alg.-2 6/112
2. 2-3 Trees
• A 2-3 tree is not a binary tree
• A 2-3 tree is never taller than a minimum-height binary
tree
• The items in a 2-3 tree are ordered
Alg.-2 7/112
2-3 Trees
• T is a 2-3 tree of height h if one of the following
is true:
i. T is empty, in which case h is 0.
ii. T is of the form
r
TL TR
where r is a node that contains one data item and TL and TR are
both 2-3 trees, each of height h – 1.
Alg.-2 8/112
2-3 Trees
• T is a 2-3 tree of height h if one of the following
is true:
iii. T is of the form
r
TL TM TR
where r is a node that contains two data items and TL , TM , and TR are 2-3
trees, each of height h – 1.
In this case, the smaller item in r must be greater than each item in the left
subtree TL , and smaller than each item in the middle subtree TM
Alg.-2 9/112
2-3 Trees
Rules for placing data items in the nodes of a
2-3 tree
• A 2-node, which has two children, must contain a
single data item that is greater than the left child’s
item(s) and less than the right child’s item(s),
• A 3-node, which has three children, must contain
two data items, S and L , that satisfy the following
relationships
Alg.-2 10/112
2-3 Trees
• Nodes in a 2-3 tree
Alg.-2 11/112
2-3 Trees
• A 2-3 tree
Alg.-2 12/112
2-3 Trees
• A header file for a class of nodes for a 2-3 tree
Alg.-2 13/112
2-3 Trees
• A header file for a class of nodes for a 2-3 tree
Alg.-2 14/112
Traversing a 2-3 Tree
Performing the
analogue of an
inorder
traversal on a
binary tree:
Alg.-2 15/112
Searching a 2-3 Tree
• Retrieval operation for a 2-3 tree
Alg.-2 16/112
Searching a 2-3 Tree
• Retrieval operation for a 2-3 tree
Alg.-2 17/112
Searching a 2-3 Tree
• Search of a 2-3 and the shortest binary search
tree with approximately the same efficiency
– A binary search tree with n nodes cannot be
shorter than log2(n + 1) => approx. lgn.
– A 2-3 tree with n nodes cannot be taller than
log2(n + 1)
– Node in a 2-3 tree has at most two data items
Alg.-2 18/112
Searching a 2-3 Tree
• A balanced binary search tree
Alg.-2 19/112
Searching a 2-3 Tree
Alg.-2 20/112
Searching a 2-3 Tree
(b) The 2-3 tree of fig.b after inserting the sequence of
values 32 through 39;
Alg.-2 21/112
The insertion algorithm into a 2-3 Tree
Insertion algorithm;
When a leaf would contain three items, split it into two
nodes
you locate the leaf at which the search for the new
item would terminate.
If the leaf does not contain two items, you insert the
new item into the leaf, and you are done.
However, if the leaf already contains two items, you
must split it into two nodes, n1 and n2 .
Alg.-2 22/112
Inserting Data into a 2-3 Tree
Initial tree
Alg.-2 23/112
Inserting Data into a 2-3 Tree
• The steps for adding 38 to the tree
Alg.-2 24/112
Inserting Data into a 2-3 Tree
• After adding 37 to the tree
Alg.-2 25/112
Inserting Data into a 2-3 Tree
• The steps for adding 36 to the tree
Alg.-2 26/112
Inserting Data into a 2-3 Tree
• The steps for adding 36 to the tree
Alg.-2 27/112
Inserting Data into a 2-3 Tree
The tree after the adding 35, 34, and 33
to the tree in the previous figure
Alg.-2 28/112
Inserting Data into a 2-3 Tree
• Splitting a leaf in a 2-3 tree in general and in a
specific example
Alg.-2 29/112
Inserting Data into a 2-3 Tree
Splitting a leaf in a 2-3 tree in general and in a specific example
Alg.-2 30/112
Adding Data to a 2-3 Tree
Splitting an
internal node
in a 2-3 tree
in general
and in a
specific
example
Alg.-2 31/112
Adding Data to a 2-3 Tree
Splitting an
internal node
in a 2-3 tree
in general
and in a
specific
example
Alg.-2 32/112
Adding Data to a 2-3 Tree
Splitting the
root of a 2-3
tree general
and in a
specific
example
20 20 40
Alg.-2 33/112
Inserting Data into a 2-3 Tree
Think the same tree figure
Alg.-2 34/112
Removing Data from a 2-3 Tree
Swap the value to be removed with its inorder
successor,
• The removal strategy for a 2-3 tree is the inverse of
its insertion strategy.
• Just as a 2-3 tree spreads insertions throughout the
tree by splitting nodes when they would become too
full, it spreads removals throughout the tree by
merging nodes when they become empty.
Alg.-2 35/112
Removing Data from a 2-3 Tree
• The steps for removing 70 from the 2-3 tree
Alg.-2 36/112
Removing Data from a 2-3 Tree
• The steps for removing 70 from the 2-3 tree
Alg.-2 37/112
Removing Data from a 2-3 Tree
• The steps for removing 70 from the 2-3 tree
Alg.-2 38/112
Removing Data from a 2-3 Tree
• The steps for removing 70 from the 2-3 tree
Alg.-2 39/112
Removing Data from a 2-3 Tree
• The steps for removing 100 from the tree
Alg.-2 40/112
Removing Data from a 2-3 Tree
• The steps for removing 100 from the tree
Alg.-2 41/112
Removing Data from a 2-3 Tree
Next: The steps for removing 80 from the 2-3 tree
Alg.-2 42/112
Removing Data from a 2-3 Tree
• The steps for removing 80 from the 2-3 tree
Alg.-2 43/112
Removing Data from a 2-3 Tree
• The steps for removing 80 from the 2-3 tree
Alg.-2 44/112
Removing Data from a 2-3 Tree
Results of removing 70, 100, and 80 from (a) the 2-3 tree of
previous figure in (a) and the binary search tree of the same figure
Alg.-2 45/112
Removing Alg. from a 2-3 Tree
• Locate the node n that contains it. If n is not a leaf,
you find I ’s inorder successor and swap it with I .
• As a result of the swap, the removal always begins at
a leaf.
• If the leaf contains an item in addition to I , you
simply remove I and you are done.
• On the other hand, if the leaf contains only I,
removing I would leave the leaf without a data item,
• And then;
Alg.-2 46/112
Removing Data from a 2-3 Tree
• Possible situations during the removal of a
data item
Alg.-2 47/112
Removing Data from a 2-3 Tree
• Possible situations during the removal of a
data item
Alg.-2 48/112
Removing Data from a 2-3 Tree
• Possible situations during the removal of a
data item
Alg.-2 49/112
Removing Data from a 2-3 Tree
• Possible situations during the removal of a
data item
Alg.-2 50/112
2-3-4 Trees
3. 2-3-4 Trees
A 2-3-4 tree with the same data items as the 2-3 tree
Fig.19-20
Alg.-2 52/112
2-3-4 Trees
Rules for placing data items in the nodes of a 2-3-4 tree
Alg.-2 53/112
2-3-4 Trees
3. A 4-node, which has four children, must contain three data items S , M , and L
that satisfy the following relationships, as in the figure below:
S is greater than the left child’s item(s) and less than the middle-left child’s
item(s);
M is greater than the middle-left child’s item(s) and less than the middle-right
child’s item(s);
L is greater than the middle-right child’s item(s) and less than the right child’s
item(s).
• A leaf may contain either one, two, or three data items.
Alg.-2 54/112
2-3-4 Trees
• Searching and traversing
– Simple extensions of corresponding algorithms for
a 2-3 tree
• Adding data
– Like addition algorithm for 2-3 tree
– Splits node by moving one data item up to parent
node
Alg.-2 55/112
Inserting Data into a 2-3-4 Tree
• The insertion algorithm for a 2-3-4 tree, like the insertion
algorithm for a 2-3 tree, splits a node by moving one of its items
up to its parent node.
• For a 2-3 tree, the search algorithm traces a path from the root
to a leaf and then backs up from the leaf as it splits nodes
• To avoid this return path after reaching a leaf, the insertion
algorithm for a 2-3-4 tree splits 4-nodes as soon as it encounters
them on the way down the tree from the root to a leaf.
• As a result, when a 4-node is split and an item is moved up to
the node’s parent, the parent cannot possibly be a 4-node, and
so it can accommodate another item.
Alg.-2 56/112
Adding Data to 2-3-4 Trees
• Adding 20 to a one-node 2-3-4 tree, Fig19-22
• Insert 20. While determining the insertion point, you begin at the root
and encounter the 4-node <10 30 60>, which you split by moving the
middle value 30 up.
• Because the node is the root, you create a new root, move 30 into it, and
attach two children,
Alg.-2 57/112
Adding Data to 2-3-4 Trees
• After adding 50 and 40 to the tree
• Or adding 40 and 50 to the tree
Alg.-2 58/112
Adding Data to 2-3-4 Trees
• The steps for adding 70 to the tree
• While searching previous figure for 70’s insertion point, you encounter the
4-node <40 50 60>, because 70 is greater than 30. You split this 4-node by
moving 50 up to the node’s parent, <30>, to get the tree in Figure (a).
• Then insert 70 into the leaf <60>, as in Figure (b)
Alg.-2 59/112
Adding Data to 2-3-4 Trees
• After adding 80 and 15 to the tree
Alg.-2 60/112
Adding Data to 2-3-4 Trees
• The steps for adding 90 to the tree
• Split this 4-node into two nodes and move 70 up to the root, as in Figure (a)
• Finally, because 90 is greater than 70, you insert 90 into the leaf <80> to get
the tree in Figure (b).
Alg.-2 61/112
Adding Data to 2-3-4 Trees
• The steps for adding 100 to the tree
Alg.-2 62/112
Adding Data to 2-3-4 Trees
Splitting 4-nodes during insertion. As you have just seen,
you split each 4-node as soon as you encounter it during your
search from the root to the leaf that will accommodate the new
item to be inserted.
Alg.-2 63/112
Adding Data to 2-3-4 Trees
1. Splitting a 4-node root when adding data to a 2-3-4 tree
Fig19-28
• Split <10 30 60> in Figure 19-22 a, resulting in the tree in Fig. 19-22, as in slide 56.
• Then split <30 50 70> during the insertion of 100 into the tree as in Figure in slide 60.
Alg.-2 64/112
Adding Data to 2-3-4 Trees
2. Splitting a 4-
node whose
parent is a 2-node
when adding data
to a 2-3-4 tree.
Fig.19-29
Alg.-2 65/112
Adding Data to 2-3-4 Trees
3. Splitting a 4-node whose parent is a 3-node
when adding data to a 2-3-4 tree,
Fig.19-30
Alg.-2 66/112
Adding Data to 2-3-4 Trees
3. Splitting a 4-node whose parent is a 3-node
when adding data to a 2-3-4 tree, Fig.19-30
Alg.-2 67/112
Adding Data to 2-3-4 Trees
3. Splitting a 4-node whose parent is a 3-node
when adding data to a 2-3-4 tree, Fig.19-30
Alg.-2 68/112
Removing Data from a 2-3-4 Tree
• Has same beginning as removal algorithm for
a 2-3 tree
Alg.-2 69/112
Removing Data from a 2-3-4 Tree
• That is, either the parent or the sibling could be a 2-node, a 3-
node, or a 4-node.
• For example, if the next node that you encounter is a 2-node and
both its parent and nearest sibling are 2-nodes, apply the
transformation that Figure 19-28 illustrates,
• but in reverse; however, if the parent is a 3-node, apply the
transformation that Figure 19-29 illustrates, but in reverse; and
if the parent is a 4-node, apply the transformation that Figure
19-30 illustrates, but in reverse.
Alg.-2 70/112
Red-Black Trees
4. Red-Black Trees
• A 2-3-4 tree requires more storage than binary
search tree
Alg.-2 72/112
4. Red-Black Trees
Alg.-2 73/112
4. Red-Black Trees
• The idea is to represent each 3-node and 4-
node in a 2-3-4 tree as an equivalent binary
search tree.
• To distinguish between 2-nodes that appeared
in the original 2-3-4 tree and 2-nodes that were
generated from 3-nodes and 4-nodes, you use
red and black child pointers.
Alg.-2 74/112
Red-Black Trees
Red-black representations of a 4-node and a 3-node.
Alg.-2 75/112
Red-Black Trees
• A red-black tree that represents
the 2-3-4 tree, the same tree
in slide 51, Fig.19-20
Alg.-2 76/112
Introduction to Red-Black Trees
Red-Black Tree is a self-balancing Binary Search Tree
(BST) where every node follows following rules.
Rules:
1) Every node has a color either red or black.
2) Root of tree is always black.
3) There are no two adjacent red nodes (A red node cannot
have a red parent or red child).
4) Every path from a node (including root) to any of its
descendant NULL node has the same number of black nodes.
Alg.-2 77/112
Introduction to Red-Black Trees
Insertion sequence;
7-18-3-10-22-8-11-26
Then insert 20-21
after slide 91
Rules:
1) Every node has a color either red or black.
2) Root of tree is always black
3) There are no two adjacent red nodes (A red node cannot have a red
parent or red child).
4) Newly added nodes are always in red. Every path from a node
(including root) to any of its descendant NULL node has the same number
of black nodes.
Alg.-2 78/112
Insertion to Red-Black Trees
Alg.-2 79/112
Insertion to Red-Black Trees
Let x be the newly inserted node.
1) Perform standard BST insertion and make the color of
newly inserted nodes as RED.
2) If x is root, change color of x as BLACK (Black height of
complete tree increases by 1).
3) Do following if color of x’s parent is not BLACK and x is not
root.
….a) If x’s uncle is RED (Grand parent must have been black
from property 4)
……..(i) Change color of parent and uncle as BLACK.
……..(ii) color of grand parent as RED.
……..(iii) Change x = x’s grandparent, repeat steps 2 and 3 for
new x.
Alg.-2 80/112
Insertion to Red-Black Trees
Alg.-2 81/112
Insertion to Red-Black Trees
Alg.-2 82/112
Insertion to Red-Black Trees
Alg.-2 83/112
Insertion to Red-Black Trees
Right Right
Case (See g, p
and x)
Alg.-2 84/112
Searching and Traversing a Red-Black Tree
Alg.-2 85/112
Adding to and Removing from a Red-Black Tree
Alg.-2 86/112
Adding to and Removing from a Red-Black Tree
Other example;
Splitting a red-black representation of
a 4-node root, As Figure 19-28 shows
in slide 63
Alg.-2 87/112
Adding to and Removing from a Red-Black Tree
• Splitting a red-black representation of a 4-
node whose parent is a 2-node (Slide 64)
Alg.-2 88/112
Adding to and Removing from a Red-Black Tree
Alg.-2 89/112
Adding to and Removing from a Red-Black Tree
Splitting a red-black
representation
of a 4-node whose
parent is a 3-node,
Same situation as
in slide 65,
Left case
Alg.-2 90/112
Adding to and Removing from a Red-Black Tree
Splitting a red-black
representation
of a 4-node whose
parent is a 3-node,
Same situation as in
slide 66,
Middle Node
Alg.-2 91/112
Adding to and Removing from a Red-Black Tree
Splitting a red-black
representation
of a 4-node whose
parent is a 3-node,
Same situation as in
slide 67
Right case
Alg.-2 92/112
AVL Trees
5. AVL Trees
• An AVL tree (named after inventors Adel’son-Vel’skii
and Landis) tree
• AVL tree means a balanced binary search tree
• Maintains its height close to the minimum
• Rotations restore the balance
Alg.-2 94/112
5. AVL Trees
• It is possible to rearrange any binary search tree
of n nodes to obtain a binary search tree with
the minimum possible height [ log2 ( n + 1) ],
• The AVL algorithm is a compromise.
– It maintains a binary search tree with a height close
to the minimum, but it is able to do so with far less
work than would be necessary to keep the height
of the tree exactly equal to the minimum.
Alg.-2 95/112
5. AVL Trees
• The basic strategy of the AVL algorithm is to monitor
the shape of the binary search tree.
• You insert or delete nodes just as you would for any
binary search tree, but after each insertion or deletion,
you check that the tree is still an AVL tree.
• That is, you determine whether any node in the tree
has left and right subtrees whose heights differ by
more than 1. (One is permitted!)
Alg.-2 96/112
AVL Trees
• An unbalanced binary search tree
Alg.-2 97/112
AVL Tree Rotations
• In AVL tree, after performing operations like insertion
and deletion we need to check the balance factor of
every node in the tree.
• If every node satisfies the balance factor condition
then we conclude the operation otherwise we must
make it balanced.
• Whenever the tree becomes imbalanced due to any
operation we use rotation operations to make the
tree balanced.
• Rotation operations are used to make the tree
balanced.
Alg.-2 98/112
AVL Trees
• Correcting an imbalance in an AVL tree due to
an addition by using a single rotation to the left
Alg.-2 99/112
AVL Trees
• Correcting an imbalance in an AVL tree due to an
addition by using a single rotation to the left
Alg.-2 100/112
AVL Trees
• A single rotation to the left that does not
affect the height of an AVL tree
Alg.-2 101/112
AVL Trees
A single
rotation to the
left that does
not affect the
height of an AVL
tree
Alg.-2 102/112
AVL Trees
Alg.-2 103/112
AVL Tree Rotations
• Rotation operations are used to make the tree
balanced
Rotation is the process of moving nodes either to left
or to right to make the tree balanced.
• There are 4 rotations and they are classified into
two types.
Alg.-2 104/112
AVL Tree Rotations
1. Single Left Rotation (LL Rotation)
Alg.-2 105/112
AVL Tree Rotations
3. Left Right Rotation (LR Rotation)
Alg.-2 106/112
AVL Tree Rotations
4. Right Left Rotation (RL Rotation)
Alg.-2 107/112
AVL Tree Example
Alg.-2 108/112
AVL Tree Example
Depending upon the type of insertion, the Rotations
are categorized into four categories. (just info)
# Rotation Description
1 LL Rotation The new node is inserted to the left sub-tree of left
sub-tree of critical node.
2 RR Rotation The new node is inserted to the right sub-tree of
the right sub-tree of the critical node.
3 LR Rotation The new node is inserted to the right sub-tree of
the left sub-tree of the critical node.
4 RL Rotation The new node is inserted to the left sub-tree of the
right sub-tree of the critical node.
Alg.-2 109/112
AVL Tree Visualization
See:
https://www.cs.usfca.edu/~galles/visualization/AVLtree.html
• The point is you will rotate the tree in the node unbalancing
takes place. Keep in mind that in might be AN INTERMEDIATE
NODE.
Alg.-2 110/112
Summary
1. In computer science, a B-tree is a self-balancing tree data
structure that maintains sorted data and allows searches,
sequential access, insertions, and deletions in logarithmic
time.
2. 2-3, 2-3-4, and AVL trees are examples of self-balancing
binary search tree, balanced to maintain O(log n) height.
3. A B-tree is a balanced tree, but it is NOT a binary tree. Nodes
have more children, which increases per-node search time
but decreases the number of nodes the search needs to visit.
This makes them good for disk-based trees.
Alg.-2 111/112
Summary
4. A 2-3 tree and a 2-3-4 tree are variants of a binary search tree
(BST). The internal nodes of a 2-3 tree can have either two or
three children.
5. The insertion and removal algorithms for a 2-3-4 tree require
only a single pass from root to leaf and, thus, are more
efficient than the corresponding algorithms for a 2-3 tree.
6. A red-black tree is a binary tree representation of a 2-3-4 tree
that requires less storage than a 2-3-4 tree. Insertions and
removals for a red-black tree are more efficient than the
corresponding operations on a 2-3-4 tree.
Alg.-2 112/112