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

Trees

The document provides an overview of tree data structures, highlighting their hierarchical nature and key properties such as nodes, root, children, and types of trees including binary trees and their variations. It discusses the implementation of trees, their applications in organizing data, and the importance of choosing appropriate data structures based on factors like data type and operation costs. Additionally, it covers various types of binary trees, including full, complete, perfect, degenerate, and balanced binary trees, along with their properties.

Uploaded by

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

Trees

The document provides an overview of tree data structures, highlighting their hierarchical nature and key properties such as nodes, root, children, and types of trees including binary trees and their variations. It discusses the implementation of trees, their applications in organizing data, and the importance of choosing appropriate data structures based on factors like data type and operation costs. Additionally, it covers various types of binary trees, including full, complete, perfect, degenerate, and balanced binary trees, along with their properties.

Uploaded by

RIGENIX FF
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 78

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.

Some factors are considered for choosing the data structure:

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.

Let's understand some key points of the Tree data structure.


o A tree data structure is defined as a collection of objects or entities known as
nodes that are linked together to represent or simulate hierarchy.

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.

Some basic terms used in Tree data structure.

Let's consider the tree structure, which is shown below:


In the above structure, each node is labeled with some number. Each arrow shown in
the above figure is known as a link between the two nodes.

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 Ancestor node: - An ancestor of a node is any predecessor node on a path from


the root to that node. The root node doesn't have any ancestors. In the tree
shown in the above image, nodes 1, 2, and 5 are the ancestors of node 10.

o Descendant: The immediate successor of the given node is known as a


descendant of a node. In the above figure, 10 is the descendant of node 5.

Properties of Tree data structure

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.

In programming, the structure of a node can be defined as:

1. struct node

2. {

3. int data;

4. struct node *left;

5. struct node *right;

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

The following are the 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 Heap: It is also a tree data structure implemented using arrays. It is used to


implement priority queues.

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.

Types of Tree data structure

The following are the types of a tree data structure:

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;

4. struct node *left;

5. struct node *right;

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.

Let's understand the binary tree through an example.


The above tree is a binary tree because each node contains the utmost two children.
The logical representation of the above tree is given below:
In the above tree, node 1 contains two pointers, i.e., left and a right pointer pointing to
the left and right node respectively. The node 2 contains both the nodes (left and right
node); therefore, it has two pointers (left and right). The nodes 3, 5 and 6 are the leaf
nodes, so all these nodes contain NULL pointer on both left and right parts.

Properties of Binary Tree

o At each level of i, the maximum number of nodes is 2i.

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 The minimum number of nodes possible at height h is equal to h+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.

If there are 'n' number of nodes in the binary tree.

The minimum height can be computed as:

As we know that,

n = 2h+1 -1

n+1 = 2h+1

Taking log on both the sides,

log2(n+1) = log2(2h+1)

log2(n+1) = h+1
h = log 2(n+1) - 1

The maximum height can be computed as:

As we know that,

n = h+1

h= n-1

Advertisement

Types of Binary Tree

There are four types of Binary tree:

o Full/ proper/ strict Binary tree

o Complete Binary tree

o Perfect Binary tree

o Degenerate Binary tree

o Balanced Binary tree

1. Full/ proper/ strict Binary tree

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.

Let's look at the simple example of the Full Binary tree.


In the above tree, we can observe that each node is either containing zero or two
children; therefore, it is a Full Binary tree.

Properties of Full Binary Tree

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 minimum number of nodes in the full binary tree is 2*h-1.

o The minimum height of the full binary tree is log 2(n+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.

Let's create a complete binary tree.

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.

Properties of Complete Binary Tree

o The maximum number of nodes in complete binary tree is 2h+1 - 1.

o The minimum number of nodes in complete binary tree is 2 h.

o The minimum height of a complete binary tree is log 2(n+1) - 1.

o The maximum height of a complete binary tree is


Perfect Binary Tree

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.

Let's look at a simple example of a perfect binary tree.

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.

Degenerate Binary Tree

The degenerate binary tree is a tree in which all the internal nodes have only one
children.

Let's understand the Degenerate binary tree through examples.

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.

Balanced Binary Tree

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.

Let's understand the balanced binary tree through examples.


The above tree is a balanced binary tree because the difference between the left subtree
and right subtree is zero.

The above tree is not a balanced binary tree because the difference between the left
subtree and the right subtree is greater than 1.

Binary Tree Implementation

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

Algorithm for Inorder Traversal:

Inorder(tree)

 Traverse the left subtree, i.e., call Inorder(left->subtree)

 Visit the root.

 Traverse the right subtree, i.e., call Inorder(right->subtree)

Uses of Inorder Traversal:

 In the case of binary search trees (BST), Inorder traversal gives nodes in non-
decreasing order.

 To get nodes of BST in non-increasing order, a variation of Inorder traversal


where Inorder traversal is reversed can be used.

 Inorder traversal can be used to evaluate arithmetic expressions stored in


expression trees.
Preorder traversal visits the node in the order: Root -> Left -> Right

Algorithm for Preorder Traversal:

Preorder(tree)

 Visit the root.

 Traverse the left subtree, i.e., call Preorder(left->subtree)

 Traverse the right subtree, i.e., call Preorder(right->subtree)

Uses of Preorder Traversal:

 Preorder traversal is used to create a copy of the tree.

 Preorder traversal is also used to get prefix expressions on an expression tree.


Postorder traversal visits the node in the order: Left -> Right -> Root

Algorithm for Postorder Traversal:

Algorithm Postorder(tree)

 Traverse the left subtree, i.e., call Postorder(left->subtree)

 Traverse the right subtree, i.e., call Postorder(right->subtree)

 Visit the root

Uses of Postorder Traversal:

 Postorder traversal is used to delete the tree.

 Postorder traversal is also useful to get the postfix expression of an expression


tree.

 Postorder traversal can help in garbage collection algorithms, particularly in


systems where manual memory management is used.
Binary Search tree
A binary search tree follows some order to arrange the elements. In a Binary search
tree, the value of left node must be smaller than the parent node, and the value of right
node must be greater than the parent node. This rule is applied recursively to the left
and right subtrees of the root.

Let's understand the concept of Binary search tree with an example.

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.

Advantages of 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.

Example of creating a binary search tree

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 -

Step 1 - Insert 45.

Step 2 - Insert 15.

As 15 is smaller than 45, so insert it as the root node of the left subtree.

Step 3 - Insert 79.

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.

Step 5 - Insert 10.

10 is smaller than 45 and 15, so it will be inserted as a left subtree of 15.


Step 6 - Insert 55.

55 is larger than 45 and smaller than 79, so it will be inserted as the left subtree of 79.

Step 7 - Insert 12.

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.

Step 9 - Insert 50.


50 is greater than 45 but smaller than 79 and 55. So, it will be inserted as a left subtree of
55.

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.

Let's understand how a search is performed on a binary search tree.

Searching in Binary search tree

Searching means to find or locate a specific element or node in a data structure. In


Binary search tree, searching a node is easy because elements in BST are stored in a
specific order. The steps of searching a node in Binary Search tree are listed as follows -

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.

5. Repeat the above procedure recursively until the match is found.

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.

Algorithm to search an element in Binary search tree

1. Search (root, item)

2. Step 1 - if (item = root → data) or (root = NULL)

3. return root

4. else if (item < root → data)

5. return Search(root → left, item)


6. else

7. return Search(root → right, item)

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.

Deletion in Binary Search 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 -

o The node to be deleted is the leaf node, or,

o The node to be deleted has only one child, and,

o The node to be deleted has two children

We will understand the situations listed above in detail.

When the node to be deleted is the leaf node

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.

When the node to be deleted has two children

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.

Now let's understand how insertion is performed on a binary search tree.

Insertion in Binary Search tree

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.

The complexity of the Binary Search tree

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

Insertion O(log n) O(log n) O(n)

Deletion O(log n) O(log n) O(n)

Search O(log n) O(log n) O(n)

Where 'n' is the number of nodes in the given tree.

2. Space Complexity

Operations Space complexity

Insertion O(n)

Deletion O(n)

Search O(n)

o The space complexity of all operations of Binary search tree is O(n).


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.

PauseNext

Mute

Current Time 0:57

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

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)

Operations on AVL tree

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

Insertion in AVL tree is performed in the same way as it is


performed in a binary search tree. However, it may lead to
1 Insertion violation in the AVL tree property and therefore the tree
may need balancing. The tree can be balanced by applying
rotations.
Deletion can also be performed in the same way as it is
performed in a binary search tree. Deletion may also disturb
2 Deletion
the balance of the tree therefore, various types of rotations
are used to rebalance the tree.

Why AVL Tree?

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:

1. L L rotation: Inserted node is in the left subtree of left subtree of A

2. R R rotation : Inserted node is in the right subtree of right subtree of A

3. L R rotation : Inserted node is in the right subtree of left subtree of A

4. R L rotation : Inserted node is in the left subtree of right subtree of A

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

A node B has been inserted into the right


subtree of A the left subtree of C, because of
which C has become an unbalanced node
having balance factor 2. This case is L R
rotation where: Inserted node is in the right
subtree of left subtree of C

As LR rotation = RR + LL rotation, hence


RR (anticlockwise) on subtree rooted at A is
performed first. By doing RR rotation,
node A, has become the left subtree of B.

After performing RR rotation, node C is still


unbalanced, i.e., having balance factor 2, as
inserted node A is in the left of left of C
Now we perform LL clockwise rotation on
full tree, i.e. on node C. node C has now
become the right subtree of node B, A is left
subtree of B

Balance factor of each node is now either -1,


0, or 1, i.e. BST is balanced now.

Let us understand each and every step very clearly:


State Action

A node B has been inserted into the left subtree


of C the right subtree of A, because of which A
has become an unbalanced node having
balance factor - 2. This case is RL rotation
where: Inserted node is in the left subtree of
right subtree of A

As RL rotation = LL rotation + RR rotation,


hence, LL (clockwise) on subtree rooted at C is
performed first. By doing RR rotation,
node C has become the right subtree of B.

After performing LL rotation, node A is still


unbalanced, i.e. having balance factor -2,
which is because of the right-subtree of the
right-subtree node A.

Now we perform RR rotation (anticlockwise


rotation) on full tree, i.e. on node A.
node C has now become the right subtree of
node B, and node A has become the left
subtree of B.
Balance factor of each node is now either -1, 0,
or 1, i.e., BST is balanced now.

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.

Q: Construct an AVL tree having the following elements

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.

The resultant balance tree is:

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.

The resultant balance tree is:

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

3 a) We first perform RR rotation on node B

The resultant tree after RR rotation is:


3b) We first perform LL rotation on the node I

The resultant balanced tree after LL rotation is:


4. Insert C, F, D

On inserting C, F, D, BST becomes unbalanced as the Balance Factor of B and H is -2,


since if we travel from D to B we find that it is inserted in the right subtree of left
subtree of B, we will perform RL Rotation on node I. RL = LL + RR rotation.

4a) We first perform LL rotation on node E

The resultant tree after LL rotation is:


4b) We then perform RR rotation on node B

The resultant balanced tree after RR rotation is:

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.

5 a) We first perform RR rotation on node C

The resultant tree after RR rotation is:


5 b) We then perform LL rotation on node H

The resultant balanced tree after LL rotation is:

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.

The resultant balanced tree after RR rotation is:

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.

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

2. Every node in a B-Tree except the root node and the leaf node contain at least
m/2 children.

3. The root nodes must have at least 2 nodes.

4. 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.

Backward Skip 10sPlay VideoForward Skip 10s

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.

2. Since, 40<49<56, traverse right sub-tree of 40.

3. 49>45, move to right. Compare 49.

4. match found, return.


Searching in a B tree depends upon the height of the tree. The search algorithm takes
O(log n) time to search any element in a B 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 Insert the new element in the increasing order of elements.

o Split the node into the two nodes at the median.

o Push the median element upto its parent node.

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.

8 will be inserted to the right of 5, therefore insert 8.

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.

1. Locate the leaf node.


2. If there are more than m/2 keys in the leaf node then delete the desired key from
the node.

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.

The final B tree is shown as follows.

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

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

2. Height of the tree remains balanced and less as compare to B tree.

3. We can access the data stored in a B+ tree sequentially as well as


directly.

4. Keys are used for indexing.

5. Faster search queries as the data is stored only on the leaf nodes.
B Tree VS B+ Tree

SN B Tree B+ Tree

Search keys can not be Redundant search keys can be


1
repeatedly stored. present.

Data can be stored in leaf


Data can only be stored on the leaf
2 nodes as well as internal
nodes.
nodes
Searching for some data is a
slower process since data Searching is comparatively faster as
3 can be found on internal data can only be found on the leaf
nodes as well as on the leaf nodes.
nodes.

Deletion of internal nodes Deletion will never be a complexed


4 are so complicated and process since element will always
time consuming. be deleted from the leaf nodes.

Leaf nodes are linked together to


Leaf nodes can not be
5 make the search operations more
linked together.
efficient.

Insertion in B+ Tree

Step 1: Insert the new node as a leaf node

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 1: Delete the key and data from the leaves.

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.

Now, the height of B+ tree will be decreased by 1.

Threaded Binary Tree


Threaded Binary Tree

In this article, we will understand about the threaded binary tree in


detail.

What do you mean by Threaded Binary Tree?

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.

Types of Threaded Binary Tree

There are two types of threaded Binary Tree:

o One-way threaded Binary Tree

o Two-way threaded Binary Tree

One-way threaded Binary trees:


In one-way threaded binary trees, a thread will appear either in the right
or left link field of a node. If it appears in the right link field of a
node then it will point to the next node that will appear on
performing in order traversal. Such trees are called Right threaded
binary trees. If thread appears in the left field of a node then it will
point to the nodes inorder predecessor. Such trees are called Left
threaded binary trees. Left threaded binary trees are used less often
as they don't yield the last advantages of right threaded binary
trees. In one-way threaded binary trees, the right link field of last
node and left link field of first node contains a NULL. In order to
distinguish threads from normal links they are represented by
dotted lines.
The above figure shows the inorder traversal of this binary tree yields D,
B, E, A, C, F. When this tree is represented as a right threaded
binary tree, the right link field of leaf node D which contains a
NULL value is replaced with a thread that points to node B which
is the inorder successor of a node D. In the same way other nodes
containing values in the right link field will contain NULL value.

Two-way threaded Binary Trees:


In two-way threaded Binary trees, the right link field of a node
containing NULL values is replaced by a thread that points to
nodes inorder successor and left field of a node containing NULL
values is replaced by a thread that points to nodes inorder
predecessor.

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 In threaded binary tree, linear and fast traversal of nodes in the


tree so there is no requirement of stack. If the stack is used then it
consumes a lot of memory and time.

o It is more general as one can efficiently determine the successor


and predecessor of any node by simply following the thread and
links. It almost behaves like a circular linked list.

Disadvantages of Threaded Binary Tree:

o When implemented, the threaded binary tree needs to maintain


the extra information for each node to indicate whether the link
field of each node points to an ordinary node or the node's
successor and predecessor.

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

A B*-tree is a type of self-balancing tree data structure commonly used in


databases and file systems to manage large blocks of sorted data. It’s
an optimized version of the B-tree, which is a generalization of binary
search trees that allows nodes to have more than two children. Like a
B-tree, a B*-tree maintains sorted data and allows for efficient
insertion, deletion, and search operations, but it does so with
improved space utilization and fewer splits.

Key Features of a B* -tree

1. Self-Balancing : Like B-trees, B*-trees remain balanced, ensuring


efficient operations by keeping all leaves at the same depth.

2. High Fanout : Each node has a large number of children (or


pointers), which allows for fewer levels in the tree, making traversals
faster.

3. Space Efficiency : B*-trees typically require nodes to be filled more


densely (often at least two-thirds full) before splitting. This minimizes
the number of splits and enhances space efficiency.

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.

5. Efficient Disk Access : Since B*-trees minimize the number of node


splits and maximize data per node, they reduce disk I/O operations,
which is crucial for databases and file systems that store data on disk.

Structure of a B* -tree

A B*-tree is structured with internal nodes and leaf nodes:


 Internal Nodes : These nodes act as decision points, storing keys that
help navigate through the tree and having pointers to their children.

 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.

Example of B* -tree Operations

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 .

1. Insert 10 : 10 becomes the root node.

2. Insert 20 : 10 and 20 are stored in the root node.

3. Insert 5 : The root node now contains 5, 10, 20.

4. Insert 15 : The root node now contains 5, 10, 15, 20 .

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

 Improved Space Utilization : B*-trees keep nodes densely packed,


which means they use storage space more efficiently than regular B-
trees.

 Reduced Splits : By sharing keys with neighboring nodes, B*-trees


require fewer splits, which can reduce the height of the tree and
improve search times.

 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

B*-trees are commonly used in:

 Databases : To index large datasets, as B*-trees help to reduce the


number of I/O operations needed to retrieve data.

 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

A B*-tree is a variation of the B-tree that is highly efficient for database


indexing and file systems, offering better space utilization and
performance by requiring nodes to be more densely filled and
minimizing the number of splits. This leads to faster data retrieval,
reduced memory overhead, and fewer disk I/O operations.

You might also like