Module-4 Trees
Module-4 Trees
MODULE NO. 4
TREE
TOPIC
11/14/2021 2
11/14/2021 3
11/14/2021 4
11/14/2021 5
Representation of Trees
List Representation
– ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
– The root comes first, followed by a list of sub-trees
11/14/2021 8
1. Root-
The first node from where the tree originates is called as a root node.
In any tree, there must be only one root node.
We can never have multiple root nodes in a tree data structure.
11/14/2021 9
2. Edge-
11/14/2021 10
3. Parent-
•The node which has a branch from it to any other node is called as a parent
node.
•In other words, the node which has one or more children is called as a
parent node.
•In a tree, a parent node can have any number of child nodes.
11/14/2021 12
5. Siblings
11/14/2021 13
6. Leaf
• In a tree data structure, the node which does not have
a child is called as LEAF Node. In simple words, a
leaf is a node with no child.
11/14/2021 14
7. Internal Nodes
• In a tree data structure, the node which has atleast one
child is called as INTERNAL Node. In simple words, an
internal node is a node with atleast one child.
In a tree data structure, nodes other than leaf nodes are
called as Internal Nodes. The root node is also said to
be Internal Node if the tree has more than one
node. Internal nodes are also called as 'Non-Terminal'
nodes.
11/14/2021 15
8. Degree
• In a tree data structure, the total number of children of a
node is called as DEGREE of that Node. In simple
words, the Degree of a node is total number of children it
has. The highest degree of a node among all the nodes in
a tree is called as 'Degree of Tree'
11/14/2021 16
9. Level
• In a tree data structure, the root node is said to be at
Level 0 and the children of root node are at Level 1 and
the children of the nodes which are at Level 1 will be at
Level 2 and so on... In simple words, in a tree each step
from top to bottom is called as a Level and the Level
count starts with '0' and incremented by one at each level
(Step).
11/14/2021 17
10. Height
• In a tree data structure, the total number of edges from
leaf node to a particular node in the longest path is called
as HEIGHT of that Node. In a tree, height of the root
node is said to be height of the tree. In a tree, height of
all leaf nodes is '0'.
•
11/14/2021 18
11. Depth
• In a tree data structure, the total number of egdes from
root node to a particular node is called as DEPTH of that
Node. In a tree, the total number of edges from root node
to a leaf node in the longest path is said to be Depth of
the tree. In simple words, the highest depth of any leaf
node in a tree is said to be depth of that tree. In a
tree, depth of the root node is '0'.
11/14/2021 19
12. 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.
•
11/14/2021 20
11/14/2021 21
11/14/2021 22
Depth of a Node
The depth of a node is the number of edges from
the root to the node.
Height of a Tree
The height of a Tree is the height of the root node
or the depth of the deepest node.
Forest
A collection of disjoint trees is called a forest.
11/14/2021 23
11/14/2021 24
11/14/2021 25
11/14/2021 26
Application of tree in Computer Science
11/14/2021 27
Binary Tree
• A binary tree is a tree data structure in which each
parent node can have at most two children. Each
node of a binary tree consists of three items:
• data item
• address of left child
• address of right child
11/14/2021 28
Binary Tree Representations
• A binary tree data structure is represented using two
methods. Those methods are as follows...
1. Array Representation
2. Linked List Representation
11/14/2021 29
Binary Tree Representation
struct node
{
int data;
struct node *left;
struct node *right;
};
11/14/2021 30
Types of Binary Tree
1. Full/Proper/Strictly Binary Tree
2. Complete Binary Tree
3. Perfect Binary Tree
4. Extended/Degenerate Binary Tree
– Left skewed BT
– Right skewed BT
11/14/2021 31
11/14/2021 32
11/14/2021 33
11/14/2021 34
11/14/2021 35
11/14/2021 36
11/14/2021 37
Samples of Trees
Complete Binary Tree
A A 1 A
B B 2 B C
4 H I
E 5
CHAPTER
38 5
11/14/2021 39
11/14/2021 40
• Types of traversal
• Breadth first search algorithm
• Depth first search
11/14/2021 41
11/14/2021 42
11/14/2021 43
11/14/2021 44
Que: Construct the binary tree given the
following traversals.
• Pre-order : A,B,D,H,E,C,F,G
• inorder:D,H,B,E,A,F,C,G
11/14/2021 45
Que 2:Construct the binary tree given the
following traversals
Postorder : H,D,I,E,B,J,F,K,L,G,C,A
Inorder:H,D,B,I,E,A,F,J,C,K,G,L
11/14/2021 46
11/14/2021 47
Q1.Construct the binary tree given the following
traversals.
Pre-order : A,B,D,G,E,H,C,F,I
Inorder:D,G,B,H,E,A,F,I,C
11/14/2021 48
Q.2 Construct the binary tree given the following
traversals.
Post-order :I,D,B,G,C,H,F,E,A
Inorder:B,I,D,A,C,G,E,H,F
11/14/2021 49
11/14/2021 50
Binary Search Tree(BST)
11/14/2021 51
11/14/2021 52
Conversion of general tree to binary tree
• A general tree is one where each node can have an
outgoing degree n, where n>=0
• Each node may have many application such as charts,
genesis &networks.
• Binary trees are the trees where the maximum degree of
any node is two.
• Steps
1. All nodes of a GT will be the node of BT
2. Root of GT ia the root of BT
3. Two relationship should be
• The first or the leftmost child-parent relationship
• Node –next right sibling relationship
11/14/2021 53
Convert the general tree into corresponding binary tree
11/14/2021 54
Construct the binary tree from given tree
11/14/2021 55
Binary Search Tree is a binary tree in which every node
contains only smaller values in its left subtree and only larger
values in its right subtree.
56
Operations on a Binary Search Tree
1. Search
2. Insertion
3. Deletion
4. Traversal
57
Search Operation in BST
In a binary search tree, the search operation is performed with O(log n) time
complexity. The search operation is performed as follows...
● Step 1 - Read the search element from the user.
● Step 2 - Compare the search element with the value of root node in the tree.
● Step 3 - If both are matched, then display "Given node is found!!!" and
terminate the function
● Step 4 - If both are not matched, then check whether search element is smaller
or larger than that node value.
● Step 5 - If search element is smaller, then continue the search process in left
subtree.
● Step 6- If search element is larger, then continue the search process in right
subtree.
● Step 7 - Repeat the same until we find the exact element or until the search
element is compared with the leaf node
● Step 8 - If we reach to the node having the value equal to the search value then
display "Element is found" and terminate the function.
● Step 9 - If we reach to the leaf node and if it is also not matched with the
search element, then display "Element is not found" and terminate the
function.
58
Insertion Operation in BST
In a binary search tree, the insertion operation is performed with O(log n) time
complexity. In binary search tree, new node is always inserted as a leaf node. The
insertion operation is performed as follows...
● Step 1 - Create a newNode with given value and set its left and right to NULL.
● Step 2 - Check whether tree is Empty.
● Step 3 - If the tree is Empty, then set root to newNode.
● Step 4 - If the tree is Not Empty, then check whether the value of newNode is
smaller or larger than the node (here it is root node).
● Step 5 - If newNode is smaller than or equal to the node then move to its left
child. If newNode is larger than the node then move to its right child.
● Step 6- Repeat the above steps until we reach to the leaf node (i.e., reaches to
NULL).
● Step 7 - After reaching the leaf node, insert the newNode as left child if the
newNode is smaller or equal to that leaf node or else insert it as right child.
59
Deletion Operation in BST
In a binary search tree, the deletion operation is performed with
O(log n) time complexity. Deleting a node from Binary search
tree includes following three cases...
● Case 1: Deleting a Leaf node (A node with no children)
● Case 2: Deleting a node with one child
● Case 3: Deleting a node with two children
60
Case 2: Deleting a node with one child
We use the following steps to delete a node with one child from BST...
● Step 1 - Find the node to be deleted using search operation
● Step 2 - If it has only one child then create a link between its parent node and
child node.
● Step 3 - Delete the node using free function and terminate the function.
1
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
http://www.simplycs.in
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
6
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
6
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
6
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
6
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
6
Binary search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
6
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
6
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
2
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
2
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
2
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
2
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
2
Binary Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
2
Binary Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
2
Binary Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
2
Binary Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
2 5
Binary Tree Insertion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
2 5
Binary Search Tree Deletion
Binary Search Tree Deletion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
2 5
Binary Search Tree Deletion
Case 1: Deleting Leaf Node
1 10 8 4 6 3 2 5
1
10
8
4
3 6
2 5
Binary Tree Deletion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
5
Binary Tree Deletion
1 10 8 4 6 3 2 5
1
10
8
4
3 6
5
Binary Tree Deletion
1 10 8 4 6 3 5
1
10
8
4
3 6
5
Binary Tree Deletion
1 10 8 4 6 3 5
1
10
8
Case 2: 4
Deleting node with 1 child
3 6
5
Binary Tree Deletion
1 10 8 4 6 3 5
1
10
8
4
3 6
5
Binary Tree Deletion
1 10 8 4 6 3 5
1
10
3 6
5
Binary Tree Deletion
1 10 8 4 6 3 5
1
10
3 6
5
Binary Tree Deletion
1 10 8 4 6 3 5
1
10
4
3 6
5
Binary Tree Deletion
1 10 4 6 3 5
1
10
4
3 6
5
Binary Tree Deletion
1 10 4 6 3 5
1
10
4
3 6
5
Case 3:
Deleting node with 2 child
Binary Tree Deletion
1 10 4 6 3 5
1
10
4
3 6
5
Binary Tree Deletion
1 10 4 6 3 5
1
10
4
3 6
5
Binary Tree Deletion
1 10 4 6 3 5
1
10
4
3 6
5
Binary Tree Deletion
1 10 4 6 3 5
1
10
5
3 6
5
Binary Tree Deletion
1 10 4 6 3 5
1
10
5
3 6
5
Binary Tree Deletion
1 10 4 6 3 5
1
10
5
3 6
5
Binary Tree Deletion
1 10 4 6 3 5
1
10
5
3 6
Binary Tree Deletion
1 10 4 6 3 5
1
10
5
3 6
Binary Tree Deletion
1 10 6 3 5
1
10
5
3 6
http://www.simplycs.in
Implementaion of Binary Search Tree using C Programming Language
#include<stdio.h> q = root; // q is used to traverse
#include<conio.h> the tree
while(1)
#include<stdlib.h> {
if(p->data > q->data)
{
struct node if(q->right ==
{ NULL)
{
int data; q->right = p;
struct node *left; break;
}
struct node *right; else
}; q = q->right;
}
else
{
if(q->left == NULL)
void inorder(struct node *root) {
{ q->left = p;
break;
if(root) }
{ else
q = q->left;
inorder(root->left); }
printf(" %d",root->data); }
inorder(root->right); }
}
}
}
printf("\nBinary Search Tree nodes in Inorder Traversal:
11/9/2021
int main()
");
inorder(root);
135
11/14/2021 136
Application of BST
• Expression tree
• Huffman Encoding-An Application of Binary
Trees and Priority Queue
11/9/2021 137
Expression tree
Binary tree are widely used to store
algebraic expressions
1) exp=(a-b)+(c*d)
2) exp=((a+b)-(c*d))%((e^f)/(g-h))
138
• Huffman Encoding
• Using the Huffman Coding technique, we can compress the
string to a smaller size.
• Huffman coding first creates a tree using the frequencies of
the character and then generates code for each character.
• Once the data is encoded, it has to be decoded. Decoding is
done using the same tree.
• Huffman Coding prevents any ambiguity in the decoding
process using the concept of prefix code ie. a code
associated with a character should not be present in the
prefix of any other code.
139
Purpose of Huffman Coding
Proposed by Dr. David A. Huffman in 1952
“A Method for the Construction of Minimum Redundancy
Codes”
Applicable to many forms of data transmission
Our example: text files
140
Huffman coding is done with the help of the following
steps.
142
Problem
1. Huffman Code for each character
2. Average code length
3. Length of Huffman encoded message (in bits)
Characters : a,e,i,o,u,s,t,
Frequencies: 10,15,12,3,4,13,1
143
144
145
146
147
● We assign weight to all the edges of the constructed
Huffman Tree.
● Let us assign weight ‘0’ to the left edges and weight
‘1’ to the right edges.
Rule
● If you assign weight ‘0’ to the left edges, then assign
weight ‘1’ to the right edges.
● If you assign weight ‘1’ to the left edges, then assign
weight ‘0’ to the right edges.
● Any of the above two conventions may be followed.
● But follow the same convention at the time of decoding
that is adopted at the time of encoding.
148
149
1. Huffman Code For Characters-
To write Huffman Code for any character, traverse the Huffman Tree from root node
to the leaf node of that character.
Following this rule, the Huffman Code for each character is-
● a = 111
● e = 10
● i = 00
● o = 11001
● u = 1101
● s = 01
● t = 11000
150
2. Average Code Length-
151
3. Length of Huffman Encoded Message-
Total number of bits in Huffman encoded message
= Total number of characters in the message x Average code
length per character
= ∑ ( frequencyi x Code lengthi )
Using formula-02:
Total number of bits in Huffman encoded message
= Total number of characters in the message x Average code
length per character
= 58 x 2.52
= 146.16
≅ 147 bits
152
Que 1: Suppose have 7 elements with weight as
follows:
Item-A,B,C,D,E,F,G
Weight-15,10,5,3,7,12,25
Create an extended binary tree by Huffman algorithm
and find out
a) weighted path length
b) Average code length
c) Total number of bits in Huffman encoded message
153
Que 2: Find the Huffman codes for the symbols
appearing in the string “MALAYALAM”.
Item-A,B,C,D,E,F,G
Weight-15,10,5,3,7,12,25
Create an extended binary tree by Huffman algorithm
and find out
a) weighted path length
b) Average code length
c) Total number of bits in Huffman encoded message
11/14/2021 154
Que 3: Find the Huffman codes for the symbols
appearing in the string
Item-A,B,C,D,E,F,G
Weight-16,11,7,20,23,5,15
Create an extended binary tree by Huffman algorithm
and find out
a) weighted path length
b) Average code length
c) Total number of bits in Huffman encoded message
11/14/2021 155
156
11/14/2021 157