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

Module-4 Trees

Uploaded by

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

Module-4 Trees

Uploaded by

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

Vasantdada Patil Pratishthan’s College of

Engineering & Visual Arts, Sion, Mumbai.

Data Structure (DS )


(CSC303)
S.E. A Div SEM-III(Computer Engineering)

MODULE NO. 4
TREE

TOPIC

Introduction, Tree Terminologies, Binary Tree, Binary Tree


Representation, Types of Binary Tree, Binary Tree Traversals,
Binary Search Tree, Operations on Binary Search Tree,
Applications of Binary Tree-Expression Tree, Huffman Encoding,
Search Trees-AVL, rotations in AVL Tree, operations on AVL Tree,
Introduction of B Tree, B+ Tree.
11/14/2021 By Mrs. Prajakta S. Khelkar 1
Contents
• Introduction of tree
– Basic terminology
• Types of tree
– General Tree
– Binary Tree
– Binary Search Tree
– AVL Tree
– B Tree
– B+ Tree
• Application of trees

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

data link 1 link 2 ... link n

How many link fields are


needed in such a representation?
11/14/2021 7
Node
• node is an entity that contains a key or value
and pointers to its child nodes.
• The last nodes of each path are called leaf
nodes or external nodes that do not contain
a link/pointer to child nodes.
• The node having at least a child node is
called an internal node.

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-

•The connecting link between any two nodes is called as an edge.


•In a tree with n number of nodes, there are exactly (n-1) number of edges.

To join the video meeting, click this link:


https://meet.google.com/seo-sgmo-mnt
Otherwise, to join by phone, dial +1 785-430-
6597 and enter this PIN: 932 702 330#

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.

•Node A is the parent of nodes B and C


•Node B is the parent of nodes D, E and F
•Node C is the parent of nodes G and H
•Node E is the parent of nodes I and J
•Node G is the parent of node K
11/14/2021 11
4. Child
• In a tree data structure, the node which is
descendant of any node is called as CHILD Node.
In simple words, the node which has a link from its
parent node is called as child node. In a tree, any
parent node can have any number of child nodes.
In a tree, all the nodes except root are child nodes.

11/14/2021 12
5. Siblings

• In a tree data structure, nodes which belong to


same Parent are called as SIBLINGS. In simple
words, the nodes with the same parent are called
Sibling nodes.

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.

In a tree data structure, the leaf nodes are also called


as External Nodes. External node is also a node with
no child. In a tree, leaf node is also called as
'Terminal' node.

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

• Storing naturally hierarchical data-File System uses


tree architecture.
• Organige data for quick search ,insertion,deletion-
Binary Search Tree
• Represend algebraic Formulas
• Diverse application such as Artificial Intelligence &
encodeing algorithm.
• Dictionary
• Network Routing Algorithm.

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

C Skewed Binary Tree


3 D E F G
D

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)

• 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

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

Case 1: Deleting a leaf node


We use the following steps to delete a leaf node from BST...
● Step 1 - Find the node to be deleted using search operation
● Step 2 - Delete the node using free function (If it is a leaf) and terminate the
function.

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.

Case 3: Deleting a node with two children


We use the following steps to delete a node with two children from BST...
● Step 1 - Find the node to be deleted using search operation
● Step 2 - If it has two children, then find the largest node in its left subtree
(OR) the smallest node in its right subtree.
● Step 3 - Swap both deleting node and node which is found in the above step.
● Step 4 - Then check whether deleting node came to case 1 or case 2 or else
goto step 2
● Step 5 - If it comes to case 1, then delete using case 1 logic.
● Step 6- If it comes to case 2, then delete using case 2 logic.
● Step 7 - Repeat the same process until the node is deleted from the tree.
61
Binary Search Tree Insertion
Binary Search Tree Insertion
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
Binary Search Tree Insertion
1 10 8 4 6 3 2 5
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
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.

1. Calculate the frequency of each character in the string.


2. Sort the characters in increasing order of the frequency. These
are stored in a priority queue Q.
3. Make each unique character as a leaf node.

4. sum of two minimum frequencies.

5. again arrange them in increasing order

6. repeat the above steps.

7. construct the huffman tree and assign code to them for

left assign 0 and for right assign 1.


141
Major Steps in Huffman Coding-

There are two major steps in Huffman Coding-


1. Building a Huffman Tree from the input characters.
2. Assigning code to the characters by traversing the
Huffman Tree.

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

From here, we can observe-


● Characters occurring less frequently in the text are assigned the larger code.
● Characters occurring more frequently in the text are assigned the smaller code.

150
2. Average Code Length-

Using formula -01:


Average code length
= ∑ ( frequencyi x code lengthi ) / ∑ ( frequencyi )
= { (10 x 3) + (15 x 2) + (12 x 2) + (3 x 5) + (4 x 4) + (13 x 2) +
(1 x 5) } / (10 + 15 + 12 + 3 + 4 + 13 + 1)
= 2.52

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

You might also like