You are on page 1of 14

M Way Search Trees

M-Way Search Tree


An m-way search tree, either is empty or satisfies the
following properties:
(1)The root has at most m sub trees and has the
following structures:
n, P0, (K1, P1), (K2, P2), …, (Kn , Pn)
Where
Pi, 0 ≤ i ≤ n ≤ m, are pointers to sub trees
and
Ki, 1 ≤ i ≤ n ≤ m, are key values.
(2) Ki < Ki +1, 1 ≤ i ≤ n
M-Way Search Tree
(3) All key values in the sub tree Pi are less
than Ki +1 and greater then Ki ,
0≤i≤n

(4) All key values in the sub tree Pn are


greater than Kn , and those in P0 are less than
K1 .

(5) The sub trees Pi, 0 ≤ i ≤ n , are also m-


way search trees.
m-Way Search Tree
• An m-way search tree is a tree of order m has
the following properties
Each node has at most m children and m-1
keys
The keys in each node are in ascending order.
The keys in the first i children are smaller than
the key Ki
The keys in the last m-i children are larger
than the key Ki
3- way Search Tree

• Order = 3
• Each node has at most 3 Children
• Each node has at most 2 keys
3- way Search Tree Example
4- way Search Tree

• Order = 4
• Each node has at most 4 Children
• Each node has at most 3 keys
4- way Search Tree Example
Example
• Construct a M-way Search tree of order 3 for
the following data

10, 60 ,100, 200, 40, 120, 50, 5, 70


Task-1
• Construct a M-way Search tree of order 4 with
the data

3, 14, 7, 1, 8, 5, 11, 17, 13, 6, 23, 12,


20, 26, 4 , 16, 18, 24, 25 and 19.
Task-2
• Construct a M-way Search tree of order 4 with
the data

3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4,


31, 35 and 36
Searching in M-Way Search Tree
• Suppose to search in M-Way search tree T for the
key value X. By searching the keys of the root, we
determine i such that Ki ≤ X < Ki+1.
– If X = Ki, the search is complete.
– If X ≠ Ki, X must be in a sub tree Pi if X is in T.
– We then proceed to retrieve the root of the sub
tree Pi and continue the search until we find X or
determine that X is not in T.
Searching in M-Way Search Tree
Searching for X:
• If X < K(i), recursively search in K(i)'s left sub
tree.
• If X > K(i), recursively search in K(i)'s right sub
tree.
• If X ==K(i), for some i, X is found!
• Else, for some i, K(i) < X < K(i+1); recursively
search in sub tree between K(i) and K(i+1).
Searching an M-Way Search Tree
• The maximum number of nodes in a tree of degree m and
height h is

m
0i  h 1
i
 (m  1) /( m  1)
h

• Therefore, for an M-Way search tree, the maximum


number of keys it has is m h – 1.

You might also like