8. Trees
8. Trees
8. Trees
Suppose we want to
show the employees and their positions in the hierarchical form then it can be represented as
shown below:
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.
• 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.
• 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.
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.
• Child node: If the node is a descendant of any node, then the node is
known as a child node.
• Parent: If the node contains any sub-node, then that node is said to be the
parent of that sub-node.
• Sibling: The nodes that have the same parent are known as siblings.
• 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.
• Internal nodes: A node has atleast one child node known as an internal
• 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. Recursion means reducing something in a self-similar manner. So, this recursive
property of the tree data structure is implemented in various applications.
• 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.
• 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.
• Height of node x: The height of node x can be defined as the longest path from the node x to
the leaf node.
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:
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:
struct node
{
int data;
struct node *left;
struct node *right;
}
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.
0 1 2 3 4 5 6 7
7 A B C D /0 E F
Location number zero of the array can be used to store the size of the tree of the total number
of nodes ( existing or not existing).
All non- existing nodes are represented by /0 in array.
Index of the left child of a node i=2*I 2n+1
Index of the right child of a node i=2*i+1 2n+2
Index of the parent node i=i/2 floor(n-1)/2
Sibling of a node I will be found at the location i + 1 , if I is a left child of its parents.
Similarly, if i is a right child of its parents then its sibling will be found at i - 1.
Types of Binary Tree
There are four types of Binary tree:
• Full/ proper/ strict Binary tree
• Complete Binary tree
• Perfect Binary tree
• Degenerate Binary tree
• Balanced Binary tree
Full/ proper/ strict Binary
The full binary tree istree
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.
struct node {
int element;
struct node* left;
struct node* right;
};
return (Node);
}
return 0;
}
2022-23
2019-20
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.
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.
Huffman Coding Tree
Huffman coding is a technique of compressing data to reduce its size without losing any of the details. It was first
developed by David Huffman.
Huffman coding generally useful to compress the data in which there are frequently occurring character.
6 9
5
4
A
1 3
B D
AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962. The tree is named AVL in
honour of its inventors.
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.
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.
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:
A B tree of order m contains all the properties of an M way tree. In addition, it contains the
following properties.
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.
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.
• Insert the new element in the increasing order of elements.
• Split the node into the two nodes at the median.
• Push the median element upto its parent node.
• If the parent node also contain m-1 number of keys, then split it too by following the same
steps.
Example:- Create a B Tree for the Fallowing Sequence F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B Having a order - 5
Example:- Create a B Tree for the Fallowing Sequence F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B Having a order - 5
Example:- Create a B Tree for the Fallowing Sequence F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B Having a order - 5
Example:- Create a B Tree for the Fallowing Sequence F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B Having a order - 5
Example:- Create a B Tree for the Fallowing Sequence F, S, Q, K, C, L, H, T, V, W, M, R, N, P, A, B Having a order - 5
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.
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.
50 80
10 20 60 70 75 90 95
81 82 89 92 93 10 11 11
0 0 1
4 5 6 14 15 16 23 27 51 52 64 65 68 72 73 77 78 79
50 80
10 20 60 70 75 90 95
77 78 79 72 73 77 78 79
4 5 6 14 15 16 23 27 51 52 65 68 72 73 77 78 79
50 80
10 20 60 70 75 90 95
77 78 79 72 73 77 78 79
4 5 6 14 15 16 23 27 51 52 65 68 72 73 77 78 79
50 80
10 16 60 70 75 90 95
77 78 79 72 73 77 78 79
4 5 6 14 15 20 27 51 52 65 68 72 73 77 78 79
50 80
10 16 60 70 77 90 95
77 78 79 72 73 77 78 79
4 5 6 14 15 20 27 51 52 65 68 73 75 78 79
50 80
10 16 70 77 90 95
77 78 79 72 73 77 78 79
4 5 6 14 15 20 27 51 52 60 68 73 75 78 79
Heap Data Structure
What is Heap?
A heap is a complete binary tree, and the binary tree is a tree in which the node can have utmost
two children. Before knowing more about the heap data structure, we should know about the
complete binary tree.
insertHeap(A, n, value)
{
n=n+1; // n is incremented to insert the new element
A[n]=value; // assign new value at the nth position
i = n; // assign the value of n to i
// loop will be executed until i becomes 1.
while(i>1)
{
parent= floor value of i/2; // Calculating the floor value of i/2
// Condition to check whether the value of parent is less than the given node or not
if(A[parent]<A[i])
{
swap(A[parent], A[i]);
i = parent;
}
else
{
return;
}
}
}