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

Unit 4 Multiways Tree

ADS

Uploaded by

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

Unit 4 Multiways Tree

ADS

Uploaded by

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

B Tree

B Tree is a self-balancing data structure that uses a set of rules to search,


insert, and delete data in a faster and more memory-efficient manner. The
following rules are followed to create a B Tree to accomplish this.
Rule 1: All leaf nodes must be at the same level.
Rule 2: All non-leaf nodes (except the root node) should have children at
least [m/2].
Rule 3: All nodes ( except the root node) should have at least [m/2] - 1
key.
Rule 4: If the root node is a leaf node(only node in the tree), it will have no
children and at least one key. If the root node is a non-leaf node, it will
have at least two children and one key.
Rule 5: A non-leaf node with n-1 key values should have n non-null
children.
From the below pictorial representation, we can obtain the properties of a
B-Tree:

From the definition, we can conclude that any node except the root node
in a B-Tree is half full, which avoids wastage of storage space. The B-Tree
is perfectly balanced, so the number of nodes accessed to find a key
decreases.
The following figure shows the minimum and a maximum number of
children in any non-root and non-leaf node of B-trees of different orders.
Order of the tree Minimum Children (MIN) Maximum Children(MAX)

4 2 4

5 3 5

M [M/2] M
Insertion into a B-tree
Inserting an element on a B-tree consists of two
events: searching the appropriate node to insert
the element and splitting the node if required.
Insertion operation always takes place in the
bottom-up approach.
Let us understand these events below.
Insertion Operation
1.If the tree is empty, allocate a root node
and insert the key.
2.Update the allowed number of keys in the
node.
3.Search the appropriate node for insertion.
4.If the node is full, follow the steps below.
5.Insert the elements in increasing order.
6.Now, there are elements greater than its
limit. So, split at the median.
7.Push the median key upwards and make the
left keys as a left child and the right keys as
a right child.
8.If the node is not full, follow the steps
below.
9.Insert the node in increasing order.

Example:

Build a B Tree of order 3 .The elements to be


inserted are 8, 9, 10, 11, 15, 20, 17.
Deletion in B-tree
While inserting a key, we had to ensure that the number of keys should
not exceed MAX. Similarly, while deleting, we must watch that the number
of keys in a node should not become less than MIN. While inserting, when
the keys exceed MAX, we split the node into two nodes, and the median
key went to the parent node; while deleting when the keys will become
less than MIN, we will combine two nodes into one.
Now let us see how all this is done by studying the deletion cases.
Deletion in a B-Tree can be classified into two cases:
1. Deletion from the leaf node.
2. Deletion from the non-leaf node.
Let us now elaborate on each case:
Deletion From Leaf Node.
It will be divided into various cases. Let us study every case in detail.
If Node Has More Than MIN Keys,
In this case, deletion is very straightforward, and the key can be very
easily deleted from the node by shifting other keys of the node.
If Node Has MIN Keys
After the deletion of a key, the node will have less than MIN keys and will
become an underflow node. In such a case, we can borrow a key from the
left or right sibling if any one of them has more than MIN keys.
Now you must be wondering which sibling to start from?
In our algorithm, we'll first try to borrow a key from the left sibling; if the
left sibling has only MIN keys, then we'll try to borrow from the right
sibling.
 When a key is borrowed from the left sibling, the separator key
in the parent is moved to the underflow node, and the last key
from the left sibling is moved to the parent.
 When a key is borrowed from the right sibling, the separator
key in the parent is moved to the underflow node, and
the first key from the right sibling is moved to the parent. All
the remaining keys in the right sibling are moved from one
position to the left.
Time for some examples:
 Delete 15 from the tree:
Here key 15 is to be deleted from the node [15, 19]. Since this node
has only MIN keys, we will try to borrow from its left
sibling[ 3,7,9,11], which has more than MIN keys.
The parent of these nodes is node [12, 20], and the separator key is
12. Hence, the last key of the left sibling (11) is moved to the place
of the separator key, which is moved to the underflow node.

The resulting tree after deletion of 15 will be:-

Note: The involvement of the separator key is necessary since if we


simply borrow 11 and put it at the place of 15, then the property of
the B-tree will be violated.
 Delete 19 from the tree:
As we can see above, key 19 will be deleted by borrowing the left
sibling, so key nine will be moved up to the parent node, and 11 will
be shifted to the underflow node. In the underflow node, the key 12
will be shifted to the right to make place for 11. The resultant tree
will look like this:

 Delete 45 from the tree:


As we can see in the diagram itself, the left sibling of the [45, 47] is
[35, 38], which has only MIN keys, so we can't borrow from it. Thus,
we will try to borrow from the right sibling, i.e., [65, 78, 80]. As
discussed above, while borrowing from the right sibling, the first key
of the node is moved to the parent node. In this case, the first key of
the right sibling(65) will be moved to the parent node, and the
separator key from the parent node(55) will be moved to the
underflow node.
In the underflow node, 47 is shifted left to make room for 55. In the
right sibling, 78 and 80 are moved to the left to fill the gap created
by the removal of 65.
The resulting tree will look like this:

Note: If both left and right siblings of the underflow node have MIN
keys, then we can’t borrow from any of the siblings. In this case, the
underflow node is combined with its left( or right ) sibling.
Let’s see an example for the same:
 Delete 47 from the tree:
If we try to delete 47 from the tree which has only MIN keys and if
we delete it, it leads to the underflow condition, so we'll try to
borrow from the left sibling [ 35, 38], which also has only MIN keys,
then we try to borrow from the right sibling [ 78, 80] which has
again MIN keys only. So, what should we do now?
We’ll combine the left sibling node with the key left in the current
node. For combining these two nodes, the separator key(40) from
the parent node will move down in the combined node.
But the parent node also has only MIN keys.
In this case, when the parent node becomes underflow, we will
borrow a key from its left sibling. But if we look at its left sibling, it
also has only MIN keys.
Here, the separator key ( 30) the root node comes down in the
combined node, which becomes the new root of the tree, and the
height of the tree decreases by one.
The resulting tree will look like this:

Deletion From Non-Leaf Node,


In this case, the successor key is copied at the place of the key to be
removed, and then the successor is deleted. The successor key is the
smallest key in the right subtree and will always be in the leaf node. So
this case reduces to the above-mentioned approaches.
Let’s retake the above B-tree example:

 Delete 12 from the tree:


The successor key of 12 is 15, this we’ll copy 15 at the place of 12,
and now our task reduces to deletion of 15 from the leaf node. This
deletion is performed by borrowing a key from the left sibling.
Now let us conclude the above discussion by looking at the
flowchart:
Red Black Tree:
Introduction:
When it comes to searching and sorting data, one of the most
fundamental data structures is the binary search tree. However,
the performance of a binary search tree is highly dependent on its
shape, and in the worst case, it can degenerate into a linear
structure with a time complexity of O(n). This is where Red Black
Trees come in, they are a type of balanced binary search tree that
use a specific set of rules to ensure that 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.
Red Black Trees are self-balancing, meaning that the tree adjusts
itself automatically after each insertion or deletion operation. It
uses a simple but powerful mechanism to maintain balance, by
coloring each node in the tree either red or black.
Red Black Tree-
Red-Black tree is a binary search tree in which every node is
colored with either red or black. It is a type of self balancing binary
search tree. It has a good efficient worst case running time
complexity.

Properties of Red Black Tree:

The Red-Black tree satisfies all the properties of binary search tree
in addition to that it satisfies following additional properties –
1. Root property: The root is black.
2. External property: Every leaf (Leaf is a NULL child of a node) is
black in Red-Black tree.
3. Internal property: The children of a red node are black. Hence
possible parent of red node is a black node.
4. Depth property: All the leaves have the same black depth.
5. Path property: Every simple path from root to descendant leaf
node contains same number of black nodes.
The result of all these above-mentioned properties is that the Red-
Black tree is roughly balanced.
Rules That Every Red-Black Tree Follows:
1. Every node has a color either red or black.
2. The root of the tree is always black.
3. There are no two adjacent red nodes (A red node cannot
have a red parent or red child).
4. Every path from a node (including root) to any of its
descendants NULL nodes has the same number of black
nodes.
5. Every leaf (e.i. NULL node) must be colored BLACK.
Why Red-Black Trees?
Most of the BST operations (e.g., search, max, min, insert, delete..
etc) take O(h) time where h is the height of the BST. The cost of
these operations may become O(n) for a skewed Binary tree. If we
make sure that the height of the tree remains O(log n) after every
insertion and deletion, then we can guarantee an upper bound of
O(log n) for all these operations. The height of a Red-Black tree is
always O(log n) where n is the number of nodes in the tree.

Sr. Algorith Time


No. m Complexity

1. Search O(log n)

2. Insert O(log n)

3. Delete O(log n)

“n” is the total number of elements in the red-black tree.


Comparison with AVL Tree:
The AVL trees are more balanced compared to Red-Black Trees, but
they may cause more rotations during insertion and deletion. So if
your application involves frequent insertions and deletions, then
Red-Black trees should be preferred. And if the insertions and
deletions are less frequent and search is a more frequent
operation, then AVL tree should be preferred over the Red-Black
Tree.
How does a Red-Black Tree ensure balance?
A simple example to understand balancing is, that a chain of 3
nodes is not possible in the Red-Black tree. We can try any
combination of colors and see if all of them violate the Red-Black
tree property.
Proper structure of three noded Red-black tree

Interesting points about Red-Black Tree:


1. The black height of the red-black tree is the number of
black nodes on a path from the root node to a leaf node.
Leaf nodes are also counted as black nodes. So, a red-black
tree of height h has black height >= h/2.
2. Height of a red-black tree with n nodes is h<= 2 log 2(n +
1).
3. All leaves (NIL) are black.
4. The black depth of a node is defined as the number of
black nodes from the root to that node i.e the number of
black ancestors.
5. Every red-black tree is a special case of a binary tree.
Black Height of a Red-Black Tree :
Black height is the number of black nodes on a path from the root
to a leaf. Leaf nodes are also counted black nodes. From the above
properties 3 and 4, we can derive, a Red-Black Tree of height h
has black-height >= h/2.
Number of nodes from a node to its farthest descendant leaf is no
more than twice as the number of nodes to the nearest descendant
leaf.
Every Red Black Tree with n nodes has height <= 2Log2(n+1)
This can be proved using the following facts:
1. For a general Binary Tree, let k be the minimum number of
nodes on all root to NULL paths, then n >= 2 k – 1 (Ex. If k is
3, then n is at least 7). This expression can also be written
as k <= Log2(n+1).
2. From property 4 of Red-Black trees and above claim, we
can say in a Red-Black Tree with n nodes, there is a root to
leaf path with at-most Log2(n+1) black nodes.
3. From properties 3 and 5 of Red-Black trees, we can claim

least ⌊ n/2 ⌋ where n is the total number of nodes.


that the number of black nodes in a Red-Black tree is at
From the above points, we can conclude the fact that Red Black
Tree with n nodes has a height <= 2Log 2(n+1)
Search Operation in Red-black Tree:
As every red-black tree is a special case of a binary tree so the
searching algorithm of a red-black tree is similar to that of a binary
tree.
Advantages:
1. Red Black Trees have a guaranteed time complexity of
O(log n) for basic operations like insertion, deletion, and
searching.
2. Red Black Trees are self-balancing.
3. Red Black Trees can be used in a wide range of
applications due to their efficient performance and
versatility.
4. The mechanism used to maintain balance in Red Black
Trees is relatively simple and easy to understand.
Disadvantages:
1. Red Black Trees require one extra bit of storage for each
node to store the color of the node (red or black).
2. Complexity of Implementation.
3. Although Red Black Trees provide efficient performance for
basic operations, they may not be the best choice for
certain types of data or specific use cases.

Splay Tree

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.
1. The splay tree was first introduced by Daniel Dominic
Sleator and Robert Endre Tarjan in 1985. It has a simple
and efficient implementation that allows it to perform
search, insertion, and deletion operations in O(log n)
amortized time complexity, where n is the number of
elements in the tree.
2. The basic idea behind splay trees is to bring the most
recently accessed or inserted element to the root of the
tree by performing a sequence of tree rotations, called
splaying. Splaying is a process of restructuring the tree by
making the most recently accessed or inserted element the
new root and gradually moving the remaining nodes closer
to the root.
3. Splay trees are highly efficient in practice due to their self-
adjusting nature, which reduces the overall access time for
frequently accessed elements. This makes them a good
choice for applications that require fast and dynamic data
structures, such as caching systems, data compression,
and network routing algorithms.
4. However, the main disadvantage of splay trees is that they
do not guarantee a balanced tree structure, which may
lead to performance degradation in worst-case scenarios.
Also, splay trees are not suitable for applications that
require guaranteed worst-case performance, such as real-
time systems or safety-critical systems.
Overall, splay trees are a powerful and versatile data structure that
offers fast and efficient access to frequently accessed or inserted
elements. They are widely used in various applications and provide
an excellent tradeoff between performance and simplicity.
A splay tree is a self-balancing binary search tree, designed for
efficient access to data elements based on their key values.
 The key feature of a splay tree is that each time an
element is accessed, it is moved to the root of the tree,
creating a more balanced structure for subsequent
accesses.
 Splay trees are characterized by their use of rotations,
which are local transformations of the tree that change its
shape but preserve the order of the elements.
 Rotations are used to bring the accessed element to the
root of the tree, and also to rebalance the tree if it
becomes unbalanced after multiple accesses.
Operations in a splay tree:
 Insertion: To insert a new element into the tree, start by
performing a regular binary search tree insertion. Then,
apply rotations to bring the newly inserted element to the
root of the tree.
 Deletion: To delete an element from the tree, first locate it
using a binary search tree search. Then, if the element has
no children, simply remove it. If it has one child, promote
that child to its position in the tree. If it has two children,
find the successor of the element (the smallest element in
its right subtree), swap its key with the element to be
deleted, and delete the successor instead.
 Search: To search for an element in the tree, start by
performing a binary search tree search. If the element is
found, apply rotations to bring it to the root of the tree. If it
is not found, apply rotations to the last node visited in the
search, which becomes the new root.
 Rotation: The rotations used in a splay tree are either a
Zig or a Zig-Zig rotation. A Zig rotation is used to bring a
node to the root, while a Zig-Zig rotation is used to balance
the tree after multiple accesses to elements in the same
subtree.
Here’s a step-by-step explanation of the rotation
operations:
 Zig Rotation: If a node has a right child, perform a right
rotation to bring it to the root. If it has a left child, perform
a left rotation.
 Zig-Zig Rotation: If a node has a grandchild that is also
its child’s right or left child, perform a double rotation to
balance the tree. For example, if the node has a right child
and the right child has a left child, perform a right-left
rotation. If the node has a left child and the left child has a
right child, perform a left-right rotation.
 Note: The specific implementation details, including the
exact rotations used, may vary depending on the exact
form of the splay tree.
Rotations in Splay Tree
 Zig Rotation
 Zag Rotation
 Zig – Zig Rotation
 Zag – Zag Rotation
 Zig – Zag Rotation
 Zag – Zig Rotation

1) Zig Rotation:
The Zig Rotation in splay trees operates in a manner similar to the
single right rotation in AVL Tree rotations. This rotation results in
nodes moving one position to the right from their current location.
For example, consider the following scenario:
2) Zag Rotation:

The Zag Rotation in splay trees operates in a similar fashion to the


single left rotation in AVL Tree rotations. During this rotation, nodes
shift one position to the left from their current location. For
instance, consider the following illustration:

3) Zig-Zig Rotation:

The Zig-Zig Rotation in splay trees is a double zig rotation. This


rotation results in nodes shifting two positions to the right from
their current location. Take a look at the following example for a
better understanding:
4) Zag-Zag Rotation:

In splay trees, the Zag-Zag Rotation is a double zag rotation. This


rotation causes nodes to move two positions to the left from their
present position. For example:

5) Zig-Zag Rotation:

The Zig-Zag Rotation in splay trees is a combination of a zig


rotation followed by a zag rotation. As a result of this rotation,
nodes shift one position to the right and then one position to the
left from their current location. The following illustration provides a
visual representation of this concept:

6) Zag-Zig Rotation:

The Zag-Zig Rotation in splay trees is a series of zag rotations


followed by a zig rotation. This results in nodes moving one position
to the left, followed by a shift one position to the right from their
current location. The following illustration offers a visual
representation of this concept:

Drawbacks of splay tree data structure:

 Unbalanced Trees: Splay trees can become unbalanced


and inefficient if the tree is repeatedly rotated in the same
direction.
 Memory Usage: Splay trees can use a lot of memory
compared to other data structures because each node
contains additional information.
 Complexity: Splay trees can have a high time complexity
for basic operations such as insertion and deletion because
the trees need to be reorganized after every operation.
 Reorganization Overhead: The splaying operation
required in every operation can be time-consuming and
result in a high overhead.
 Limited Use Cases: Splay trees are not suitable for all
data structures and have limited use cases because they
don’t handle duplicate keys efficiently.

Applications of the splay tree:

 Caching: Splay trees can be used to implement cache


memory management, where the most frequently accessed
items are moved to the top of the tree for quicker access.
 Database Indexing: Splay trees can be used to index
databases for faster searching and retrieval of data.
 File Systems: Splay trees can be used to store file system
metadata, such as the allocation table, directory structure,
and file attributes.
 Data Compression: Splay trees can be used to compress
data by identifying and encoding repeating patterns.
 Text Processing: Splay trees can be used in text
processing applications, such as spell-checkers, where
words are stored in a splay tree for quick searching and
retrieval.
 Graph Algorithms: Splay trees can be used to implement
graph algorithms, such as finding the shortest path in a
weighted graph.
 Online Gaming: Splay trees can be used in online gaming
to store and manage high scores, leaderboards, and player
statistics.
Sure, here are some advantages and disadvantages of splay trees,
as well as some recommended books for learning more about the
topic:

Advantages of Splay Trees:

Splay trees have amortized time complexity of O(log n) for many


operations, making them faster than many other balanced tree
data structures in some cases.
Splay trees are self-adjusting, meaning that they automatically
balance themselves as items are inserted and removed. This can
help to avoid the performance degradation that can occur when a
tree becomes unbalanced.

Disadvantages of Splay Trees:

Splay trees can have worst-case time complexity of O(n) for some
operations, making them less predictable than other balanced tree
data structures like AVL trees or red-black trees.
Splay trees may not be suitable for certain applications where
predictable performance is required.

You might also like