Module 2
Module 2
HIERARCHICAL -
1
Module 2
INTRODUCTION TO ADT:
2
Coding competition
[Link]
[Link]
[Link]
[Link]
3
Trees
4
Introduction to Tree terminology
• A tree is an abstract model of a hierarchical structure that consists of nodes with a parent-child
relationship.
• for example, records, family trees and table of contents.
B C
Left subtree
Right subtree
D E F G
Edges H I Leaves
J K L M N
6
What is a Tree?
Tree is a non-linear data structure which organizes data in a hierarchical structure
7
What is a Tree?
• A tree is a connected graph without any circuits.
• If in a graph, there is one and only one path between every pair of vertices, then
graph is called as a tree.
8
What is a Tree?
9
Tree-Basic Terminology
1) Root − Node at the top of the tree is called root.
10
Tree-Basic Terminology
2) Edge − In a tree data structure, the connecting
link between any two nodes is called as EDGE.
11
Tree-Basic Terminology
12
Tree-Basic Terminology
• 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.
13
Tree-Basic Terminology
5) Sibling − nodes which belong to same Parent are called as SIBLINGS.
• The nodes with the same parent are called Sibling nodes.
14
Tree-Basic Terminology
15
Tree-Basic Terminology
7) Internal nodes − the node which has atleast one
child is called as INTERNAL Node.
16
Tree-Basic Terminology
8) Degree− the total number of children of a node is
called as DEGREE of that Node.
17
Tree-Basic Terminology
9) Level−
• 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).
18
Tree-Basic Terminology
10) Height −
• The total number of edges from leaf node to a particular
node in the longest path is called as HEIGHT of that
Node.
19
Tree-Basic Terminology
11) Depth − the total number of egdes from root node
to a particular node is called as DEPTH of that Node.
20
Tree-Basic Terminology
12) Path −
• the sequence of Nodes and Edges from one node to
another node is called as PATH between that two
Nodes.
21
Tree-Basic Terminology
13) Subtree − each child from a node forms a
subtree recursively.
22
Tree-Basic Terminology
Root Node: A
Parent of C and D: A
D is sibling of : B and C
Leaf Nodes: E, F, G, D
Internal Nodes: ABC
Depth of F: 2
Depth of Tree: 2
Depth of Root Node: 0
Height of Tree: 2
Height of A: 2
Height of G: 0
Degree of Node A: 3
Degree of Tree: 3
Degree of C 1
23
Characteristics of Trees
• Non-linear data structure
• Combines advantages of an ordered array
• Searching as fast as in an ordered array
• Insertion and deletion as fast as in the linked list
Application of Trees
• Directory structure of a file store
• Structure of arithmetic expressions
• Used in almost every 3D video game to determine what objects need to
be rendered.
• Used in almost every high-bandwidth router for storing router-tables.
• used in compression algorithms, such as those used by the .jpeg and .mp3
file- formats.
24
Representation Of Trees
Left Child-Right Sibling Representation
List Representation
25
Representation Of Trees
Left Child-Right Sibling Representation
B C D
*E *C *G *D *H NULL
List Representation
K L M
26
NULL *L NULL NULL NULL NULL
Implementation of Trees int main()
{ // Create root
create a simple tree with 4 nodes struct node* root = newNode(1);
/* following is the tree after above statement
#include <stdio.h> 1
#include <stdlib.h> /\
NULL NULL
struct node { */
int data; root->left = newNode(2);
struct node* left; root->right = newNode(3);
struct node* right;
}; /* 2 and 3 become left and right children of 1
1
// newNode() allocates a new node with the given data and NULL left and right /\
pointers // 23
/\/\
struct node* newNode(int data) NULL NULL NULL NULL
{ */
// Allocate memory for new node root->left->left = newNode(4);
struct node* node
= (struct node*)malloc(sizeof(struct node)); /* 4 becomes left child of 2
1
// Assign data to this node /\
node->data = data; 23
/\/\
// Initialize left and right children as NULL 4 NULL NULL NULL
node->left = NULL; /\
node->right = NULL; NULL NULL
return (node); */
} getchar(); 27
return 0; }
Basic Operation Of Tree Data Structure:
28
Tree Traversal -
Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all nodes are connected via edges
(links) we always start from the root (head) node.
That is, we cannot randomly access a node in a tree.
1. Pre order tree traversal
2. In order tree traversal
3. Post order tree traversal
29
30
In-order Traversal
• In this traversal method, the left subtree is visited first, then the root and later the right
sub-tree. We should always remember that every node may represent a subtree itself.
• If a binary tree is traversed in-order, the output will produce sorted key values in an
ascending order.
We start from A, and following in-order traversal, we move to its left subtree B.B is also
traversed in-order. The process goes on until all the nodes are visited.
The output of in-order traversal of this tree will be − D → B → E → A → F → C → G
31
Pre-order Traversal - In this traversal method, the root node is visited first,
then
the left subtree and finally the right subtree.
Algorithm –
32
Post-order Traversal -
• In this traversal method, the root node is visited last, hence the name. First we
traverse the left subtree, then the right subtree and finally the root node.
Algorithm –
34
Binary Trees
• A binary tree, T, is either empty or such that
i. T has a special node called the root node
ii. T has two sets of nodes LT and RT, called the left subtree and right subtree
of T, respectively.
iii. LT and RT are binary trees.
35
Binary Trees
2) Linked Representation
38
Binary Tree – Linked List Representation
• Use a linked list to represent a binary tree.
• Every node consists of three fields.
• left child address,
• actual data
• right child address. struct node
• Advantages: {
int data;
• No wastage of space struct node *left;
• Insertions and deletions are easier struct node *right;
• Disadvantages: };
• Does not provide direct access
• Needs additional space for storing left and right subtrees
39
Binary Tree – Linked List Representation
40
Binary Tree – Linked List Representation
41
Binary Tree – Linked List Representation
#include <stdio.h> // Create a new Node
#include <stdlib.h> struct node* createNode(value)
{
struct node { struct node* newNode = malloc(sizeof(struct
int item; node));
struct node* left; newNode->item = value;
struct node* right; newNode->left = NULL;
}; newNode->right = NULL;
int main() {
return newNode;
struct node* root =
}
createNode(1); // Insert on the left of the node
insertLeft(root, 12); struct node* insertLeft(struct node* root, int
insertRight(root, 9); value)
{
insertLeft(root->left, 5); root->left = createNode(value);
insertRight(root->left, 6); return root->left;
1 }
43
1. In-order Traversal (follows LDR)
The left subtree is visited first, then the root, and later the right subtree.
49
Binary Tree Traversals
50
Binary Tree Traversals
Inorder Traversal
Preorder Traversal
Postorder Traversal
51
Application - Evaluate a Binary Expression Tree
• A binary expression tree is a binary tree where operators are stored in the tree’s
internal nodes and the leaves contain constants.
52
Find the output
110
100
53
Rules
54
55
Binary Tree Traversals
56
57
Binary Search Tree (BST)
Binary Search tree can be defined as a class of binary trees, in
which the nodes are arranged in a specific order. This is also
called ordered binary tree.
• The binary search tree is considered as efficient data structure compared to arrays and
linked lists. In the searching process, it removes half sub-tree at every step.
• It also speeds up the insertion and deletion operations compared to that in array and
linked list.
Operations of BST
• Insertion
• Search
• Deletion
• Traversal
59
BST Insertion
• Insert values 43, 10, 79, 90, 12, 54, 11, 9, 50 into a BST
60
BST Insertion
61
BST Searching
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 -
• First, compare the element to be searched with the root element of the tree.
• If root is matched with the target element, then return the node's location.
• 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.
• If it is larger than the root element, then move to the right subtree.
• Repeat the above procedure recursively until the match is found.
• If the element is not found or not present in the tree, then return NULL.
64
BST Deletion
66
BST Deletion
67
BST Deletion
68
BST Deletion
69
To delete the given node from the binary search tree(BST), we
should follow the below rules –
[Link] Node –
70
Case 2 - If the node has 1 child, it is simply removed by swapping from the tree.
71
2
72
Program in C to Perform Operations on Binary Search Tree struct Node* search(struct Node* root, int value)
{
#include <stdio.h> if (root == NULL || root->data == value) {
#include <stdlib.h> return root;
}
if (value < root->data) {
struct Node { return search(root->left, value);
}
int data; return search(root->right, value);
struct Node* left; }
struct Node* right;
struct Node* delete(struct Node* root, int value)
}; {
if (root == NULL)
{
struct Node* createNode(int value) { return root;
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); }
newNode->data = value; if (value < root->data)
{
newNode->left = NULL; root->left = delete(root->left, value);
newNode->right = NULL; } else if (value > root->data)
{
return newNode; root->right = delete(root->right, value);
} } else
{
if (root->left == NULL) {
struct Node* insert(struct Node* root, int value) { struct Node* temp = root->right;
if (root == NULL) { free(root);
return temp;
return createNode(value); } else if (root->right == NULL) {
} struct Node* temp = root->left;
if (value < root->data) free(root);
return temp;
{ }
root->left = insert(root->left, value); struct Node* temp = root->right;
while (temp->left != NULL) {
} else if (value > root->data) { temp = temp->left;
root->right = insert(root->right, value); }
} root->data = temp->data;
root->right = delete(root->right, temp->data);
return root; }
} return root;
} 73
void inorderTraversal(struct Node* root)
{
if (root == NULL) {
return;
}
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
int main()
{
struct Node* root = NULL;
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70);
root = insert(root, 60);
root = insert(root, 80);
return 0;
} 74
Applications of Trees (Explain briefly below points)
Applications of tree data structure-
• Storing naturally hierarchical data - This includes a large amount of real-world data. Consider your computer's file system,
for example. The files and folders are organized hierarchically, with a root folder (typically designated by /) at the top. Each
subdirectory can have other subfolders, and so on. When storing such data, a tree data structure is the most intuitive
approach to do so
• Database indexing –
-Large collections of frequently updated records.
-A single key or a combination of keys is used to search.
-Use of key range queries for min/max searches
• Parsing - Parsing is the process of breaking down code into its constituent pieces using grammar.
• Artificial Intelligence – Used in the field of machine learning. They are used in various algorithms to model complex
relationships between inputs and outputs and to classify and make predictions. Decision Trees(used in AI &ML), Random
Forests,
• Cryptography - to provide integrity and authenticity guarantees for data transmitted over a network
• Binary Search – Searching
• Expression trees are beneficial for evaluating and manipulating mathematical expressions efficiently
• 3D video Game development 75
AVL Tree
• The term AVL tree was introduced by Adelson-Velsky and Landis.
It is a balanced binary search tree and is the first data structure
like this. In the AVL tree, the heights of the subtree cannot be
more than one for all nodes.
It is clearly visible that the heights of the The heights of the left and right subtrees are
left and right subtrees are equal to, or less higher than 1.
than one.
76
Balance Factor in AVL Tree
• The balanced factor should be -1, 0 or +1. Otherwise, the tree will be considered an
unbalanced tree.
• To reduce the issue of time complexity in a binary search tree, the AVL tree was
introduced by Adelson-Velski & Landis. It is a self-balancing tree that helps in reducing
the complexity issue
77
• Operations on AVL Tree
• The AVL tree is a balancing binary tree, and therefore it follows the same operations we perform in
the binary search tree.
1. Insertion
2. Deletion
• Insertion: The process of insertion is the same as it is executed in the binary search tree. However,
there are chances that it may point to a violation in the AVL tree property, and the tree may
require balancing. To balance a tree we can apply rotations.
• Deletion: The process of deletion is the same as it is executed in a binary search tree. It can affect
the balance factor of the tree, therefore, we need to utilize different types of rotations to balance
the tree.
78
AVL Rotation
• Left rotation
• Right rotation
• Left-Right rotation
• Right-Left rotation
79
[Link] Rotation: When we perform insertion at the left subtree, then
it is a left rotation.
80
2. Right Rotation: When we perform insertion at the right subtree,
then it is a right rotation.
81
82
3. Left-Right Rotation
83
• Right-Left Rotation
84
Expression trees
• An expression tree is a tree built up from the simple operands as the leaves of
binary tree and operators as the non -leaves of binary tree.
• It is a special kind of binary tree in which:-
(i) Each leaf node contains single operand.
(ii) Each non-leaf node contains a single binary operator.
(iii) The left and right subtrees of an operator node represent sub-expression that
must be evaluated before applying the operator at the root of the subtree.
85
• The levels in a binary expression tree represent the precedence of operators.
• The operators at the lower level must be evaluated first and then the operators at
the next level and so on and at the last operator at the root node is applied and
there by the expression is evaluated.
87
• A Red-Black Tree is a self-balancing binary search tree where
each node has an additional attribute: a color, which can be
either red or black.
• The primary objective of these trees is to maintain balance
during insertions and deletions, ensuring efficient data retrieval
and manipulation..
88
Properties of Red-Black Trees
[Link] Property: Red nodes cannot have red children (no two
consecutive red nodes on any path).
93