Unit 4 Multiways Tree
Unit 4 Multiways 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:
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:
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.
1. Search O(log n)
2. Insert O(log n)
3. Delete O(log n)
Splay Tree
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:
3) Zig-Zig Rotation:
5) Zig-Zag Rotation:
6) Zag-Zig Rotation:
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.