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

Btree DB

Uploaded by

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

Btree DB

Uploaded by

mani1449336
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

 Sign In
 Home
 Saved Videos
 Courses

Data Structures and Algorithms
ML & Data Science
Web Development
Languages
Interview Corner
CS Subjects
Jobs
Practice
Contests
 GBlog
 Puzzles
 What's New ?
Change Language
 DSA Tutorial
 Data Structures
 Algorithms
 Array
 Strings
 Linked List
 Stack
 Queue
 Tree
 Graph
 Searching
 Sorting
 Recursion
 Dynamic Programming
 Binary Tree
 Binary Search Tree
 Heap
 Hashing
 Divide & Conquer
 Mathematical
 Geometric
 Bitwise
 Greedy
 Backtracking
 Branch and Bound
 Matrix
 Pattern Searching
 Randomized
Open In App

Introduction of B-Tree
Last Updated : 28 Dec, 2023



The limitations of traditional binary search trees can be frustrating.
Meet the B-Tree, the multi-talented data structure that can handle
massive amounts of data with ease. When it comes to storing and
searching large amounts of data, traditional binary search trees can
become impractical due to their poor performance and high memory
usage. B-Trees, also known as B-Tree or Balanced Tree, are a type of
self-balancing tree that was specifically designed to overcome these
limitations.
Unlike traditional binary search trees, B-Trees are characterized by the
large number of keys that they can store in a single node, which is why
they are also known as “large key” trees. Each node in a B-Tree can
contain multiple keys, which allows the tree to have a larger branching
factor and thus a shallower height. This shallow height leads to less
disk I/O, which results in faster search and insertion operations. B-
Trees are particularly well suited for storage systems that have slow,
bulky data access such as hard drives, flash memory, and CD-ROMs.
B-Trees maintains balance by ensuring that each node has a minimum
number of keys, so the tree is always balanced. This balance
guarantees that the time complexity for operations such as insertion,
deletion, and searching is always O(log n), regardless of the initial
shape of the tree.
Time Complexity of B-Tree:

Sr. No. Algorithm Time Complexity

1. Search O(log n)

2. Insert O(log n)

3. Delete O(log n)

Note: “n” is the total number of elements in the B-tree


Properties of B-Tree:
 All leaves are at the same level.
 B-Tree is defined by the term minimum degree ‘t‘. The value of
‘t‘ depends upon disk block size.
 Every node except the root must contain at least t-1 keys. The
root may contain a minimum of 1 key.
 All nodes (including root) may contain at most (2*t – 1) keys.
 Number of children of a node is equal to the number of keys in
it plus 1.
 All keys of a node are sorted in increasing order. The child
between two keys k1 and k2 contains all keys in the range
from k1 and k2.
 B-Tree grows and shrinks from the root which is unlike Binary
Search Tree. Binary Search Trees grow downward and also
shrink from downward.
 Like other balanced Binary Search Trees, the time complexity
to search, insert, and delete is O(log n).
 Insertion of a Node in B-Tree happens only at Leaf Node.

Following is an example of a B-Tree of minimum order 5


Note: that in practical B-Trees, the value of the minimum order is
much more than 5.
We can see in the above diagram that all the leaf nodes are at the
same level and all non-leafs have no empty sub-tree and have keys
one less than the number of their children.
Interesting Facts about B-Trees:
 The minimum height of the B-Tree that can exist with n
number of nodes and m is the maximum number of children of
a node can have is:
 The maximum height of the B-Tree that can exist with n
number of nodes and t is the minimum number of children

that a non-root node can have is:


and
Traversal in B-Tree:
Traversal is also similar to Inorder traversal of Binary Tree. We start
from the leftmost child, recursively print the leftmost child, then repeat
the same process for the remaining children and keys. In the end,
recursively print the rightmost child.
Search Operation in B-Tree:
Search is similar to the search in Binary Search Tree. Let the key to be
searched is k.
 Start from the root and recursively traverse down.
 For every visited non-leaf node,
o If the node has the key, we simply return the
node.
o Otherwise, we recur down to the appropriate
child (The child which is just before the first
greater key) of the node.
 If we reach a leaf node and don’t find k in the leaf node, then
return NULL.
Searching a B-Tree is similar to searching a binary tree. The algorithm
is similar and goes with recursion. At each level, the search is
optimized as if the key value is not present in the range of the parent
then the key is present in another branch. As these values limit the
search they are also known as limiting values or separation values. If
we reach a leaf node and don’t find the desired key then it will display
NULL.
Algorithm for Searching an Element in a B-Tree:-
C++

struct Node {

int n;

int key[MAX_KEYS];

Node* child[MAX_CHILDREN];

bool leaf;

};

Node* BtreeSearch(Node* x, int k) {

int i = 0;

while (i < x->n && k > x->key[i])


{

i++;

if (i < x->n && k == x->key[i]) {

return x;

if (x->leaf) {

return nullptr;

return BtreeSearch(x->child[i],
k);

C
Java
Python3
C#
Javascript
Examples:
Input: Search 120 in the given B-Tree.

Solution:
In this example, we can see that our search was reduced by just
limiting the chances where the key containing the value could be
present. Similarly if within the above example we’ve to look for 180,
then the control will stop at step 2 because the program will find that
the key 180 is present within the current node. And similarly, if it’s to
seek out 90 then as 90 < 100 so it’ll go to the left subtree
automatically, and therefore the control flow will go similarly as shown
within the above example.
Below is the implementation of the above approach:
C++

// C++ implementation of search() and


traverse() methods

#include <iostream>

using namespace std;

// A BTree node

class BTreeNode {

int* keys; // An array of keys

int t; // Minimum degree (defines


the range for number

// of keys)

BTreeNode** C; // An array of
child pointers

int n; // Current number of keys

bool leaf; // Is true when node


is leaf. Otherwise false

public:

BTreeNode(int _t, bool _leaf); //


Constructor

// A function to traverse all


nodes in a subtree rooted

// with this node

void traverse();

// A function to search a key in


the subtree rooted with

// this node.

BTreeNode*
search(int k); // returns NULL if
k is not present.

// Make the BTree friend of this


so that we can access

// private members of this class


in BTree functions

friend class BTree;

};

// A BTree

class BTree {

BTreeNode* root; // Pointer to


root node

int t; // Minimum degree

public:

// Constructor (Initializes tree


as empty)

BTree(int _t)

root = NULL;

t = _t;

// function to traverse the tree

void traverse()

if (root != NULL)

root->traverse();
}

// function to search a key in


this tree

BTreeNode* search(int k)

return (root == NULL) ?


NULL : root->search(k);

};

// Constructor for BTreeNode class

BTreeNode::BTreeNode(int _t, bool


_leaf)

// Copy the given minimum degree


and leaf property

t = _t;

leaf = _leaf;

// Allocate memory for maximum


number of possible keys

// and child pointers

keys = new int[2 * t - 1];

C = new BTreeNode*[2 * t];

// Initialize the number of keys


as 0

n = 0;

}
// Function to traverse all nodes in
a subtree rooted with

// this node

void BTreeNode::traverse()

// There are n keys and n+1


children, traverse through n

// keys and first n children

int i;

for (i = 0; i < n; i++) {

// If this is not leaf, then


before printing key[i],

// traverse the subtree


rooted with child C[i].

if (leaf == false)

C[i]->traverse();

cout << " " << keys[i];

// Print the subtree rooted with


last child

if (leaf == false)

C[i]->traverse();

// Function to search key k in


subtree rooted with this node

BTreeNode* BTreeNode::search(int k)

{
// Find the first key greater
than or equal to k

int i = 0;

while (i < n && k > keys[i])

i++;

// If the found key is equal to


k, return this node

if (keys[i] == k)

return this;

// If the key is not found here


and this is a leaf node

if (leaf == true)

return NULL;

// Go to the appropriate child

return C[i]->search(k);

Java
Python3
C#
Javascript

Note: The above code doesn’t contain the driver program. We will be
covering the complete program in our next post on B-Tree Insertion.
There are two conventions to define a B-Tree, one is to define by
minimum degree, second is to define by order. We have followed the
minimum degree convention and will be following the same in coming
posts on B-Tree. The variable names used in the above program are
also kept the same
Applications of B-Trees:
 It is used in large databases to access data stored on the disk
 Searching for data in a data set can be achieved in
significantly less time using the B-Tree
 With the indexing feature, multilevel indexing can be achieved.
 Most of the servers also use the B-tree approach.
 B-Trees are used in CAD systems to organize and search
geometric data.
 B-Trees are also used in other areas such as natural language
processing, computer networks, and cryptography.
Advantages of B-Trees:
 B-Trees have a guaranteed time complexity of O(log n) for
basic operations like insertion, deletion, and searching, which
makes them suitable for large data sets and real-time
applications.
 B-Trees are self-balancing.
 High-concurrency and high-throughput.
 Efficient storage utilization.
Disadvantages of B-Trees:
 B-Trees are based on disk-based data structures and can have
a high disk usage.
 Not the best for all cases.
 Slow in comparison to other data structures.
Insertion and Deletion:
B-Tree Insertion
B-Tree Deletion

Are you a student in Computer Science or an employed professional


looking to take up the GATE 2025 Exam? Of course, you can get a
good score in it but to get the best score our GATE CS/IT 2025 - Self-
Paced Course is available on GeeksforGeeks to help you with its
preparation. Get comprehensive coverage of all topics of GATE,
detailed explanations, and practice questions for study. Study at
your pace. Flexible and easy-to-follow modules. Do well in GATE to
enhance the prospects of your career. Enroll now and let your journey
to success begin!
GeeksforGeeks

163

Next Article
Insert Operation in B-Tree

Similar Reads

Complexity of different operations in Binary tree,


Binary Search Tree and AVL tree
In this article, we will discuss the complexity of different operations
in binary trees including BST and AVL trees. Before understanding
this article, you should have a basic idea about Binary Tree, Binary
Search Tree, and AVL Tree. The main operations in a binary tree are:
search, insert and delete. We will see the worst-case time
complexity of t
4 min read

Convert a Generic Tree(N-array Tree) to Binary Tree


Prerequisite: Generic Trees(N-array Trees) In this article, we will
discuss the conversion of the Generic Tree to a Binary Tree.
Following are the rules to convert a Generic(N-array Tree) to a Binary
Tree: The root of the Binary Tree is the Root of the Generic Tree.The
left child of a node in the Generic Tree is the Left child of that node
in the B
13 min read

Introduction to R-tree
R-tree is a tree data structure used for storing spatial data indexes in
an efficient manner. R-trees are highly useful for spatial data queries
and storage. Some of the real-life applications are mentioned below:
Indexing multi-dimensional information.Handling geospatial
coordinates.Implementation of virtual maps.Handling game data.
Example: R-Tre
1 min read

Introduction to Height Balanced Binary Tree


A height-balanced binary tree is defined as a binary tree in which
the height of the left and the right subtree of any node differ by not
more than 1. AVL tree, red-black tree are examples of height-
balanced trees. Conditions for Height-Balanced Binary Tree:
Following are the conditions for a height-balanced binary tree: The
difference between the
5 min read

Introduction to Log structured merge (LSM) Tree


B+ Trees and LSM Trees are two basic data structures when we talk
about the building blocks of Databases. B+ Trees are used when we
need less search and insertion time and on the other hand, LSM
trees are used when we have write-intensive datasets and reads are
not that high. This article will teach about Log Structured Merge Tree
aka LSM Tree. LSM
3 min read

Introduction to Li Chao Tree


A Li Chao tree (also known as a Dynamic Convex Hull or Segment
Tree with lazy propagations) is a data structure that allows for
efficient dynamic maintenance of the convex hull of a set of points
in a 2D plane. The Li Chao tree allows for dynamic insertion,
deletion, and query operations on the set of points, and can be used
in a variety of geometr
14 min read
Introduction to Degenerate Binary Tree
Every non-leaf node has just one child in a binary tree known as a
Degenerate Binary tree. The tree effectively transforms into a linked
list as a result, with each node linking to its single child. When a
straightforward and effective data structure is required, degenerate
binary trees, a special case of binary trees, may be employed. For
instance
8 min read

Gomory-Hu Tree | Set 1 (Introduction)


Background : In a flow network, an s-t cut is a cut that requires the
source ‘s’ and the sink ‘t’ to be in different subsets, and it consists of
edges going from the source’s side to the sink’s side. The capacity
of an s-t cut is defined by the sum of capacity of each edge in the
cut-set. (Source: Wiki). Given a two vertices, s and t, we can find m
3 min read

Persistent Segment Tree | Set 1 (Introduction)


Prerequisite : Segment Tree Persistency in Data Structure Segment
Tree is itself a great data structure that comes into play in many
cases. In this post we will introduce the concept of Persistency in
this data structure. Persistency, simply means to retain the changes.
But obviously, retaining the changes cause extra memory
consumption and hence a
15+ min read

ScapeGoat Tree | Set 1 (Introduction and Insertion)


A ScapeGoat tree is a self-balancing Binary Search Tree like AVL
Tree, Red-Black Tree, Splay Tree, ..etc. Search time is O(Log n) in
worst case. Time taken by deletion and insertion is amortized O(Log
n)The balancing idea is to make sure that nodes are ? size balanced.
? size balanced means sizes of left and right subtrees are at most ? *
(Size of
15+ min read
Palindromic Tree | Introduction & Implementation
Palindromic Tree is a tree-based data structure designed for efficient
string processing tasks such as pattern matching, string searching,
and spell-checking. It is an advanced data structure based on a trie
that is optimized for palindromic strings. A Palindromic Tree is a trie-
like data structure that stores all the substrings of a given string i
15+ min read

BK-Tree | Introduction & Implementation


BK-Tree is a data structure used for efficient searching of words that
are close to a target word in terms of their Levenshtein distance (or
edit distance). It is a tree-like data structure, where each node
represents a word and its children represent words that are one edit
distance away. A BK-Tree is constructed by inserting words into an
initial
15+ min read

Introduction to Link-Cut Tree


Link Cut Trees (LCT) is a data structure that allows for efficient
dynamic maintenance of trees. It is a type of self-adjusting data
structure that allows for efficient manipulation of trees, such as link
and cut operations, find-root, and access. The implementation of an
LCT typically consists of a set of nodes, each representing a tree or a
subtr
15 min read

Introduction of B+ Tree
B + Tree is a variation of the B-tree data structure. In a B + tree,
data pointers are stored only at the leaf nodes of the tree. In a B+
tree structure of a leaf node differs from the structure of internal
nodes. The leaf nodes have an entry for every value of the search
field, along with a data pointer to the record (or to the block that
contains
8 min read

Introduction to Merkle Tree


Merkle tree also known as hash tree is a data structure used for data
verification and synchronization. It is a tree data structure where
each non-leaf node is a hash of it's child nodes. All the leaf nodes
are at the same depth and are as far left as possible. It maintains
data integrity and uses hash functions for this purpose. Hash
Functions:
12 min read

Introduction to Splay tree data structure


Splay tree is a self-adjusting binary search tree data structure, which
means that the tree structure is adjusted dynamically based on the
accessed or inserted elements. In other words, the tree
automatically reorganizes itself so that frequently accessed or
inserted elements become closer to the root node. The splay tree
was first introduced by Da
15+ min read

Introduction to Finger search tree Data Structure


A finger search tree is a data structure that is designed to allow for
efficient search and access of data in a set or a sequence. It is a
type of binary search tree that uses a "finger" or a reference to a
particular element in the tree to quickly find and retrieve other
elements. In this article, we will explore the types, advantages,
disadvantag
15+ min read

Introduction to Tree Data Structure


Tree data structure is a specialized data structure to store data in
hierarchical manner. It is used to organize and store data in the
computer to be used more effectively. It consists of a central node,
structural nodes, and sub-nodes, which are connected via edges. We
can also say that tree data structure has roots, branches, and leaves
connected
15+ min read

Introduction to Binary Search Tree


Binary Search Tree is a data structure used in computer science for
organizing and storing data in a sorted manner. Binary search tree
follows all properties of binary tree and its left child contains values
less than the parent node and the right child contains values greater
than the parent node. This hierarchical structure allows for
efficient S
15+ min read

Introduction to Binary Tree


Binary Tree is a non-linear data structure where each node has at
most two children. In this article, we will cover all the basics of
Binary Tree, Operations on Binary Tree, its implementation,
advantages, disadvantages which will help you solve all the
problems based on Binary Tree. Table of Content What is Binary
Tree?Representation of Binary Tre
15+ min read

Article Tags :
 Advanced Data Structure
 DBMS
 DSA
 DBMS Indexing
+1 More

Practice Tags :
 Advanced Data Structure

Corporate & Communications Address:- A-143, 9th Floor, Sovereign Corporate Tower,
Sector- 136, Noida, Uttar Pradesh (201305) | Registered Address:- K 061, Tower K,
Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh,
201305
 Company
 About Us
 Legal
 In Media
 Contact Us
 Advertise with us
 GFG Corporate Solution
 Placement Training Program
 GeeksforGeeks Community
 Languages
 Python
 Java
 C++
 PHP
 GoLang
 SQL
 R Language
 Android Tutorial
 Tutorials Archive
 DSA
 Data Structures
 Algorithms
 DSA for Beginners
 Basic DSA Problems
 DSA Roadmap
 Top 100 DSA Interview Problems
 DSA Roadmap by Sandeep Jain
 All Cheat Sheets
 Data Science & ML
 Data Science With Python
 Data Science For Beginner
 Machine Learning
 ML Maths
 Data Visualisation
 Pandas
 NumPy
 NLP
 Deep Learning
 Web Technologies
 HTML
 CSS
 JavaScript
 TypeScript
 ReactJS
 NextJS
 Bootstrap
 Web Design
 Python Tutorial
 Python Programming Examples
 Python Projects
 Python Tkinter
 Web Scraping
 OpenCV Tutorial
 Python Interview Question
 Django
 Computer Science
 Operating Systems
 Computer Network
 Database Management System
 Software Engineering
 Digital Logic Design
 Engineering Maths
 Software Development
 Software Testing
 DevOps
 Git
 Linux
 AWS
 Docker
 Kubernetes
 Azure
 GCP
 DevOps Roadmap
 System Design
 High Level Design
 Low Level Design
 UML Diagrams
 Interview Guide
 Design Patterns
 OOAD
 System Design Bootcamp
 Interview Questions
 Inteview Preparation
 Competitive Programming
 Top DS or Algo for CP
 Company-Wise Recruitment Process
 Company-Wise Preparation
 Aptitude Preparation
 Puzzles
 School Subjects
 Mathematics
 Physics
 Chemistry
 Biology
 Social Science
 English Grammar
 Commerce
 World GK
 GeeksforGeeks Videos
 DSA
 Python
 Java
 C++
 Web Development
 Data Science
 CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

You might also like