Search Algorithm
Search Algorithm
23 15 2 13 60 4
15
23
2 ==
13 ==13
13??
Speed of Linear Search
Best: 1 Comparison
Worst: n Comparison
0 1 2 3 . . . n-2 N-1
Balance Tree Algorithm
• If data is added or deleted in a tree structure, leaves
randomly develop and the operational efficiency of searching
and processing the tree will decrease
A C G N
Inserting a New Item
• Inserting E, K, and Q
proceeds without
requiring any splits
E G Q
K
A A
C C H N
K N
Inserting a New Item
• Inserting M requires a
split. Note that M
happens to be the M
median key and so is
moved up into the G G
parent node.
A C E H K N Q
Inserting a New Item
• The letters F, W, L, and
T are then added
without needing any W
FLT
split. G M
A A
C C
E E H H K K N NQ Q W
Inserting a New Item
• When Z is added, the rightmost leaf must be split. The
median item T is moved up into the parent node. Note
that by moving up the median key, the tree is kept fairly
balanced, with 2 keys in each of the resulting nodes.
Z
G M T
A C E F H K L N Q T W Z
Inserting a New Item
• The insertion of D causes the leftmost leaf to be
split. D happens to be the median key and so is
the one moved up into the parent node. The
letters P, R, X, and Y are then added without any
need of splitting
D
D GG M T
A AC C E F H K L N P Q R W X Y Z
Inserting a New Item
• Finally, when S is added, the node with N, P, Q, and R splits, sending the
median Q up to the parent. However, the parent node is full, so it splits,
sending the median M up to form a new root node. Note how the 3
pointers from the old parent node stay in the revised node that
contains D and G.
M
S
D D G G MMQ T T
A C E F H K L N
N PPP Q
Q
Q RRR W
S X Y Z
Deleting an Item
• In the B-tree as we left it at the end of the last section, delete H. Of
course, we first do a lookup to find H. Since H is in a leaf and the leaf has
more than the minimum number of keys, this is easy. We move the K
over where the H had been and the L over where the K had been.
H M
D G Q T
A C E F H K L N P R S W X Y Z
Deleting an Item
• Next, delete the T. Since T is not in a leaf, we find its successor (the
next item in ascending order), which happens to be W, and move W
up to replace the T. That way, what we really have to do is to delete
W from the leaf, which we already know how to do, since this leaf
has extra keys. In ALL cases we reduce deletion to a deletion in a
leaf, by using this method.
M T
D G Q T
A C E F K L N P R S W
W XX YY Z
Deleting an Item
• Next, delete R. Although R is in a leaf, this leaf does not have an extra key; the
deletion results in a node with only one key, which is not acceptable for a B-tree
of order 5. If the sibling node to the immediate left or right has an extra key, we
can then borrow a key from the parent and move a key up from this sibling. In
our specific case, the sibling to the right has an extra key. So, the successor W of
S (the last key in the node where the deletion occurred), is moved down from the
parent, and the X is moved up. (Of course, the S is moved over so that the W can
be inserted in its proper place.)
M R
D G Q W
A C E F K L N P R
S W
S YX ZY Z