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

Dsa CH 5

Uploaded by

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

Dsa CH 5

Uploaded by

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

Tree: Introduction to Trees,

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

 Examples of tree-like structures


• Trees: binary, balanced, etc.

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.

A tree contains a unique first element


known as the root, which is shown at
the top of the tree structure.

The root is the only node in the tree


that does not have a parent.
8
root TCET

Students Faculty Admin

FE SE Regular Part time


leaf
 A node which points to other nodes
is said to be the parent of the nodes
to which it is pointing.

 The nodes that the parent node


points to are called the children, or
child nodes of the parent node.

10
There are nodes in the tree that do not
have any children. Such nodes are
called leaf nodes.

Sibling:Two nodes that have the same


parent are said to be siblings.

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.

 Such a tree is called the subtree that


itself is a tree.

12
 Internal node : nodes that have children
 External node/ leaf: nodes that don’t have children

Internal node External node


13
A node is an ancestor of another node
if it is the parent of that node, or the
parent of some other ancestor of that
node.

Theroot is an ancestor of every other


node in the tree.

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.

You may note that all the nodes in the


tree are descendants of the root 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.

❖ The height of a tree is defined as the maximum


depth of any node within the tree

❖ The height of a tree with one node is 0


 Just the root node

❖ For convenience, we define the height of the


empty tree to be –1
17
• 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.

 An important feature of a tree is that there


is a single unique path from the root to
any particular node.

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.

Number of node at level i in a complete


binary tree is given by 2i.

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.

Afull /perfect binary tree is a tree in


which every node other than the leaves
has two children.
31
32
A "binary search tree" is different from a
"binary tree".
A "binary search tree" (BST) or "ordered
binary tree" is a type of binary tree where
the nodes are arranged in an order.
 For each node, all elements in its left sub
tree are less-or-equal to the node (<=)
 All the elements in its right sub tree are
greater than the parent node (>).
33
Example:
5

3 9

1 4 6

34
35
ARRAYS : A

Left child index : 2i B C

Right child index : 2i+1 D F G

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*/

struct node create_node(int value)


{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->left= NULL;
temp->data=value;
temp->right = NULL;
return temp;
}

40
main()
{
struct node *root = NULL;
………..
if (root == NULL) /* Tree is not Created */
root = temp;
else
insert(root, temp);
41
/*Insert a Node*/

void insert(struct node root, struct node *temp)


{
if (temp->data < =root->data) {
if (root->left == NULL)
root->left= temp;
else
insert(root->left, temp);
}

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.

 Many traversal algorithms are known as


❖ Depth-First Search (DFS)
❖ Breadth-First Search (BFS)

47
 Depth-First Search (DFS)
❖Visit node's successors first
❖Usually implemented by recursion

 Breadth-First Search (BFS)


❖Nearest nodes visited first
❖Implemented by a queue

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:

 Visit all the nodes at same depth or


level before visiting the nodes in
the next level.

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.

 Compilers and database are two major


examples in this regard.

 In case of compilers, when the languages


are translated into machine language, tree-
like structures are used.
62
 Its an application of binary trees
 Binary trees are used to evaluate
algebraic expression.
 An expression tree makes it easy
to visualize the expression and
evaluate it.

63
 An expression tree for an arithmetic,
relational, or logical expression is a binary
tree in which:

• The parentheses in the expression do not


appear.
• The leaves are the variables or constants in
the expression.
• The non-leaf nodes are the operators in the
expression
64
A special kind of binary tree in which:
1. Each leaf node contains a single operand

2. Each nonleaf node contains a single operator

3. The left and right subtrees of an operator node


represent sub expressions that must be
evaluated before applying the operator at the
root of the sub tree.

65
*
+ 3

4 2

( 4 + 2 ) * 3 = 18
66
((8-5)*((4+2)/3))

 Draw the Expression tree

 Traverse the tree


• Preorder
• Postorder
• Inorder

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

 Traverse the tree


• Preorder
• Postorder
• Inorder

69
*

2 +

1 *
2 1

2*(1+(2*1))

70
we will read a symbol from the
postfix expression.

In the postfix expression, we have


either operators or operands.

We will start reading the


expression from left to right.
71
Ifthe symbol is an operand, push it
on the stack.
Ifsymbol is an operator, pop two
nodes/trees from the stack, form a
new tree with operator as the root
and T1 and T2 as left and right
subtrees and push this tree on the
stack.
72
 Convert postfix expression to expression
tree.
ab+cde+**

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

In a Threaded Binary Tree, the nodes will store the in-


order predecessor/ successor instead of storing NULL in
the left/right child pointers.
83
 The basic idea of a threaded binary tree is that for the
nodes whose right pointer is null, we store the in-order
successor of the node (if-exists), and for the nodes
whose left pointer is null, we store the in-order
predecessor of the node(if-exists).

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

You might also like