0% found this document useful (0 votes)
28 views

Search Algorithm

The linear search algorithm sequentially checks each element of an array to see if it matches the search key. It has a best case of 1 comparison and worst case of n comparisons. The B-tree data structure allows for efficient searching, insertion, and deletion operations on large data sets. It maintains balance by ensuring each node has a minimum number of children. Keys are moved up or down during splits and merges to keep the tree balanced.

Uploaded by

jigschuwa74
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Search Algorithm

The linear search algorithm sequentially checks each element of an array to see if it matches the search key. It has a best case of 1 comparison and worst case of n comparisons. The B-tree data structure allows for efficient searching, insertion, and deletion operations on large data sets. It maintains balance by ensuring each node has a minimum number of children. Keys are moved up or down during splits and merges to keep the tree balanced.

Uploaded by

jigschuwa74
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Search Algorithm

Linear Search Algorithm


• An algorithm that starts at the beginning and
walk to the end, testing for a match at each
item.
• The algorithm itself is simple. Loop to walk
over every item in the array, with a test to see
if the current item in the list matches the
search key. The loop can terminate in one of
two ways. If i reaches the end of the list, the
loop condition fails.
13

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 tree structure capable of reorganising itself is a balanced


tree.
Example:
B - Tree
B-Tree
– Further developed version of the Binary Tree
– A B-Tree of order m is either the empty tree or it is an m-way search
tree t with the following properties:
» All leaves are on the bottom level.
» All internal nodes (except the root node) have at least ceil(m / 2) (nonempty)
children.
» The root node can have as few as 2 children if it is an internal node, and can
obviously have no children if the root node is a leaf (that is, the whole tree
consists only of the root node).
» Each leaf node must contain at least ceil(m / 2) – 1 keys.
– Note that ceil(x) is the so-called ceiling function. It's value is the smallest
integer that is greater than or equal to x. Thus ceil(3) = 3, ceil(3.35) = 4,
ceil(1.98) = 2, ceil(5.01) = 6, ceil(7) = 7, etc.

Example: B – Tree of order m = 5


Min children: ceil(m/2) Max children: m = 5 Max Keys: m - 1
ceil(5/2) Min keys: ceil(m/2)-1 5–1=4
ceil(2.5) = 3 ceil(5/2)-1 = (2.5)-1
ceil(3)-1 = 2
B-Tree
» A search starts from root
» Addition or deletion of data starts from a leaf
» Example of a B-tree of order m=3. The root of a B-
tree of order three has either two or three subtrees
and the internal nodes also have either two or three
subtrees
B-Tree of Order, m = 5
• Other that the root node, all internal nodes have at least ceil(5 / 2) =
ceil(2.5) = 3 children (and hence at least 2 keys).
• Of course, the maximum number of children that a node can have is 5 (so
that 4 is the maximum number of keys).
Inserting a New Item
• Kruse’s Algorithm (see reference at the end of this file) the insertion
algorithm proceeds as follows:
– When inserting an item, first do a search for it in the B-tree.
– If the item is not already in the B-tree, this unsuccessful search will end at a leaf.
– If there is room in this leaf, just insert the new item here.
– Note that this may require that some existing keys be moved one to the right to
make room for the new item
– If instead this leaf node is full so that there is no room to add the new item, then
the node must be "split" with about half of the keys going into a new node to the
right of this one.
– The median (middle) key is moved up into the parent node. (Of course, if that
node has no room, then it may have to be split as well.)
– Note that when adding to an internal node, not only might we have to move
some keys one position to the right, but the associated pointers have to be
moved right as well. If the root node is ever split, the median key moves up into a
new root node, thus causing the tree to increase in height by one
Inserting a New Item
4 keys
• Insert the following letters into
what is originally an empty B-
tree of order 5: C N G A H E K Q
MFWLTZDPRXYS

• Order 5 means that a node can


have a maximum of 5 children
and 4 keys. All nodes other
than the root must have a
minimum of 2 keys. The first 4
letters get inserted into the Max 5 children
same node
Inserting a New Item
• When we try to insert the H, we
find no room in this node, so
we split it into 2 nodes, moving
the median item G up into a
new root node.
H

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

You might also like