Dsa CH 5
Dsa CH 5
Tree terminology,
Types of Trees,
Binary tree representation,
Operations on binary tree,
Traversal of binary tree,
Binary search tree,
Expression tree,
Threaded Binary Tree.
Application of Trees - file system structure of an
operating system
3
Linked lists are linear data structures, trees are
non-linear data structures.
In a linked list, each node has a link which points
to another node.
In a tree structure, however, each node may point
to several nodes, which may in turn point to
several other nodes.
Thus, a tree is a very flexible and a powerful data
structure that can be used for a wide variety of
applications.
4
Tree-like data structures are
❑ Recursive data structures
❑ Consisting of nodes
❑ Each node can be connected to other nodes
5
Real World In Data Structure
6
• Faster than linear data structures
• Suitable for Hierarchical structure
A tree consists of a collection of nodes
that are connected to each other.
10
There are nodes in the tree that do not
have any children. Such nodes are
called leaf nodes.
11
In a tree, any node can be considered
to be a root of the tree formed by
considering only the descendants of
that node.
12
Internal node : nodes that have children
External node/ leaf: nodes that don’t have children
14
Similarly, we can define a node to be a
descendant of another node if it is the
child of the node, or the child of some
other descendant of that node.
15
Ancestor of “u” Descendant of “u”
16
❖ Height : Number of nodes which must be
traversed from the root to reach a leaf of a tree.
17 Depth 0
Height = 2 9 14 15 Depth 1
6 5 8
Depth 2
18
The length of the longest path from the
root to any node is known as the depth of
the tree.
19
The degree of a node is defined as the number of
its children
deg(9) = 2
Nodes with degree zero are also called leaf nodes
17
9 14 15
6 5 8
20
A tree is binary if each node of the tree
can have maximum of two children.
21
Example: A
B C
D F G
22
Binary trees: Each node has at most 2 children
Root node
Right child
17
Left subtree
9 15 Right child
6 5 8 10
Left child
23
STRICTLY BINARY TREE
COMPLETE BINARY TREE
EXTENDED BINARY TREE
24
AStrict/Proper binary tree can be
defined that each node should contain
either 0 or 2 Child nodes
17 Level 0
9
15 Level 1
6 5 Level 2
25
A complete binary tree can be defined
Every level except the possibly last , is completely
filled.
All nodes are as far left as possible.
26
Example: A
B C
D E F G
H I
27
Example:
28
All levels are completely filled.
This is also called as a balanced binary
tree.
Number of node at level i in a perfect
binary tree is given by 2i.
Maximum No. of nodes in a tree with
height ‘h’ = 2h+1-1
29
30
Acomplete binary tree is a binary tree
in which every level, except possibly
the last, is completely filled, and all
nodes are as far left as possible.
3 9
1 4 6
34
35
ARRAYS : A
7 A B C D \0 F G
0 1 2 3 4 5 6 7
36
A Level 0
B C Level 1
D E G Level 2
Level 3
I
9 A B C D E \0 G \0 I
0 1 2 3 4 5 6 7 8 9
37
LINKED LISTS : A
•Data B C
•Address of Left child
•Address of Right child D F G
struct node
{
char data;
struct node *left, *right;
}; 38
IMPLEMENTATION OF BINARY
TREE USING LINKED LISTS
39
/*Create a new Node*/
40
main()
{
struct node *root = NULL;
………..
if (root == NULL) /* Tree is not Created */
root = temp;
else
insert(root, temp);
41
/*Insert a Node*/
42
if (temp->data > root->data)
{
if (root->right == NULL)
root->right = temp;
else
insert(root->right, temp);
}
}
43
struct node search(struct node root, int key)
{
struct node *temp;
temp = root;
while (temp != NULL)
{
if (temp->data == key)
{
printf(“The %d Element is Present", temp->data);
return temp;
}
44
if (temp->data > key)
temp = temp->left;
else
temp = temp->right;
}
return NULL;
}
45
Traversing a tree means to visit each of
its nodes exactly one in particular
order.
47
Depth-First Search (DFS)
❖Visit node's successors first
❖Usually implemented by recursion
48
Depth-First Search first visits all
descendants of given node recursively,
finally visits the node itself
7
7
3 6
1 14
1 2 4 5
1 22 13 16
49
Basedon the relative order, there are 3
popular DFS strategies.
Inorder
Preorder
Postorder
50
F
B G
A D I
C E H
A BCD E F G H I
51
52
void inorder(struct node temp)
{
if (temp != NULL)
{
inorder(temp->left);
printf("%d", temp->data);
inorder(temp->right);
}
53
Preorder traversal
• Process the value in the node.
• Traverse the left sub tree with a preorder traversal
• Traverse the right sub tree with a preorder traversal
54
F
F BADCEG I H
B G
A D I
C E H
55
void preorder(struct node temp)
{
if (temp != NULL)
{
printf("%d", temp->data);
preorder(temp->left);
preorder(temp->right);
}
} 56
Post order traversal
• Traverse the left sub tree with a post order traversal
• Traverse the right sub tree with a post order
traversal
• Process the value in the node
57
F
ACE DBH I GF
B G
A D I
C E H
58
void postorder(struct node *temp)
{
if (temp != NULL)
{
postorder(temp->left);
postorder(temp->right);
printf("%d", temp->data);
}
} 59
Breadth First Search:
60
F
F BG A D I C E H
B G
A D I
C E H
61
Trees are used in many other ways in the
computer science.
63
An expression tree for an arithmetic,
relational, or logical expression is a binary
tree in which:
65
*
+ 3
4 2
( 4 + 2 ) * 3 = 18
66
((8-5)*((4+2)/3))
67
*
- /
8 5 + 3
4 2
Infix: ((8-5)*((4+2)/3))
Prefix: *-85 /+423
Postfix: 85- 42+3/*
68
2*(1+(2*1))
Construct the Expression tree
69
*
2 +
1 *
2 1
2*(1+(2*1))
70
we will read a symbol from the
postfix expression.
73
ab+cde+**
ab
74
a b+cde+**
a b
75
a b+cde+**
a b
76
a b+cde+**
c d
a b
77
a b+cde+**
c d e
a b
78
a b+cde+**
+ +
a b d e
79
a b+cde+**
*
+
C +
a b
d e
80
a b+cde+**
*
+
C +
a b
d e
81
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.
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. 82
Two types of threaded Binary Tree:
One-way threaded Binary Tree
Two-way threaded Binary Tree
84
One thing to note is that the leftmost and the
rightmost child pointer of a tree always points to null
as their in-order predecessor and successor do not
exist.
85
File system
Storing hierarchies in organizations
86