You are on page 1of 28

Department of

Computer Science and Engineering

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

• In the structure, P0, P1, P2,…, Pn are pointers to the node’s


sub-trees and K0, K1, K2,…, Kn-1 are the key values of the
node. All the key values are stored in ascending order. That is,
Ki < Ki+1 for 0 ≤ i ≤ n-2.
MULTIWAY(M-WAY)
AVL Tree
SEARCH TREE

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

STEP 1 : INSERT 29 STEP 2 : INSERT 12

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

 1 - If the key is already in a leaf node, and removing it


doesn’t cause that leaf node to have too few keys, then
simply remove the key to be deleted.
 2 - If the key is not in a leaf then it is guaranteed (by the
nature of a B-tree) that its predecessor or successor will
be in a leaf -- in this case we can delete the key and
promote the predecessor or successor key to the non-
leaf deleted key’s position.
Removal from a B-tree (2)
 If (1) or (2) lead to a leaf node containing less than the
minimum number of keys then we have to look at the
siblings immediately adjacent to the leaf in question:
– 3: if one of them has more than the min. number of keys then
we can promote one of its keys to the parent and take the
parent key into our lacking leaf
– 4: if neither of them has more than the min. number of keys then
the lacking leaf and one of its neighbours can be combined with
their shared parent (the opposite of promoting a key) and the
new leaf will have the correct number of keys; if this step leave
the parent with too few keys then we repeat the process up to
the root itself, if required
Type #1: Simple leaf deletion
DELETE 2

12 29 52

2 7 9 15 22 31 43 56 69 72

Delete 2: Since there are enough


keys in the node, just delete it
Type #2: Simple non-leaf deletion
DELETE 52

12 29 52 Delete 52

7 9 15 22 31 43 56 69 72

Borrow the predecessor


Delete 52: Promote the or (in this case) successor
successor key 56 to the non-
leaf deleted key’s position
Type #2: Simple non-leaf deletion
DELETE 52

12 29 56
52

7 9 15 22 31 43 56 69 72

Borrow the predecessor


Delete 52: Promote the or (in this case) successor
successor key 56 to the non-
leaf deleted key’s position
Type #4: Too few keys in node and its siblings

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

You might also like