Professional Documents
Culture Documents
Search Structures
FEB MAR
DEC OCT
Max comparisons: 6
NOV
Average comparisons: 3.5
A Balanced Binary Search Tree
For The Months of The Year
Input Sequence: JULY, FEB, MAY, AUG, DEC, MAR, OCT, APR,
JAN, JUNE, SEPT, NOV
Max comparisons: 4
JULY Average comparisons: 3.1
FEB MAY
JAN
AUG MAR OCT
NOV
+1 +1
MAR MAR
-1 -1 -1
-1
AUG MAY AUG MAY
0 0 0 0
0 +1
APR NOV APR JAN NOV
JAN
0 0
0
DEC DEC JULY
JAN
+1 -1
DEC MAR
+1 0 -1 -1
0 0 0 -1
APR JUNE MAY OCT
+1 +2 0
A A B
0 +1 0
B B A
h+2 h+2
AR h AR BL
BL BR BL BR
BR AR
height of BL increases
to h+1
Rebalancing Rotation RR
RR
-1 -2 0
A A B
0 -1 0
B B A
h+2 h+2
AL AL BR
BL BR BL BR
AL BL
height of BR increases
to h+1
Rebalancing Rotation LR(a)
+1 +2 0
A A C
LR(a)
0 -1 0 0
B B B A
0
C
Rebalancing Rotation LR(b)
LR(b)
+1 +2
A A 0
C
0 -1
B 0 -1
B
B A
h+2
0 AR +1 h+2
h AR
C C
h BL BL
CL CR CL CR CL CR
h BL AR
Rebalancing Rotation LR(c)
+2 0
A C
LR(c)
-1 +1 0
B B A
h+2
-1 AR
C
BL CL CR
h BL AR
CL CR
AVL Trees (Cont.)
• Once rebalancing has been carried
out on the subtree in question,
examining the remaining tree is
unnecessary.
• To perform insertion, binary search
tree with n nodes could have O(n) in
worst case. But for AVL, the
insertion time is O(log n).
AVL Insertion Complexity
• Let Nh be the minimum number of nodes in a
height-balanced tree of height h. In the worst
case, the height of one of the subtrees will be
h-1 and that of the other h-2. Both subtrees
must also be height balanced. Nh = Nh-1 + Nh-2 + 1,
and N0 = 0, N1 = 1, and N2 = 2.
• The recursive definition for Nh and that for the
Fibonacci numbers Fn= Fn-1 + Fn-2, F0=0, F1= 1.
• It can be shown that hN 2 h
= Fh+2 – 1. Therefore we
can derive that N h / 5 1 . So the worst-case
insertion time for a height-balanced tree with n
nodes is O(log n).
Probability of Each Type of
Rebalancing Rotation
B C
10 20 80
The Height of A 2-3 Tree
A A
40 20 40
B C B D C
10 20 70 80 10 30 70 80
A F
20 70
B D C E
10 30 60 80
Node Split
• From the above examples, we find
that each time an attempt is made to
add an element into a 3-node p, a new
node q is created. This is referred to
as a node split.
B C D B C D
10 20 60 70 90 95 10 20 60 90 95
A (b) 70 deleted
(a) Initial 2-3 tree
50 80
B C D
10 20 60 95
(c) 90 deleted
Deletion From A 2-3Tree
Example (Cont.)
A A
(d) 60 deleted (e) 95 deleted
20 80 20
B C D B C
10 50 95 10 50 80
A
(f) 50 deleted (g) 10 deleted
20 B
20 80
B C
10 80
Rotation and Combine
• As shown in the example, deletion may
invoke a rotation or a combine operations.
• For a rotation, there are three cases
– the leaf node p is the left child of its parent r.
– the leaf node p is the middle child of its parent
r.
– the leaf node p is the right child of its parent
r.
Three Rotation Cases
r r r
x ? y ? w z
p q p q q p
y z x z x y
a b c d a b c d b c d e
r r r
z ? y ? w y
q p q p q p
a
x y x z x z
a b c d a b c d b c d e
(b) p is the middle child of r (c) p is the right child of r
Steps in Deletion From a
Leaf Of a 2-3 Tree
• Step 1: Modify node p as necessary to reflect its status after the
desired element has been deleted.
• Step 2: while( p has zero elements && p is not the root ) {
let r be the parent of p;
let q be the left or right sibling of p ( as appropriate );
if( q is a 3-node )
rotate;
else
combine;
p=r;
}
• Step 3: If p has zero elements, then p must be the root. The left
child of p becomes the new root, and node p is deleted.
Combine When p is the Left
Child of r
r r
x z z
p q p
y x y
a b c a b c
(a)
r r
x z z
q p p
d
y x d
a b c a b c
(b)
M-Way Search Tree
Definition: An m-way search tree, either is empty or
satisfies the following properties:
(1)The root has at most m subtrees and has the following
structures:
n, A0, (K1, A1), (K2, A2), …, (Kn, An)
where the Ai, 0 ≤ i ≤ n ≤ m, are pointers to subtrees, and
the Ki, 1 ≤ i ≤ n ≤ m, are key values.
(2) Ki < Ki +1, 1 ≤ i ≤ n
(3) All key values in the subtree Ai are less than Ki +1 and
greater then Ki , 0 ≤ i ≤ n
(4) All key values in the subtree An are greater than Kn , and
those in A0 are less than K1.
(5) The subtrees Ai, 0 ≤ i ≤ n , are also m-way search trees.
Searching an m-Way Search
Tree
6.8
5.7
50 125 400
m
Insertion into a B-Tree
• Instead of using 2-3-4 tree’s top-down insertion, we generalize
the two-pass insertion algorithm for 2-3 trees because 2-3-4
tree’s top-down insertion splits many nodes, and each time we
change a node, it has to be written back to disk. This increases the
number of disk accesses.
• The insertion algorithm for B-trees of order m first performs a
search to determine the leaf node p into which the new key is to
be inserted.
– If the insertion of the new key into p results p having m keys, the node
p is split.
– Otherwise, the new p is written to the disk, and the insertion is
complete.
• Assume that the h nodes read in during the top-down pass can be
saved in memory so that they are not to be retrieved from disk
during the bottom-up pass, then the number of disk accesses for
an insertion is at most h (downward pass) +2(h-1) (nonroot splits) +
3(root split) = 3h+1.
• The average number of disk accesses is approximately h+1 for
large m.
Figure 10.38: B-Trees of
Order 3
20
10, 30
10 25, 30
(a) p = 1, s = 0
(b) p = 3, s = 1
p is the number of
nonfailure nodes in 20, 28
the final B-tree (c) p = 4, s = 2
with N entries.
10 10 25, 30
s is the number
of split
Deletion from a B-Tree
• The deletion algorithm for B-tree is also a
generalization of the deletion algorithm for 2-3
trees.
• First, we search for the key x to be deleted.
– If x is found in a node z, that is not a leaf, then the
position occupied by x in z is filled by a key from a leaf
node of the B-tree.
– Suppose that x is the ith key in z (x =Ki). Then x may be
replaced by either the smallest key in the sbutree Ai or
the largest in the subtree Ai-1. Since both nodes are leaf
nodes, the deletion of x from a nonleaf node is
transformed into a deletion from a leaf.
Deletion from a B-Tree
(Cont.)
• There are four possible cases when deleting from a leaf
node p.
– In the first case, p is also the root. If the root is left with at
least one key, the changed root is written back to disk.
Otherwise, the B-tree is empty following the deletion.
– In the second case, following the deletion, p has at least
m / 2 1keys. The modified leaf is written back to disk.
– In the third case, p has m / 2 2 keys, and its nearest sibling,
q, has at least m / 2 keys. Check only one of p’s nearest
siblings. p is deficient, as it has one less than the minimum
number of keys required. q has more keys than the minimum
required. As in the case of a 2-3 tree, a rotation is performed.
In this rotation, the number of keys in q decreases by one, and
the number in p increases by one.
– In the fourth case, p has m / 2 2 keys, and q has m / 2 1 keys.
p is deficient and q has minimum number of keys permissible
for a nonroot node. Nodes p and q and the keys K i are combined
to form a single node.
Figure 10.39 B-Tree of
Order 5
2 20 35
2 10 15 2 25 30 3 40 45 50