DS-3rd Module
DS-3rd Module
Data Structure
A data structure is a special way of organizing the data elements into a particular form.
The arrangement of data in a particular order is very important to access the particular
data element in less time
For example, in our daily life, when we used to put our clothes in a particular drawer
properly, especially in a sequence so that whenever we want to wear a particular dress,
we may not require to suffer in finding it out, and save our time from wasting.
○ The linear data structure is nothing but arranging the data elements linearly one
after the other. Here, we cannot arrange the data elements randomly as in the
hierarchical order.
○ This linear data structure will follow the sequential order of inserting the various
data elements. Similarly, in this way, we perform the deletion operation onto the
elements. Linear data structures are easy to implement because computer
memory is arranged linearly. Its examples are array, stack, queue, linked list, etc.
○ A non-linear data structure is another important type in which data elements are
not arranged sequentially; mainly, data elements are arranged in random order
without forming a linear structure.
○ In trees, the data elements are arranged in the hierarchical form, whereas in
graphs, the data elements are arranged in random order, using the edges and
vertex.
○ Multiple runs are required to traverse through all the elements completely.
Traversing in a single run is impossible to traverse the whole data structure.
○ The data structure where data items are not organized sequentially is called a
non-linear data structure. In other words, data elements of the non-linear data
structure could be connected to more than one element to reflect a special
relationship among them.
Tree:
○ The tree is a non-linear data structure that is comprised of various nodes. The
nodes in the tree data structure are arranged in hierarchical order.
○ It consists of a root node corresponding to its various child nodes, present at the
next level. The tree grows on a level basis, and root nodes have limited child
nodes depending on the order of the tree.
○ For example, in the binary tree, the order of the root node is 2, which means it can
have at most 2 children per node, not more than it.
○ The tree itself is a very broad data structure and is divided into various categories
like Binary tree, Binary search tree, AVL trees, Heap, max Heap, min-heap, etc.
○ All the types of trees mentioned above differ based on their properties.
○ Unlike in an array, we have to define the size of the array, and subsequent
memory space is allocated to that array; if we don't want to store the elements till
the range of the array, then the remaining memory gets wasted.
○ So to overcome this factor, we will use the non-linear data structure and have
multiple options to traverse from one node to another.
○ Each node contains some data and the link or reference of other nodes that can
be called children.
Root
● In a tree data structure, the root is the first node of the tree. The root node is
the initial node of the tree in data structures.
● In the tree data structure, there must be only one root node.
Edge
● In a tree in data structures, the connecting link of any two nodes is called the
edge of the tree data structure.
● In the tree data structure, N number of nodes connecting with N -1 number of
edges.
Parent
In the tree in data structures, the node that is the predecessor of any node is known as a
parent node, or a node with a branch from itself to any other successive node is called
the parent node.
Child
● The node, a descendant of any node, is known as child nodes in data
structures.
● In a tree, any number of parent nodes can have any number of child nodes.
● In a tree, every node except the root node is a child node.
Siblings
In trees in the data structure, nodes that belong to the same parent are called siblings.
Leaf
● Trees in the data structure, the node with no child, is known as a leaf node.
● In trees, leaf nodes are also called external nodes or terminal nodes.
Internal nodes
● Trees in the data structure have at least one child node known as internal
nodes.
● In trees, nodes other than leaf nodes are internal nodes.
● Sometimes root nodes are also called internal nodes if the tree has more than
one node.
Degree
● In the tree data structure, the total number of children of a node is called the
degree of the node.
● The highest degree of the node among all the nodes in a tree is called the
Degree of Tree.
Level
In tree data structures, the root node is said to be at level 0, and the root node's children
are at level 1, and the children of that node at level 1 will be level 2, and so on.
Height
● In a tree data structure, the number of edges from the leaf node to the
particular node in the longest path is known as the height of that node.
● In the tree, the height of the root node is called "Height of Tree".
● The tree height of all leaf nodes is 0.
Depth
● In a tree, many edges from the root node to the particular node are called the
depth of the tree.
● In the tree, the total number of edges from the root node to the leaf node in the
longest path is known as "Depth of Tree".
● In the tree data structures, the depth of the root node is 0.
Path
● In the tree in data structures, the sequence of nodes and edges from one node
to another node is called the path between those two nodes.
● The length of a path is the total number of nodes in a path.zx
Subtree
In the tree in data structures, each child from a node shapes a sub-tree recursively and
every child in the tree will form a sub-tree on its parent node.
Ancestor:An ancestor of a node is any other node on the path from the node to the
root.
. Path In a tree data structure, the sequence of Nodes and Edges from one node to
another node is called as PATH between that two Nodes. Length of a Path is total
number of nodes in that path. In below example the path A - B - E - J has length 4.
Now you will look into the types of trees in data structures.
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.
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.
○ 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.
○ 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.
As we know that,
n = 2h+1 -1
n+1 = 2h+1
log2(n+1) = log2(2h+1)
log2(n+1) = h+1
h = log2(n+1) - 1
As we know that,
n = h+1
h= n-1
A perfect binary tree is a type of binary tree in which every internal node
has exactly two child nodes and all the leaf nodes are at the same level.
A complete binary tree is just like a full binary tree, but with two major
differences
A tree is a perfect binary tree if all the internal nodes have 2 children, and all the leaf
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.
follows...
1. Array Representation
2. Linked List Representation
To represent a binary tree of depth 'n' using array representation, we need one dimensional
consists of three fields. First field for storing left child address, second for storing actual data
and third for storing right child address.
The above example of the binary tree represented using Linked list representation is shown
as follows...
Binary Search Tree(BST)
Binary search tree is a data structure that quickly allows us to maintain a
sorted list of numbers.
● It is called a binary tree because each tree node has a maximum of two
children.
● It is called a search tree because it can be used to search for the
presence of a number in O(log(n)) time.
The properties that separate a binary search tree from a regular binary tree is
1. All nodes of left subtree are less than the root node
2. All nodes of right subtree are more than the root node
3. Both subtrees of each node are also BSTs i.e. they have the above two
properties
The binary tree on the right isn't a binary search tree because the right
subtree of the node "3" contains a value smaller than it.
○ Searching an element in the Binary search tree is easy as we always have a hint
that which subtree has the desired element.
○ 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
○ First, we have to insert 45 into the tree as the root of the tree.
○ 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.
○ 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
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.
90 is greater than 45 and 79, so it will be inserted as the right subtree of 79.
Step 5 - Insert 10.
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.
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.
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.
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.
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 -
○ The node to be deleted is the leaf node, or,
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.
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 -
○ After that, replace that node with the inorder successor until the target node is
placed at the leaf of tree.
○ 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.
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.
Inorder traversal
This technique follows the 'left root right' policy. It means that first left subtree is visited
after that root node is traversed, and finally, the right subtree is traversed. As the root
node is traversed between the left and right subtree, it is named inorder traversal.
So, in the inorder traversal, each node is visited in between of its subtrees.
Algorithm
Example
So, for left subtree B, first, its left subtree D is traversed. Since node D does not have any
children, so after traversing it, node B will be traversed, and at last, right subtree of node
B, that is E, is traversed. Node E also does not have any children; therefore, the traversal
of the left subtree of root node A is completed.
At last, move towards the right subtree of root node A that is C. So, for right subtree C;
first, its left subtree F is traversed. Since node F does not have any children, node C will
be traversed, and at last, a right subtree of node C, that is, G, is traversed. Node G also
does not have any children; therefore, the traversal of the right subtree of root node A is
completed.
As all the nodes of the tree are traversed, the inorder traversal of the given tree is
completed. The output of the inorder traversal of the above tree is -
D→B→E→A→F→C→G
To know more about the inorder traversal in data structure, you can follow the link
Inorder Traversal.
Now, start applying the postorder traversal on the above tree. First, we traverse the left
subtree B that will be traversed in postorder. After that, we will traverse the right subtree
C in postorder. And finally, the root node of the above tree, i.e., A, is traversed.
So, for left subtree B, first, its left subtree D is traversed. Since node D does not have any
children, traverse the right subtree E. As node E also does not have any children, move
to the root node B. After traversing node B, the traversal of the left subtree of root node
A is completed.
Preorder traversal
This technique follows the 'root left right' policy. It means that, first root node is visited
after that the left subtree is traversed recursively, and finally, right subtree is recursively
traversed. As the root node is traversed before (or pre) the left and right subtree, it is
called preorder traversal.
So, in a preorder traversal, each node is visited before both of its subtrees.
Algorithm
Example
So, for left subtree B, first, the root node B is traversed itself; after that, its left subtree D
is traversed. Since node D does not have any children, move to right subtree E. As node
E also does not have any children, the traversal of the left subtree of root node A is
completed.
Now, move towards the right subtree of root node A that is C. So, for right subtree C, first
the root node C has traversed itself; after that, its left subtree F is traversed. Since node
F does not have any children, move to the right subtree G. As node G also does not have
any children, traversal of the right subtree of root node A is completed.
Therefore, all the nodes of the tree are traversed. So, the output of the preorder traversal
of the above tree is -
A→B→D→E→C→F→G
To know more about the preorder traversal in the data structure, you can follow the link
Preorder traversal.
Postorder traversal
This technique follows the 'left-right root' policy. It means that the first left subtree of the
root node is traversed, after that recursively traverses the right subtree, and finally, the
root node is traversed. As the root node is traversed after (or post) the left and right
subtree, it is called postorder traversal.
So, in a postorder traversal, each node is visited after both of its subtrees.
Algorithm
Example
Now, move towards the right subtree of root node A that is C. So, for right subtree C, first
its left subtree F is traversed. Since node F does not have any children, traverse the right
subtree G. As node G also does not have any children, therefore, finally, the root node of
the right subtree, i.e., C, is traversed. The traversal of the right subtree of root node A is
completed.
At last, traverse the root node of a given tree, i.e., A. After traversing the root node, the
postorder traversal of the given tree is completed.
Therefore, all the nodes of the tree are traversed. So, the output of the postorder
traversal of the above tree is -
D→E→B→F→G→C→A
To know more about the postorder traversal in the data structure, you can follow the link
Postorder traversal.
Tree sort is a sorting algorithm that is based on Binary Search Tree data
structure. It first creates a binary search tree from the elements of the input list
or array and then performs an in-order traversal on the created binary search
tree to get the elements in sorted order.
Algorithm:
● Step 2: Create a Binary search tree by inserting data items from the
sorted order.
CountNodes(node x)
return 0
[End If]
If(x->left!=NULL)
n=n+1
CountNode(x->left)
[End If]
If(x->right!=NULL)
n=n+1
CountNode(x->right)
[End If]
Return n
if (node == NULL)
return 0;
else {
else
○ The operator present in the depth of the tree is always at the highest priority.
○ The operator, which is not much at the depth in the tree, is always at the lowest
priority compared to the operators lying at the depth.
○ The operand will always present at a depth of the tree; hence it is considered the
highest priority among all the operators.
The expression tree is a binary tree in which each external or leaf node corresponds to the
operand and each internal or parent node corresponds to the operators so for example
expression tree for 7 + ((1+8)*3) would be:
If S is not null, then
Return S.value
x = solve(S.left)
y = solve(S.right)
1. The main objective of using the expression trees is to make complex expressions
and can be easily be evaluated using these expression trees.
2. It is also used to find out the associativity of each operator in the expression.
3. It is also used to solve the postfix, prefix, and infix expression evaluation.
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.
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.
n 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.