0% found this document useful (0 votes)
28 views15 pages

B Treeb+ Tree

Uploaded by

Irfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views15 pages

B Treeb+ Tree

Uploaded by

Irfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Department of Computer Science and Engineering (CSE)

TOPICS TO BE COVERED

• INTRODUCTION

• CONSTRUCTING A B-TREE

• INSERTING IN A B-TREE

• DELETING FROM A B-TREE

• DIFFERENCE BETWEEN B & B+ TREES

University Institute of Engineering (UIE) 2


Department of Computer Science and Engineering (CSE)

Introduction to B tree
B tree is used to index the data and provides fast access to
the actual data stored on the disks since, the access to value
stored in a large database that is stored on a disk is a very
time consuming process.
One of the main reason of using B tree is its capability to
store large number of keys in a single node and large key
values by keeping the height of tree relatively small.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)
B-Tree
A B-Tree of order m is an m-way tree(i.e. a tree where each node may have up to(maximum)
m children) in which:
The maximum number of keys in each non-leaf node is one less than the number of its children. (m-1)
All leaves are on the same level.
All non-leaf(internal) nodes and external nodes (leaf) except the root have at least Гm/2˥ children or in
other words Гm/2˥ -1 keys.

Suppose m=5.
•Number of keys in internal node is 1 less than its number of children.
•All internal node except the root have at least Г5/2˥ = 3 children (or at least 2 keys)
•A leaf node contains not more than 4 keys.
•Leaf node must contain at least Г5/2˥ - 1 = 2 keys.

University Institute of Engineering (UIE) 4


Department of Computer Science and Engineering (CSE)
Constructing a B-Tree
Suppose we start with an empty B-tree and keys arrive in the following order:
1, 12, 8, 2, 25, 6, 14, 28, 17, 7, 52, 16, 48, 68, 3, 26, 29, 53, 55, 45

Construct a B-tree of order 5. That means each node can have upto 5 childrens.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)
Constructing a B-Tree(contd..)

University Institute of Engineering (UIE) 6


Department of Computer Science and Engineering (CSE)
Constructing a B-Tree(contd..)

University Institute of Engineering (UIE) 7


Department of Computer Science and Engineering (CSE)
Constructing a B-Tree(contd..)

University Institute of Engineering (UIE) 8


Department of Computer Science and Engineering (CSE)
Inserting into a B-Tree

1. Attempt to insert the new key into a leaf.


2. If this would result in that leaf becoming too big, split the leaf into 2, promoting the middle key to
the leaf’s parent.
3. If this would result in the parent becoming too big, split the parent into two, promoting the middle
key.
4. This strategy must have to be repeated all the way to the top.
5. If necessary, the root is split in two and the middle key is promoted to a new root, making the tree
one level higher.

University Institute of Engineering (UIE) 9


Department of Computer Science and Engineering (CSE)

Practice???

B tree of order 3:
The elements to be inserted are 4, 2, 20, 10, 1, 14, 7, 11, 3, 8.

B Tree of order 4:
5, 3, 21, 9, 1, 13, 2, 7, 10, 12, 4, 8

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Deletion from B-Tree


• During insertion, the key always goes into a leaf. For deletion we wish to remove from a
leaf.
• There are 3 possible ways we can do this:
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 that its predecessor or successor will be in a
leaf. In this case, can we delete the key & promote the predecessor or successor key to the
non-leaf deleted key’s position.
3. If (1) & (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:
• If one of them has more than the minimum number of keys then we can promote
one of its keys to the parent and take the parent key into our lacking leaf.
• If neither of them has more than the minimum number of keys then the lacking
leaf and one of its neighbors 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 leaves
the parent with too few keys then we repeat the process up to the root itself, if required.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

B+ Trees
• In B Tree, Keys and records both can be stored in the internal as well
as leaf nodes. Whereas, in B+ tree, records (data) can only be stored
on the leaf nodes while internal nodes can only store the key values.

• The leaf nodes of a B+ tree are linked together in the form of a singly
linked lists to make the search queries more efficient.

• B+ Tree are used to store the large amount of data which can not be
stored in the main memory. Due to the fact that, size of main
memory is always limited, the internal nodes (keys to access
records) of the B+ tree are stored in the main memory whereas, leaf
nodes are stored in the secondary memory.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

The leaf nodes of a B+ tree are linked together in the form


of a singly linked lists to make the search queries more
efficient.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Application of B & B+ Tress???

Differences between B & B+ Tress???

University Institute of Engineering (UIE)


Department of Computer and Communication Engineering (CCE)

References
1. Li`pschutz, Seymour, “Data Structures”, Schaum's Outline Series,
Tata McGraw Hill.
2. Data structure and algorithm by Narasimha Karumanchi.
3. www.tutorialspoint.com
4. www.geeksforgeeks.com

University Institute of Engineering (UIE) 15


Department of Computer and Communication Engineering (CCE)

Books Recommended
• Goodrich, Michael T., Tamassia, Roberto, and Mount, David M.,
“Data Structures and Algorithms in C++”, Wiley Student Edition.
• Aho, Alfred V., Ullman, Jeffrey D., Hopcroft ,John E. “Data
Structures and Algorithms”, Addison Wesley
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series,
Tata McGraw Hill.
• Gilberg/Forouzan,” Data Structure with C ,Cengage Learning.
• Augenstein,Moshe J , Tanenbaum, Aaron M, “Data Structures
using C and C++”, Prentice Hall of India

University Institute of Engineering (UIE)

You might also like