Professional Documents
Culture Documents
CS 8391
DATA STRUCTURES
UNIT - III
NON LINEAR DATA
STRUCTURES – TREES
Tree ADT – tree traversals - Binary Tree
ADT – expression trees – applications of
trees – binary search tree ADT –Threaded
Binary Trees - AVL Trees – B-Tree - B+
Tree - Heap – Applications of heap.
B TREE
(b – BALANCE)
MULTIWAY(M-WAY)
AVL Tree
SEARCH TREE
• In such a tree M is called the degree of the tree. Note in a
binary search tree M = 2, so it has one value and 2 sub-trees.
• Every internal node of an M-way search tree consists of
pointers to M sub-trees and contains M – 1 keys, where M > 2
P0 K0 P1 K1 P2 K2 …… Pn-1 Kn-1 Pn
…
18 45
9 11 27 36 54 63
AVL
B Trees
Tree
A B-tree of order m is an m-way tree (i.e., a tree where each
node may have up to m children) in which:
• the number of keys in each non-leaf node is one less than
the number of its children
• all leaves are on the same level
• the root node has 2 to m children
• all non-leaf nodes except the root has m / 2 to m children
• a leaf node contains m / 2 - 1 keys to m - 1 keys
AVL
B Trees
Tree
NOTE:
B-Tree of order m (or) m-way tree (or) m / 2-m tree
Example:
B-Tree of order 3
3-way tree
2-3 tree
AVL
B Trees
Tree
NOTE:
B-Tree of order m (or) m-way tree (or) m / 2-m tree
Example:
B-Tree of order 4
4-way tree
2-3-4 tree ( Shortly 2-4 tree )
AVL
B Trees
Tree
NOTE:
B-Tree of order m (or) m-way tree (or) m / 2-m tree
Example:
B-Tree of order 5
5-way tree
3-4-5 tree ( Shortly 3-5 tree )
Inserting into a B-Tree
Attempt to insert the new key into a leaf
If this would result in that leaf becoming too big, split the leaf
into two, promoting the middle key to the leaf’s parent
If this would result in the parent becoming too big, split the
parent into two, promoting the middle key
This strategy might have to be repeated all the way to the top
If necessary, the root is split in two and the middle key is
promoted to a new root, making the tree one level higher
Inserting into a B-Tree
Assuming a 3-way B-Tree
29 12 29
STEP 3 : INSERT 52
29
12 29 52
12 52
Inserting into a B-Tree
Assuming a 3-way B-Tree
STEP 4 : INSERT 56 29
12 52 56
STEP 5 : INSERT 42
29 52
29
12 42 52 56 12 42 56
Exercise in Inserting a B-Tree
Insert the following keys to a 5-way B-tree:
3, 9, 7, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31, 35, 56
Removal from a B-tree
12 29 52
2 7 9 15 22 31 43 56 69 72
12 29 52 Delete 52
7 9 15 22 31 43 56 69 72
12 29 56
52
7 9 15 22 31 43 56 69 72
DELETE 72
12 29 56
Join back together
7 9 15 22 31 43 69 72
Delete 72:
Too few
keys!
Type #4: Too few keys in node and its siblings
12 29
7 9 15 22 31 43 56 69
Type #3: Enough siblings
DELETE 22
12 29
Demote root key and
promote leaf key
7 9 15 22 31 43 56 69
Delete 22
Type #3: Enough siblings
DELETE 22
12 29
Demote root key and
promote leaf key
7 9 15 22 31 43 56 69
Delete 22
Type #3: Enough siblings
12 31
7 9 15 29 43 56 69
Exercise in Inserting a B-Tree
Insert the following keys to a 5-way B-tree:
3, 9, 7, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31, 35, 56
Delete 45, 24, 31
ASSIGNMENT – 3 (PART-2)
Consider the 3-way search tree given below.
Insert 23, 45, 67, 87, 54, 32, and 11 in the tree.
Delete 9, 36, and 54 from it.
ASSIGNMENT – 3 (PART-3)
Create a B+ tree of order 5 for the following data arriving in
sequence:
90, 27, 7, 9, 18, 21, 3, 4, 16, 11, 21, 72