# ch19 final final

```Balanced trees: AVL trees
Definition An AVL tree is a binary search tree in which, for
every node, the difference between the heights of its left and
right subtrees, called the balance factor, is at most 1 (with
the height of an empty tree defined as -1)
1
2
10
10
0
1
0
0
5
20
5
20
1
-1
4
7
0
1
-1
12
4
7
0
0
0
0
2
8
2
8
(a)
(b)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6
Rotations
If a key insertion violates the balance requirement at some node,
the subtree rooted at that node is transformed via one of the
four rotations. (The rotation is always performed for a subtree
rooted at an “unbalanced” node closest to the new leaf.)
1
2
2
0
3
2
-1
R
>
0
0
1
3
0
3
2
LR
1
>
0
0
1
3
0
0
1
2
2
(a)
Single R-rotation
(c)
Double LR-rotation
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6
AVL tree construction - an example
Construct an AVL tree for the list 5, 6, 8, 3, 2, 4, 7
0
-1
-2
5
5
5
0
L(5)
0
-1
6
6
6
>
0
0
5
8
0
8
1
2
1
6
6
6
1
0
2
0
5
8
5
8
0
3
R (5)
>
0
0
3
8
1
0
0
3
2
5
0
2
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6
AVL tree construction - an example (cont.)
2
0
6
5
-1
0
3
8
LR (6)
>
0
-1
3
6
0
1
0
0
0
2
5
2
4
8
0
4
-1
0
5
5
0
-2
3
6
0
RL (6)
0
0
1
2
4
8
>
0
3
7
0
0
0
0
2
4
6
8
0
7
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6
Deleting nodes
Case 1: leaf (easy)
- Remove the leaf node
Case 2: node with 1 child node
Case 3: node with 2 child nodes
- same process as in a normal binary search tree
1) find successor node
2) replace value being deleted
3) delete successor node
Deleting nodes - rebalancing
• What nodes could become imbalanced?
• Case 1: path from deleted node to root
• Case 2: path from deleted node to root
• Case 3: path from deleted successor to root
• Must retrace the path to determine imbalanced nodes
• Similar rebalancing procedures (not covered here)
2-3 Tree
• Definition A 2-3 tree is a search tree that
•
may have 2-nodes and 3-nodes
•
height-balanced (all leaves are on the same level)
•
<K
2-node
3-node
K
K1, K2
>K
< K1
(K1 , K 2 )
> K2
• A 2-3 tree is constructed by successive insertions of keys
given, with a new key always inserted into a leaf of the tree.
If the leaf is a 3-node, it’s split into two with the middle key
promoted to the parent.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6
2-3 tree construction – an example
Construct a 2-3 tree the list 9, 5, 8, 3, 2, 4 , 7
9
5, 9
2, 3, 5
>
5, 8, 9
8
8
8
5
3, 5
9
3, 8
>
2
9
5
9
3, 8
2
9
4, 5
9
5
>
3, 8
2
4, 5, 7
9
>
3, 5, 8
2
4
7
9
2
3
8
4
7
9
2-3 Trees
• A 2-3 tree of height 3
2-3 Trees
• Nodes in a 2-3 tree
2-3 Trees
• A 2-3 tree
Traversing a 2-3 Tree
Retrieval operation on a 2-3
Tree
Searching a 2-3 Tree
• Search of a 2-3 and shortest binary search
tree approximately same efficiency
– A binary search tree with n nodes cannot be
shorter than log2(n + 1)
– 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
• Searching 2-3 tree is O(log n)
Searching a 2-3 Tree
• vs. balanced binary search tree
Searching a 2-3 Tree
the values 39 down to 32
Adding Data to a 2-3 Tree
• After inserting 39 into the tree
Adding Data to a 2-3 Tree
• The steps for adding 38 to the tree
Adding Data to a 2-3 Tree
• After adding 37 to the tree
Adding Data to a 2-3 Tree
• Adding 36 to the tree
Adding Data to a 2-3 Tree
Adding Data to a 2-3 Tree
• The tree after the adding 35, 34,
and 33
Adding Data to a 2-3 Tree
• Splitting a leaf in a 2-3 tree in general
and in a specific example
Adding Data to a 2-3 Tree
• Splitting a leaf in a 2-3 tree in general
and in a specific example
Adding Data to a 2-3 Tree
• Splitting an internal node in a 2-3 tree
in general and in a specific example
Adding Data to a 2-3 Tree
• Splitting an internal node in a 2-3 tree
in general and in a specific example
Adding Data to a 2-3 Tree
• Splitting an internal node in a 2-3 tree
in general and in a specific example
Adding Data to a 2-3 Tree
• Splitting the root of a 2-3 tree
general and in a specific example
Removing Data from a 2-3 Tree
• The steps for removing 70
Removing Data from a 2-3 Tree
• The steps for removing 70
Removing Data from a 2-3 Tree
• The steps for removing 70
Removing Data from a 2-3 Tree
• After removing 70
Removing Data from a 2-3 Tree
• The steps for removing 100
Removing Data from a 2-3 Tree
• After removing 100
Removing Data from a 2-3 Tree
• The steps for removing 80
Removing Data from a 2-3 Tree
• The steps for removing 80
Removing Data from a 2-3 Tree
• The steps for removing 80
Removing Data from a 2-3 Tree
• Possible situations during the
removal of a data item from a 2-3 tree
Removing Data from a 2-3 Tree
• Possible situations during the
removal of a data item from a 2-3 tree
Removing Data from a 2-3 Tree
• Possible situations during the
removal of a data item from a 2-3 tree
Removing Data from a 2-3 Tree
• Possible situations during the
removal of a data item from a 2-3 tree
2-3-4 Trees
• A 2-3-4 tree with the same data
items as the 2-3 tree shown previously
2-3-4 Trees
• A 4-node in a 2-3-4 tree
2-3-4 Trees
• Searching and traversing
– Simple extensions of corresponding
algorithms for a 2-3 tree
– Like addition algorithm for 2-3 tree
– Splits node by moving one data item up to
parent node
• Adding 20 to a one-node 2-3-4 tree
• After adding 50 and 40 to the
tree
• The steps for adding 70 to the
tree
• After adding 80 and 15 to the tree
• The steps for adding 90 to the tree
• The steps for adding 100 to the tree
• Splitting a 4-node root when
adding data to a 2-3-4 tree
• Splitting a 4-node whose parent
is a 2-node when adding data to a 2-3-4 tree
• Splitting a 4-node whose parent
is a 3-node when adding data to a 2-3-4 tree
• Splitting a 4-node whose parent
is a 3-node when adding data to a 2-3-4 tree
• Splitting a 4-node whose parent
is a 3-node when adding data to a 2-3-4 tree
Removing Data from a 2-3-4
Tree
• Has same beginning as removal algorithm
for a 2-3 tree
• Transform each 2-node into a 3-node
or a 4-node
• Insertion and removal algorithms for 2-3-4
tree require fewer steps than for 2-3 tree
Red-Black Trees
• A 2-3-4 tree requires more storage than
binary search tree
• Red-black tree has advantages of a 2-3-4
tree but requires less storage
• In a red-black tree,
– Red pointers link 2-nodes that now contain
values that were in a 3-node or a 4-node.
Red-Black Trees
• Red-black representation s
of a 4-node and a 3-node
Red-Black Trees
• A red-black tree that represents
the 2-3-4 tree shown earlier
Searching and Traversing a
Red-Black Tree
• A red-black tree is a binary search tree
• Thus, search and traversal
– Use algorithms for binary search tree
– Simply ignore color of pointers
from a Red-Black Tree
• Red-black tree represents a 2-3-4 tree
– Accommodate red-black representation
• Splitting equivalent of a 4-node requires
simple color changes
– Pointer changes called rotations result in a
shorter tree
from a Red-Black Tree
• Splitting a red-black representation
of a 4-node root
from a Red-Black Tree
• Splitting a red-black representation
of a 4-node whose parent is a 2-node
from a Red-Black Tree
• Splitting a red-black representation
of a 4-node whose parent is a 2-node
from a Red-Black Tree
• Splitting a redblack
representation
of a 4-node
whose parent
is a 3-node
from a Red-Black Tree
• Splitting a redblack
representation
of a 4-node
whose parent
is a 3-node
from a Red-Black Tree
• Splitting a redblack
representation
of a 4-node
whose parent
is a 3-node
```