Binary Tree - DS
Binary Tree - DS
A Simplified Approach
to
Data Structures
Prof.(Dr.)Vishal Goyal, Professor, Punjabi University Patiala
Dr. Lalit Goyal, Associate Professor, DAV College, Jalandhar
Mr. Pawan Kumar, Assistant Professor, DAV College, Bhatinda
3
INTRODUCTION
TREE
Tree is a finite non-empty set of elements
in which first element is called Root and
remaining elements are partitioned into a
number of disjoint subsets each of which
is itself a tree. Each element in a tree is
called Node.
4
GENERAL REPRESENTATION OF
TREE
In tree, A is root node
and remaining elements
A
are nodes. The subset to
the left of the root node is
B C
called Left subtree and
node to the right of the root
is called Right subtree. D E F G
The elements of the tree
have the parent -
child relationship.
CONTINUED….
ROOT
The root of a tree is the origin of the tree form
where the tree origins or starts . Node A is the root of
the tree.
ROOT
A
EDGE B C
B C 6
CONTINUED….
• SUCCESSOR
Left and right subtree of tree are called as
successors or child of a node. A is having two
successors as B and C. A
B C
• TERMINAL NODE
A node is called as terminal node if it has no
children. A
B C
D E F
7
• PARENT NODE
Node A is said to be
A
parent of B and C.
Similarly B is the parent
B C
of D and E.
• SIBLINGS
D E F G
The nodes which are
having same parent are
known as siblings. B and H I J
C are the siblings as they
are children of same
parent node A.
8
TREE TERMINOLOGIES
• PATH A
A path between any two nodes
in tree is a sequence of nodes in B C
which successive nodes are
D E F
connected by edges.
Path from A to D
ABD
Path from A to E
ABE
LENGTH Path from A to F
ACF
The length of a path in a tree is
total number of edges which Length of Path A to B:1
come across that path. Length of Path A to C:1
Length of Path A to D:2
Length of Path A to E:2
Length of Path A to F:2 9
CONTINUED…
• HEIGHT
The height of any node in the tree is length of the
longest path from that node to a terminal node . The
height of the root is treated as the height of tree.
A Height of A:3
Height of B:2
B C Height of C:2
Height of D:1
Height of E:1
D E F Height of F:1
Height of G:0
Height of H:0
Height of I:0
G H I J
Height of J:0 10
CONTINUED….
• DEGREE
Degree of A =
The degree of node can be defined as number
2 of
child nodes it has. A leaf node always hasofdegree
Degree B
A =2
zero. Degree of C
B C =1
Degree of D
D E F =0
• LEVEL Degree of E =
0
The level of node is the length or Degree
path from the
of F =
root. The root node of the tree has level 00,andthe
level of any node in the tree is one more than
11
the
level of its father.
A
LEVEL 0
B C D
LEVEL 1
E F G H I J K
LEVEL
2
L M N O P Q R S T
LEVEL 3
B C
D E F G
H I J K
13
CONTINUED….
14
TYPES OF BINARY TREE
14
SIMILAR BINARY TREE
Two binary tree are called similar if both are having
similar structure but the elements in both the tree
can be different.
A 1
B C 2 3
D E F 4 5 6
F 7
16
EQUIVALENT BINARY TREE
A A
B C B C
D E F D E F
G G
17
COMPLETE BINARY TREE
A binary tree is said to be complete if it contains the
maximum number of nodes at each level except the
last level.
B C
C D E F
G H I J K L M N
18
STRICTLY BINARY TREE
A binary tree is called Strictly binary tree if all
non leaf nodes of tree contains exactly two
children. Every non leaf node of the binary tree
contains left right subtree.
B C
D E F G
H I J K 19
PROPERTIES OF BINARY TREE
20
CONTINUED….
21
MEMORY REPRESENTATION OF
BINARY TREE
1) Linked Representation
2) Sequential Representation
22
Linked Representation Of Binary Tree
Each element of tree is represented by a node having three
parts.
• 1st Part (Info)- Which stores the element.
• 2nd Part (left)- Stores the address of left child node.
• 3rd Part (Right)- Stores the address of right child node.
A A
B C B C
23
SEQUENTIAL
REPRESENTATION
• Root of tree is always stored at the 1st array index &
its left and right child will be stored at 2nd and 3rd
index respectively.
H I
25
OPERATIONS PERFORMED ON BINARY
TREES
Various operations performed on Binary Tree
are:
1) Traversing.
2) Finding the number of external and
internal nodes.
26
TRAVERSING BINARY TREE
• Traversing is the process of visiting each node in the tree.
• There are standard methods for traversal of Binary Tree:
o Pre-Order Traversal.
o In-Order Traversal.
o Post-Order Traversal.
28
PRE ORDER TRAVERSAL
B C
D E F
32
IN ORDER TRAVERSAL
A
B C
D E F
37
POST ORDER
TRAVERSAL
A
B C
D E F
42
TRAVERSING USING
RECURSION
• We can also do traversing using recursive in binary
tree.
• In recursion every node is traversed and creates a
copy of every call just as factorial program through
recursion.
• There are standard methods for the traversal of
Binary Tree through recursion, these are:
oPre-Order Traversal
oIn-Order Traversal
43
oPost-Order Traversal
PRE ORDER TRAVERSAL
A A
B C B C
D E F D E F
G G
RecPreTraversal (Root)
A A
B C B C
D E F D E F
G G
Call RecInTraversal(Root)
Step 1: If Root = Null
Print “Tree is Empty”
Return
[End If]
Step 2: If Root Left ≠ Null Then
Call RecInTraversal (Root Left) 48
[End If]
CONTINUED….
49
POST ORDER TRAVERSAL
A A
B C B C
D E F D E F
G G
RecPostTraversal (Root)
Step 1: If Root = Null
Print “Tree is Empty”
Return
[End if]
Step 2: If Root Left ≠ Null Then
Call RecPostTraversal (Root left)
[End if] 51
CONTINUED….
52
TO FIND INTERNAL AND
EXTERNAL NODES
• The nodes which do not have any left and right child
are said to be external nodes or leaf nodes.
54
ALGORITHM
Count the number of external and internal nodes in
a Binary Tree using the pre-order traversal
[End If]
2) If Pointer Left ≠ Null Then
Set Pointer = Pointer Left
Set Internal = Internal + 1
Else If
Pointer Right = Null Then 56
External = External + 1
CONTINUED….
Else
Internal = Internal + 1
[End If]
Set Pointer = Stack Top
Decrement the Stack’s variable Top by 1
[End If]
[End Loop]
Step 5: Print : “Number of Internal and External
nodes are:” Internal , External.
Step 6: Exit
57