0% found this document useful (0 votes)
51 views16 pages

B Tree

The document discusses B-trees, which are self-balancing search trees that allow efficient insertion, deletion, and search operations for large datasets. B-trees keep all levels approximately the same height to ensure efficient operations. Each node can contain multiple keys and children. B-trees are commonly used in databases and file systems to store and retrieve large amounts of data efficiently.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views16 pages

B Tree

The document discusses B-trees, which are self-balancing search trees that allow efficient insertion, deletion, and search operations for large datasets. B-trees keep all levels approximately the same height to ensure efficient operations. Each node can contain multiple keys and children. B-trees are commonly used in databases and file systems to store and retrieve large amounts of data efficiently.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

B-tree

1. "B-tree" stands for "balanced tree," which means that the tree attempts to keep all levels
approximately the same height to ensure efficient operations.
2. A B-tree is a self-balancing search tree data structure that maintains sorted data and
allows efficient insertion, deletion, and search operations.
3. It is commonly used in file systems and databases to store and retrieve large amounts of
data.
4. In B-tree each node can contain more than one key and can have more than two children
5. It is a generalized form of the binary search tree.it is also known as a height-balanced m-
way tree.

PROPERTIES.

 Every node in a B-Tree contains at most m children.


 Every node in a B-Tree except the root node and the leaf node contain at least m/2
children.
 The root nodes must have at least 2 nodes.
 All leaf nodes must be at the same level.
 It is not necessary that, all the nodes contain the same number of children but, each node
must have m/2 number of nodes.

A B tree of order 4 is shown in the following image.


Worst case Time complexity: Θ(log n)

Average case Time complexity: Θ(log n)

Best case Time complexity: Θ(log n)

Average case Space complexity: Θ(n)

Worst case Space complexity: Θ(n)

ADVANTAGES

B-trees offer several advantages as a data structure:

 Efficient for Large Datasets


 Fast Search Operations
 Efficient Disk Access.
 Self-Balancing

DISADVANTAGES

 Increased Complexity
 Higher Memory Overhead

B Tree Applications

 databases and file systems


 to store blocks of data (secondary storage media)
 multilevel indexing
B+ tree

 A B+ tree is an advanced form of a self-balancing tree in which all the


values are present in the leaf level.
 B+ Tree is an extension of B Tree which allows efficient insertion, deletion
and search operations.
 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.
 The internal nodes of B+ tree are often called index nodes.
 A B+ tree of order 3 is shown in the following figure.
Properties of B+ Tree in Data Structure 

 Every node in a B+ Tree contains at most “m” children.


 Every node in a B+ Tree, except the root node and the leaf node contain at
least “m/2” children.
 Nodes can have a maximum of (m-1) keys.
 Nodes can have a minimum of ⌈m/2⌉-1 keys

Advantages of B+ Tree

 Records can be fetched in equal number of disk accesses.


 Height of the tree remains balanced and less as compare to B tree.
 We can access the data stored in a B+ tree sequentially as well as directly.
 Keys are used for indexing.
 Faster search queries as the data is stored only on the leaf nodes.

Disadvantages of the B+ tree in data structure

 The difficulty of traversing the keys sequentially is the main disadvantage of


B-tree

Application of B+ tree in data structure

 Indexing at Several Levels


 It has faster operations on the tree compared to b tree(insertion, deletion,
search)
 Database indexing
B Tree VS B+ Tree

S B Tree B+ Tree
N
1 Search keys can not be repeatedly stored. Redundant search keys can be present.
2 Data can be stored in leaf nodes as well Data can only be stored on the leaf
as internal nodes nodes.
3 Searching for some data is a slower Searching is comparatively faster as
process since data can be found on data can only be found on the leaf
internal nodes as well as on the leaf nodes.
nodes.
4 Deletion of internal nodes are so Deletion will never be a complexed
complicated and time consuming. process since element will always be
deleted from the leaf nodes.
5 Leaf nodes can not be linked together. Leaf nodes are linked together to make
the search operations more efficient.
AVL TREE

 AVL Tree can be defined as height balanced binary search tree in which
each node is associated with a balance factor which is calculated by
subtracting the height of its right sub-tree from that of its left sub-tree.

 Tree is said to be balanced if balance factor of each node is in between -1 to


1, otherwise, the tree will be unbalanced and need to be balanced.

Balance Factor (k) = height (left(k)) - height (right(k))


 If balance factor of any node is 1, it means that the left sub-tree is one level
higher than the right sub-tree.

 If balance factor of any node is 0, it means that the left sub-tree and right
sub-tree contain equal height.

 If balance factor of any node is -1, it means that the left sub-tree is one level
lower than the right sub-tree.

EXAMPLE
AVL Tree Rotations

In AVL tree, after performing operations like insertion and deletion we need to
check the balance factor of every node in the tree. If every node satisfies the
balance factor condition then we conclude the operation otherwise we must make it
balanced. Whenever the tree becomes imbalanced due to any operation we
use rotation operations to make the tree balanced.

Single Left Rotation (LL Rotation)


In LL Rotation, every node moves one position to left from the current position. To
understand LL Rotation, let us consider the following insertion operation in AVL
Tree

Single Right Rotation (RR Rotation)

In RR Rotation, every node moves one position to right from the current position.
To understand RR Rotation, let us consider the following insertion operation in
AVL Tree

Left Right Rotation (LR Rotation)

The LR Rotation is a sequence of single left rotation followed by a single right


rotation. In LR Rotation, at first, every node moves one position to the left and one
position to right from the current position. To understand LR Rotation, let us
consider the following insertion operation in AVL Tree

Right Left Rotation (RL Rotation)

The RL Rotation is sequence of single right rotation followed by single left


rotation. In RL Rotation, at first every node moves one position to right and one
position to left from the current position. To understand RL Rotation, let us
consider the following insertion operation in AVL Tree

Complexity

Algorithm Average case Worst case


Space o(n) o(n)

Search o(log n) o(log n)

Insert o(log n) o(log n)

Delete o(log n) o(log n)

Applications of AVL Tree:

1. It is used to index huge records in a database and also to efficiently search in


that.
2. For all types of in-memory collections, including sets and dictionaries, AVL
Trees are used.
3. Database applications, where insertions and deletions are less common but
frequent data lookups are necessary
4. Software that needs optimized search.
5. It is applied in corporate areas and storyline games.

Advantages of AVL Tree:

1. AVL trees can self-balance themselves.


2. It is surely not skewed.
3. It provides faster lookups than Red-Black Trees
4. Better searching time complexity compared to other trees like binary tree.
5. Height cannot exceed log(N), where, N is the total number of nodes in the tree.

Disadvantages of AVL Tree:

1. It is difficult to implement.
2. It has high constant factors for some of the operations.
3. Less used compared to Red-Black trees.
4. Due to its rather strict balance, AVL trees provide complicated insertion and
removal operations as more rotations are performed.
5. Take more processing for balancing

Splay Tree
1. Splay Tree is a self - adjusted Binary Search Tree in which every operation
on element rearranges the tree so that the element is placed at the root
position of the tree.
2. Every Splay tree must be a binary search tree but it is need not to be
balanced tree.
3. In a splay tree, every operation is performed at the root of the tree. All the
operations in splay tree are involved with a common operation
called "Splaying".
4. In splay tree, to splay any element we use the following rotation operations...

Rotations in Splay Tree

1. Zig Rotation

2. Zag Rotation
3. Zig - Zig Rotation

4. Zag - Zag Rotation

5. Zig - Zag Rotation

6. Zag - Zig Rotation

Zig Rotation

The Zig Rotation in splay tree is similar to the single right rotation in AVL Tree
rotations. In zig rotation, every node moves one position to the right from its
current position. Consider the following example

Zag Rotation

The Zag Rotation in splay tree is similar to the single left rotation in AVL Tree
rotations. In zag rotation, every node moves one position to the left from its current
position. Consider the following example
Zig-Zig Rotation

The Zig-Zig Rotation in splay tree is a double zig rotation. In zig-zig rotation,


every node moves two positions to the right from its current position. Consider the
following example

Zag-Zag Rotation

The Zag-Zag Rotation in splay tree is a double zag rotation. In zag-zag rotation,


every node moves two positions to the left from its current position. Consider the
following example
Zig-Zag Rotation

The Zig-Zag Rotation in splay tree is a sequence of zig rotation followed by zag


rotation. In zig-zag rotation, every node moves one position to the right followed
by one position to the left from its current position. Consider the following
example

Zag-Zig Rotation

The Zag-Zig Rotation in splay tree is a sequence of zag rotation followed by zig


rotation. In zag-zig rotation, every node moves one position to the left followed by
one position to the right from its current position. Consider the following example
Advantages

 Splaying ensures that frequently accessed elements stay near the root of the
tree so that they are easily accessible..
 The average case performance of splay trees is comparable to other fully-
balanced trees: O(log n).
 Splay trees do not need bookkeeping data; therefore, they have a small
memory footprint.

Disadvantages

 the worst-case performance of a splay tree is O(n).


 Multithreaded operations can be complicated .

Applications of the splay tree:

 Caching
 Database Indexing
 File Systems
 Data Compression
 Text Processing
 Graph Algorithms
 Online Gaming

You might also like