Trees
Trees
e read the linear data structures like an array, linked list, stack and queue in which all
the elements are arranged in a sequential manner. The different data structures are used
for different kinds of data.
o What type of data needs to be stored?: It might be a possibility that a certain data
structure can be the best fit for some kind of data.
o Cost of operations: If we want to minimize the cost for the operations for the
most frequently performed operations. For example, we have a simple list on
which we have to perform the search operation; then, we can create an array in
which elements are stored in sorted order to perform the binary search. The
binary search works very fast for the simple list as it divides the search space into
half.
o Memory usage: Sometimes, we want a data structure that utilizes less memory.
A tree is also one of the data structures that represent hierarchical data. Suppose we
want to show the employees and their positions in the hierarchical form then it can be
represented as shown below:
The above tree shows the organization hierarchy of some company. In the above
structure, john is the CEO of the company, and John has two direct reports named
as Steve and Rohan. Steve has three direct reports named Lee, Bob, Ella where Steve is
a manager. Bob has two direct reports named Sal and Emma. Emma has two direct
reports named Tom and Raj. Tom has one direct report named Bill. This particular
logical structure is known as a Tree. Its structure is similar to the real tree, so it is
named a Tree. In this structure, the root is at the top, and its branches are moving in a
downward direction. Therefore, we can say that the Tree data structure is an efficient
way of storing the data in a hierarchical way.
o A tree data structure is a non-linear data structure because it does not store in a
sequential manner. It is a hierarchical structure as elements in a Tree are
arranged in multiple levels.
o In the Tree data structure, the topmost node is known as a root node. Each
node contains some data, and data can be of any type. In the above tree
structure, the node contains the name of the employee, so the type of data
would be a string.
o Each node contains some data and the link or reference of other nodes that can
be called children.
o Root: The root node is the topmost node in the tree hierarchy. In other words,
the root node is the one that doesn't have any parent. In the above structure,
node numbered 1 is the root node of the tree. If a node is directly linked to
some other node, it would be called a parent-child relationship.
o Child node: If the node is a descendant of any node, then the node is known as
a child node.
o Parent: If the node contains any sub-node, then that node is said to be the
parent of that sub-node.
o Sibling: The nodes that have the same parent are known as siblings.
o Leaf Node: - The node of the tree, which doesn't have any child node, is called a
leaf node. A leaf node is the bottom-most node of the tree. There can be any
number of leaf nodes present in a general tree. Leaf nodes can also be called
external nodes.
o Internal nodes: A node has atleast one child node known as an internal
o Recursive data structure: The tree is also known as a recursive data structure .
A tree can be defined as recursively because the distinguished node in a tree data
structure is known as a root node . The root node of the tree contains a link to
all the roots of its subtrees. The left subtree is shown in the yellow color in the
below figure, and the right subtree is shown in the red color. The left subtree can
be further split into subtrees shown in three different colors. Recursion means
reducing something in a self-similar manner. So, this recursive property of the
tree data structure is implemented in various applications.
o Number of edges: If there are n nodes, then there would n-1 edges. Each arrow
in the structure represents the link or path. Each node, except the root node,
will have atleast one incoming link known as an edge. There would be one link
for the parent-child relationship.
o Depth of node x: The depth of node x can be defined as the length of the path
from the root to the node x. One edge contributes one-unit length in the path.
So, the depth of node x can also be defined as the number of edges between the
root node and the node x. The root node has 0 depth.
o Height of node x: The height of node x can be defined as the longest path from
the node x to the leaf node.
Based on the properties of the Tree data structure, trees are classified into various
categories.
Implementation of Tree
The tree data structure can be created by creating the nodes dynamically with the help
of the pointers. The tree in the memory can be represented as shown below:
The above figure shows the representation of the tree data structure in the memory. In
the above structure, the node contains three fields. The second field stores the data; the
first field stores the address of the left child, and the third field stores the address of the
right child.
1. struct node
2. {
3. int data;
6. }
The above structure can only be defined for the binary trees because the binary tree
can have utmost two children, and generic trees can have more than two children. The
structure of the node for generic trees would be different as compared to the binary
tree.
Applications of trees
o Storing naturally hierarchical data: Trees are used to store the data in the
hierarchical structure. For example, the file system. The file system stored on the
disc drive, the file and folder are in the form of the naturally hierarchical data
and stored in the form of trees.
o Organize data: It is used to organize data for efficient insertion, deletion and
searching. For example, a binary tree has a logN time for searching an element.
o Trie: It is a special kind of tree that is used to store the dictionary. It is a fast and
efficient way for dynamic spell checking.
o B-Tree and B+Tree: B-Tree and B+Tree are the tree data structures used to
implement indexing in databases.
o Routing table: The tree data structure is also used to store the data in routing
tables in the routers.
o General tree: The general tree is one of the types of tree data structure. In the
general tree, a node can have either 0 or maximum n number of nodes. There is
no restriction imposed on the degree of the node (the number of nodes that a
node can contain). The topmost node in a general tree is known as a root node.
The children of the parent node are known as subtrees .
There can be n number of subtrees in a general tree. In the general tree, the
subtrees are unordered as the nodes in the subtree cannot be ordered.
Every non-empty tree has a downward edge, and these edges are connected to
the nodes known as child nodes . The root node is labeled with level 0. The
nodes that have the same parent are known as siblings .
o Binary tree: Here, binary name itself suggests two numbers, i.e., 0 and 1. In a
binary tree, each node in a tree can have utmost two child nodes. Here, utmost
means whether the node has 0 nodes, 1 node or 2 nodes.
To know more about the binary tree, click on the link given below:
https://www.javatpoint.com/binary-tree
o Binary Search tree: Binary search tree is a non-linear data structure in which one
node is connected to n number of nodes. It is a node-based data structure. A
node can be represented in a binary search tree with three fields, i.e., data part,
left-child, and right-child. A node can be connected to the utmost two child
nodes in a binary search tree, so the node contains two pointers (left child and
right child pointer).
Every node in the left subtree must contain a value less than the value of the root
node, and the value of each node in the right subtree must be bigger than the
value of the root node.
A node can be created with the help of a user-defined data type known as struct, as
shown below:
1. struct node
2. {
3. int data;
6. }
The above is the node structure with three fields: data field, the second field is the left
pointer of the node type, and the third field is the right pointer of the node type.
Binary Tree
The Binary tree means that the node can have maximum two children. Here, binary
name itself suggests that 'two'; therefore, each node can have either 0, 1 or 2 children.
o The height of the tree is defined as the longest path from the root node to the
leaf node. The tree which is shown above has a height equal to 3. Therefore, the
maximum number of nodes at height 3 is equal to (1+2+4+8) = 15. In general,
the maximum number of nodes possible at height h is (20 + 21 + 22+⋯.2h) =
2h+1 -1.
o If the number of nodes is minimum, then the height of the tree would be
maximum. Conversely, if the number of nodes is maximum, then the height of
the tree would be minimum.
As we know that,
n = 2h+1 -1
n+1 = 2h+1
log2(n+1) = log2(2h+1)
log2(n+1) = h+1
h = log 2(n+1) - 1
As we know that,
n = h+1
h= n-1
Advertisement
The full binary tree is also known as a strict binary tree. The tree can only be
considered as the full binary tree if each node must contain either 0 or 2 children. The
full binary tree can also be defined as the tree in which each node must contain 2
children except the leaf nodes.
o The number of leaf nodes is equal to the number of internal nodes plus 1. In the
above example, the number of internal nodes is 5; therefore, the number of leaf
nodes is equal to 6.
o The maximum number of nodes is the same as the number of nodes in the
binary tree, i.e., 2h+1 -1.
o The maximum height of the full binary tree can be computed as:
n= 2*h - 1
n+1 = 2*h
h = n+1/2
Complete Binary Tree
The complete binary tree is a tree in which all the nodes are completely filled except
the last level. In the last level, all the nodes must be as left as possible. In a complete
binary tree, the nodes should be added from the left.
The above tree is a complete binary tree because all the nodes are completely filled,
and all the nodes in the last level are added at the left first.
A tree is a perfect binary tree if all the internal nodes have 2 children, and all the leaf
nodes are at the same level.
The below tree is not a perfect binary tree because all the leaf nodes are not at the same
level.
Note: All the perfect binary trees are the complete binary trees as well as the full binary
tree, but vice versa is not true, i.e., all complete binary trees and full binary trees are the
perfect binary trees.
The degenerate binary tree is a tree in which all the internal nodes have only one
children.
The above tree is a degenerate binary tree because all the nodes have only one child. It
is also known as a right-skewed tree as all the nodes have a right child only.
The above tree is also a degenerate binary tree because all the nodes have only one
child. It is also known as a left-skewed tree as all the nodes have a left child only.
The balanced binary tree is a tree in which both the left and right trees differ by atmost
1. For example, AVL and Red-Black trees are balanced binary tree.
The above tree is not a balanced binary tree because the difference between the left
subtree and the right subtree is greater than 1.
A Binary tree is implemented with the help of pointers. The first node in the tree is
represented by the root pointer. Each node in the tree consists of three parts, i.e., data,
left pointer and right pointer.
In the above structure, data is the value, left pointer contains the address of the left
node, and right pointer contains the address of the right node.
The process of visiting the nodes is known as tree traversal. There are three types
traversals used to visit a node:
o Inorder traversal
o Preorder traversal
o Postorder traversal
Inorder traversal visits the node in the order: Left -> Root -> Right
Inorder(tree)
In the case of binary search trees (BST), Inorder traversal gives nodes in non-
decreasing order.
Preorder(tree)
Algorithm Postorder(tree)
In the above figure, we can observe that the root node is 40, and all the nodes of the left
subtree are smaller than the root node, and all the nodes of the right subtree are greater
than the root node.
Similarly, we can see the left child of root node is greater than its left child and smaller
than its right child. So, it also satisfies the property of binary search tree. Therefore, we
can say that the tree in the above image is a binary search tree.
Suppose if we change the value of node 35 to 55 in the above tree, check whether the
tree will be binary search tree or not.
In the above tree, the value of root node is 40, which is greater than its left child 30 but
smaller than right child of 30, i.e., 55. So, the above tree does not satisfy the property of
Binary search tree. Therefore, the above tree is not a binary search tree.
o Searching an element in the Binary search tree is easy as we always have a hint
that which subtree has the desired element.
o As compared to array and linked lists, insertion and deletion operations are
faster in BST.
Now, let's see the creation of binary search tree using an example.
Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50
o First, we have to insert 45 into the tree as the root of the tree.
o Then, read the next element; if it is smaller than the root node, insert it as the
root of the left subtree, and move to the next element.
o Otherwise, if the element is larger than the root node, then insert it as the root of
the right subtree.
Now, let's see the process of creating the Binary search tree using the given data
element. The process of creating the BST is shown below -
As 15 is smaller than 45, so insert it as the root node of the left subtree.
As 79 is greater than 45, so insert it as the root node of the right subtree.
Step 4 - Insert 90.
90 is greater than 45 and 79, so it will be inserted as the right subtree of 79.
55 is larger than 45 and smaller than 79, so it will be inserted as the left subtree of 79.
12 is smaller than 45 and 15 but greater than 10, so it will be inserted as the right subtree
of 10.
Step 8 - Insert 20.
20 is smaller than 45 but greater than 15, so it will be inserted as the right subtree of 15.
Now, the creation of binary search tree is completed. After that, let's move towards the
operations that can be performed on Binary search tree.
We can perform insert, delete and search operations on the binary search tree.
1. First, compare the element to be searched with the root element of the tree.
2. If root is matched with the target element, then return the node's location.
3. If it is not matched, then check whether the item is less than the root element, if
it is smaller than the root element, then move to the left subtree.
4. If it is larger than the root element, then move to the right subtree.
6. If the element is not found or not present in the tree, then return NULL.
Now, let's understand the searching in binary tree using an example. We are taking the
binary search tree formed above. Suppose we have to find node 20 from the below
tree.
Step1:
Step2:
Step3:
Now, let's see the algorithm to search an element in the Binary search tree.
3. return root
8. END if
9. Step 2 - END
Now let's understand how the deletion is performed on a binary search tree. We will
also see an example to delete an element from the given tree.
In a binary search tree, we must delete a node from the tree by keeping in mind that the
property of BST is not violated. To delete a node from BST, there are three possible
situations occur -
It is the simplest case to delete a node in BST. Here, we have to replace the leaf node
with NULL and simply free the allocated space.
We can see the process to delete a leaf node from BST in the below image. In below
image, suppose we have to delete node 90, as the node to be deleted is a leaf node, so it
will be replaced with NULL, and the allocated space will free.
When the node to be deleted has only one child
In this case, we have to replace the target node with its child, and then delete the child
node. It means that after replacing the target node with its child node, the child node
will now contain the value to be deleted. So, we simply have to replace the child node
with NULL and free up the allocated space.
We can see the process of deleting a node with one child from BST in the below image.
In the below image, suppose we have to delete the node 79, as the node to be deleted
has only one child, so it will be replaced with its child 55.
So, the replaced node 79 will now be a leaf node that can be easily deleted.
This case of deleting a node in BST is a bit complex among other two cases. In such a
case, the steps to be followed are listed as follows -
o First, find the inorder successor of the node to be deleted.
o After that, replace that node with the inorder successor until the target node is
placed at the leaf of tree.
o And at last, replace the node with NULL and free up the allocated space.
The inorder successor is required when the right child of the node is not empty. We
can obtain the inorder successor by finding the minimum element in the right child of
the node.
We can see the process of deleting a node with two children from BST in the below
image. In the below image, suppose we have to delete node 45 that is the root node, as
the node to be deleted has two children, so it will be replaced with its inorder
successor. Now, node 45 will be at the leaf of the tree so that it can be deleted easily.
A new key in BST is always inserted at the leaf. To insert an element in BST, we have to
start searching from the root node; if the node to be inserted is less than the root node,
then search for an empty location in the left subtree. Else, search for the empty location
in the right subtree and insert the data. Insert in BST is similar to searching, as we
always have to maintain the rule that the left subtree is smaller than the root, and right
subtree is larger than the root.
Now, let's see the process of inserting a node into BST using an example.
Let's see the time and space complexity of the Binary search tree. We will see the time
complexity for insertion, deletion, and searching operations in best case, average case,
and worst case.
1. Time Complexity
Operations Best case time Average case time Worst case time
complexity complexity complexity
2. Space Complexity
Insertion O(n)
Deletion O(n)
Search O(n)
If balance factor of any node is 1, it means that the left sub-tree is one level higher than
the right sub-tree.
PauseNext
Mute
Duration 18:10
Loaded: 11.01%
Fullscreen
Advertisement
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.
An AVL tree is given in the following figure. We can see that, balance factor associated
with each node is in between -1 and +1. therefore, it is an example of AVL tree.
Complexity
Due to the fact that, AVL tree is also a binary search tree therefore, all the operations
are performed in the same way as they are performed in a binary search tree. Searching
and traversing do not lead to the violation in property of AVL tree. However, insertion
and deletion are the operations which can violate this property and therefore, they
need to be revisited.
SN Operation Description
AVL tree controls the height of the binary search tree by not letting it to be skewed.
The time taken for all operations in a binary search tree of height h is O(h). However,
it can be extended to O(n) if the BST becomes skewed (i.e. worst case). By limiting
this height to log n, AVL tree imposes an upper bound on each operation to be O(log
n) where n is the number of nodes.
AVL Rotations
We perform rotation in AVL tree only in case if Balance Factor is other than -1, 0, and
1. There are basically four types of rotations which are as follows:
Where node A is the node whose balance Factor is other than -1, 0, 1.
The first two rotations LL and RR are single rotations and the next two rotations LR
and RL are double rotations. For a tree to be unbalanced, minimum height must be at
least 2, Let us understand each rotation
1. RR Rotation
When BST becomes unbalanced, due to a node is inserted into the right subtree of the
right subtree of A, then we perform RR rotation, RR rotation is an anticlockwise
rotation, which is applied on the edge below a node having balance factor -2
In above example, node A has balance factor -2 because a node C is inserted in the
right subtree of A right subtree. We perform the RR rotation on the edge below A.
2. LL Rotation
When BST becomes unbalanced, due to a node is inserted into the left subtree of the
left subtree of C, then we perform LL rotation, LL rotation is clockwise rotation, which
is applied on the edge below a node having balance factor 2.
In above example, node C has balance factor 2 because a node A is inserted in the left
subtree of C left subtree. We perform the LL rotation on the edge below A.
3. LR Rotation
Double rotations are bit tougher than single rotation which has already explained
above. LR rotation = RR rotation + LL rotation, i.e., first RR rotation is performed on
subtree and then LL rotation is performed on full tree, by full tree we mean the first
node from the path of inserted node whose balance factor is other than -1, 0, or 1.
State Action
4. RL Rotation
As already discussed, that double rotations are bit tougher than single rotation which
has already explained above. R L rotation = LL rotation + RR rotation, i.e., first LL
rotation is performed on subtree and then RR rotation is performed on full tree, by full
tree we mean the first node from the path of inserted node whose balance factor is
other than -1, 0, or 1.
H, I, J, B, A, E, C, F, D, G, K, L
1. Insert H, I, J
On inserting the above elements, especially in the case of H, the BST becomes
unbalanced as the Balance Factor of H is -2. Since the BST is right-skewed, we will
perform RR Rotation on node H.
2. Insert B, A
On inserting the above elements, especially in case of A, the BST becomes unbalanced
as the Balance Factor of H and I is 2, we consider the first node from the last inserted
node i.e. H. Since the BST from H is left-skewed, we will perform LL Rotation on
node H.
3. Insert E
On inserting E, BST becomes unbalanced as the Balance Factor of I is 2, since if we
travel from E to I we find that it is inserted in the left subtree of right subtree of I, we
will perform LR Rotation on node I. LR = RR + LL rotation
5. Insert G
On inserting G, BST become unbalanced as the Balance Factor of H is 2, since if we
travel from G to H, we find that it is inserted in the left subtree of right subtree of H, we
will perform LR Rotation on node I. LR = RR + LL rotation.
6. Insert K
On inserting K, BST becomes unbalanced as the Balance Factor of I is -2. Since the
BST is right-skewed from I to K, hence we will perform RR Rotation on the node I.
7. Insert L
On inserting the L tree is still balanced as the Balance Factor of each node is now either,
-1, 0, +1. Hence the tree is a Balanced AVL tree
B Tree
B Tree is a specialized m-way tree that can be widely used for disk access. A B-Tree of
order m can have at most m-1 keys and m children. 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 the tree relatively small.
A B tree of order m contains all the properties of an M way tree. In addition, it contains
the following properties.
2. Every node in a B-Tree except the root node and the leaf node contain at least
m/2 children.
While performing some operations on B Tree, any property of B Tree may violate such
as number of minimum children a node can have. To maintain the properties of B
Tree, the tree may split or join.
Operations
Searching :
Searching in B Trees is similar to that in Binary search tree. For example, if we search
for an item 49 in the following B Tree. The process will something like following :
1. Compare item 49 with root node 78. since 49 < 78 hence, move to its left sub-
tree.
Inserting
Insertions are done at the leaf node level. The following algorithm needs to be followed
in order to insert an item into B Tree.
1. Traverse the B Tree in order to find the appropriate leaf node at which the node
can be inserted.
2. If the leaf node contain less than m-1 keys then insert the element in the
increasing order.
3. Else, if the leaf node contains m-1 keys, then follow the following steps.
o If the parent node also contain m-1 number of keys, then split it too by
following the same steps.
Example:
Insert the node 8 into the B Tree of order 5 shown in the following image.
The node, now contain 5 keys which is greater than (5 -1 = 4 ) keys. Therefore split the
node from the median i.e. 8 and push it up to its parent node shown as follows.
Deletion
Deletion is also performed at the leaf nodes. The node which is to be deleted can either
be a leaf node or an internal node. Following algorithm needs to be followed in order
to delete a node from a B tree.
3. If the leaf node doesn't contain m/2 keys then complete the keys by taking the
element from eight or left sibling.
o If the left sibling contains more than m/2 elements then push its largest
element up to its parent and move the intervening element down to the
node where the key is deleted.
o If the right sibling contains more than m/2 elements then push its smallest
element up to the parent and move intervening element down to the
node where the key is deleted.
4. If neither of the sibling contain more than m/2 elements then create a new leaf
node by joining two leaf nodes and the intervening element of the parent node.
5. If parent is left with less than m/2 nodes then, apply the above process on the
parent too.
If the the node which is to be deleted is an internal node, then replace the node with its
in-order successor or predecessor. Since, successor or predecessor will always be on
the leaf node hence, the process will be similar as the node is being deleted from the
leaf node.
Example 1
Delete the node 53 from the B Tree of order 5 shown in the following figure.
53 is present in the right child of element 49. Delete it.
Now, 57 is the only element which is left in the node, the minimum number of
elements that must be present in a B tree of order 5, is 2. it is less than that, the elements
in its left and right sub-tree are also not sufficient therefore, merge it with the left sibling
and intervening element of parent i.e. 49.
Application of 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.
Searching an un-indexed and unsorted database containing n key values needs O(n)
running time in worst case. However, if we use B Tree to index this database, it will be
searched in O(log n) time in worst case.
B+ Tree
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.
Advantages of B+ Tree
5. Faster search queries as the data is stored only on the leaf nodes.
B Tree VS B+ Tree
SN B Tree B+ Tree
Insertion in B+ Tree
Step 2: If the leaf doesn't have required space, split the node and copy the
middle node to the next index node.
Step 3: If the index node doesn't have required space, split the node and
copy the middle element to the next index page.
Example :
Insert the value 195 into the B+ tree of order 5 shown in the following
figure.
195 will be inserted in the right sub-tree of 120 after 190. Insert it at the
desired position.
The node contains greater than the maximum number of elements i.e. 4,
therefore split it and place the median node up to the parent.
Now, the index node contains 6 children and 5 keys which violates the B+
tree properties, therefore we need to split it, shown as follows.
Deletion in B+ Tree
Step 2: if the leaf node contains less than minimum number of elements,
merge down the node with its sibling and delete the key in between them.
Step 3: if the index node contains less than minimum number of elements,
merge the node with the sibling and move down the key in between them.
Example
Delete the key 200 from the B+ Tree shown in the following figure.
200 is present in the right sub-tree of 190, after 195. delete it.
Merge the two nodes by using 195, 190, 154 and 129.
Now, element 120 is the single element present in the node which is
violating the B+ Tree properties. Therefore, we need to merge it by using
60, 78, 108 and 120.
In the linked representation of binary trees, more than one half of the
link fields contain NULL values which results in wastage of storage
space. If a binary tree consists of n nodes then n+1 link fields
contain NULL values. So in order to effectively manage the space,
a method was devised by Perlis and Thornton in which the NULL
links are replaced with special links known as threads. Such binary
trees with threads are known as threaded binary trees. Each node
in a threaded binary tree either contains a link to its child node or
thread to other nodes in the tree.
The above figure shows the inorder traversal of this binary tree yields D,
B, E, G, A, C, F. If we consider the two-way threaded Binary tree,
the node E whose left field contains NULL is replaced by a thread
pointing to its inorder predecessor i.e. node B. Similarly, for node
G whose right and left linked fields contain NULL values are
replaced by threads such that right link field points to its inorder
successor and left link field points to its inorder predecessor. In
the same way, other nodes containing NULL values in their link
fields are filled with threads.
In the above figure of two-way threaded Binary tree, we noticed that no
left thread is possible for the first node and no right thread is
possible for the last node. This is because they don't have any
inorder predecessor and successor respectively. This is indicated
by threads pointing nowhere. So in order to maintain the
uniformity of threads, we maintain a special node called
the header node. The header node does not contain any data part
and its left link field points to the root node and its right link field
points to itself. If this header node is included in the two-way
threaded Binary tree then this node becomes the inorder
predecessor of the first node and inorder successor of the last
node. Now threads of left link fields of the first node and right link
fields of the last node will point to the header node.
Advantages of Threaded Binary Tree:
o Insertion into and deletion from a threaded binary tree are more
time consuming since both threads and ordinary links need to be
maintained.
B* Tree
4. Data Node Splitting : When a node is full and needs to be split, a B*-
tree tries to share entries with neighboring nodes rather than splitting
immediately, reducing the overall number of splits and maintaining
denser nodes.
Structure of a B* -tree
Leaf Nodes : These contain the actual data or pointers to the data
stored in the structure.
Each node (except the root) must be at least two-thirds full to maintain
dense data storage.
Let's walk through an example where we insert values into a B*-tree of order
5 (each node can have up to 5 children and store up to 4 keys).
Initial State
Let’s start with an empty B*-tree of order 5 and insert the following
sequence of values: 10, 20, 5, 15, 25, 30, 35 .
5. Insert 25 : Since the root node has reached its capacity of 4 keys, the
node attempts to split. In a B*-tree, instead of splitting immediately, it
tries to share keys with neighboring nodes. If neighboring nodes are
also full, then it performs a split, creating a new node and promoting a
key to the parent.
After several insertions and rearrangements, the B*-tree structure may look
like this:
[15]
/ \
[5, 10] [20, 25, 30, 35]
Advantages of B* -trees
Faster Disk Access : With fewer levels in the tree, disk I/O operations
are minimized, which is beneficial in databases where data is often
stored on disk.
Use Cases
File Systems: Many file systems (like HFS+ in macOS) use B*-trees to
organize file data and metadata.
Memory Management : In-memory databases or caches can use B*-
trees for efficient memory allocation and reclamation.
Summary