Book
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
Shroff Publications and Distributors
Edition 2014
Prof.(Dr.) Vishal Goyal,Department of Computer Science, Punjabi University
BINARY TREE
Department of Computer Science, Punjabi University Patiala 2
CONTENTS
• Introduction
• Tree
• Binary Tree
o Properties of binary tree.
o Memory representation of binary tree.
o Operations performed on binary tree.
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
A line which connect a parent node to its
child node is called Edge or Branch.
A
EDGE
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
LEVELS OF NODES IN A TREE
12
BINARY TREE
A binary tree can be defined as a finite collection of
nodes where each node n is having the property that
it can have 0,1 or 2 children.
A
B C
D E F G
H I J K
13
CONTINUED….
• A binary tree can be defined as a finite collection of
nodes where each node n is having the property that it
can have 0,1 or 2 children.
• A binary tree may be empty known as NULL tree or it
contains a special node called root of the tree and
remaining nodes in the tree form the left right binary
sub trees.
14
TYPES OF BINARY TREE
• Similar Binary Trees
• Equivalent Binary Trees
• Complete Binary Trees
• Strictly Binary Trees
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
Two binary trees are said to be equivalent or copies
if they are similar & are having the same contents in
their respective nodes.
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
• A Binary Tree with n nodes has exactly n-1 edges.
• In a binary tree every node except the root node has
exactly one parent.
• In a binary tree there is exactly one path connecting
any two nodes in the tree.
• The minimum number of nodes in a binary tree of
height h is h+1.
20
CONTINUED….
• The maximum number of nodes in a binary tree of
height h is
2(h+1)−1.
• Number of leaf nodes in a complete binary tree is
(n+1)/2.
• In a complete binary tree,
Number of external nodes = Number of internal
nodes+1.
21
MEMORY REPRESENTATION OF
BINARY TREE
A binary tree can be represented into computer
memory
using two ways:-
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
D E F G NUL D NUL NUL E NUL NUL F NUL NULL G NUL
L
L L L L L L
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.
• If a node occupies the Kth index of array then its:-
o Left child will be stored at (2 × k)th array index.
o Right child will be stored at (2 × (k + 1))th array index.
• Sequential representation of binary tree of height h
will require an array size 2(h+1) - 1
24
CONTINUED… [1] A
[2] B
[3] C
A [4] D
[1] A E
[5] F
[2] B [6] G
B [3] - [7] H
[4] C [8] I
C [5] - A [9]
[6] -
[7] -
D D B C
[8]
[9] -
[10] E
E D E F G
[11] -
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.
• While traversing, three main activities take place:
o Visiting the Root.
o Traversing the left subtree.
o Traversing the right subtree.
27
PRE ORDER TRAVERSAL
• This is also known as depth-first order or Root-
Left-Right traversal.
• In this method, traversal order followed is:
o Visit the root.
o Traverse the left sub tree .
o Traverse the right sub tree
28
PRE ORDER TRAVERSAL
B C
D E F
Nodes are visited in preorder as: AB DC E F G
29
ALGORITHM
Traverse Binary Tree In Pre Order Manner.
Step1: If Root=Null Then
Print ”Tree is Empty”
Exit
Else
Set Pointer=Root
[End If]
Step2: Initialize an empty Stack by pushing Null
into it and setting the Stack variable Top to 1
Step3: Repeat While Pointer ≠ Null
Print Pointer → Info
30
If Pointer → Right ≠ Null Then
CONTINUED….
Push Pointer Right onto the Stack by
incrementing stack’s variable Top.
If Pointer Left ≠ Null Then
Set Pointer=Pointer Left
Else
Set Pointer=Stack Top
Decrement the Stack’s variable Top
by 1
[End If]
[End Loop]
Step4: Exit 31
IN ORDER TRAVERSAL
• This is also known as symmetric order or Left-
Root-Right traversal.
• In this method, traversal order followed is:
o Traverse the left subtree.
o Visit the root.
o Traverse the right subtree .
32
IN ORDER TRAVERSAL
A
B C
D E F
The nodes are visited in order as : B D A EC G F
33
ALGORITHM
Traverse Binary Tree In In Order Manner.
Step1: If Root=Null Then
Print ”Tree is empty”
Exit
Else
Set Pointer=Root
[End If]
Step2: Initialize an empty Stack by pushing Null
into it and setting the Stack variable Top to 1
Step3: Set Flag=True
34
CONTINUED…
Step 4: Repeat Steps 5 to 10 while Flag = True
Step 5: Repeat while Pointer ≠ Null
Push Pointer onto the stack and
Increment the stack variable
Top by 1
Set Pointer = Pointer Left
[End Loop]
Step 6: Set Pointer = Stack Top
Step 7: Decrement the stack variable Top by 1
Step 8: Set Flag= False
Step 9: Repeat while Pointer ≠ Null and Flag = 35
False
Continued…
Set Pointer = Pointer Right
Set Flag = True
Else
Pointer=Stack Top
decrement the stack variable Top by 1
[End If]
[End Loop]
Step 10: If Pointer = Null Then
Set Flag = False
[End If]
[End Loop]
36
Step11: Exit
POST ORDER TRAVERSAL
• This is also known as Left-Right-Root traversal.
• In this method, traversal order followed is:
o Traverse the left subtree in Post-order traversal.
o Traverse the right subtree in Post-order traversal.
o Visit the root.
37
POST ORDER
TRAVERSAL
A
B C
D E F
The nodes are visited in Post Order as : DB E G F C A 38
ALGORITHM
Traverse Binary Tree In In Order Manner.
Step1: If Root=Null Then
Print ”Tree is empty”
Exit
Else
Set Pointer=Root
[End If]
Step2: Initialize an empty stack by pushing Null into
it and setting the stack variable Top to 1 39
Step 3: Set Flag=True
CONTINUED….
Step 4: Repeat Steps 5 to 10 while Flag = True
Step 5: Repeat while Pointer ≠ Null
Push Pointer onto the Stack and
Increment the stack variable Top by 1
Set Pointer = Pointer Left
[End Loop]
Step 6: Set Pointer = Stack Top
Step 7: Decrement the stack variable Top by 1
Step 8: Set Flag = False
40
CONTINUED….
Step 9: Repeat while Pointer ≠ Null and Flag =
False
If Pointer > 0 Then
Print: Pointer Info
Set Pointer = Stack Top
decrement the stack variable Top
by 1
Else
Set Pointer = Pointer
Set Flag = True
41
[End If]
CONTINUED….
Step 10: If Pointer = Null Then
Set Flag = False
[End If]
[End Loop]
Step 11: Exit
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
The nodes are visited in Pre Order as: A B D C E F G
44
ALGORITHM
Traversing a binary tree in Pre order manner
recursively.
RecPreTraversal (Root)
Step 1: If Root = Null
Print “Tree is Empty”
Return
Else
45
Print Root Info
Continued…
Step 2: If Root Left ≠ Null Then
Call RecPreTraversal (Root → Left)
[End If]
Step 3: If Root Right ≠ Null Then
Call RecPreTraversal (Root →
Right)
[End If]
Step 4: Return
46
IN ORDER TRAVERSAL
A A
B C B C
D E F D E F
G G
The nodes are visited in In Order as: B D A E C G F
47
CONTINUED….
Traversing a binary tree in In order manner
recursively.
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….
Step 3: Print Root Info
Step 4: If Root Right ≠ Null Then
Call RecInTraversal (Root Right)
[End if]
Step 5: Return
49
POST ORDER TRAVERSAL
A A
B C B C
D E F D E F
G G
The nodes are visited in Post Order as DB E G F C A 50
ALGORITHM
Traversing a binary tree in Postorder manner recursively.
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….
Step 3: If Root Right ≠ Null Then
Call RecPostTraversal (Root → Right)
[End if]
Step 4: Print Root Info
Step 5: Return
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.
• All other nodes having one or two children are said
to be internal nodes.
• To find the number of external/internal nodes in a
tree we have to traverse it and we can traverse the
tree using any of the three traversal methods.
53
CONTINUED….
• During traversing the
tree
o Each node will be A
tested for its number of INTERNAL
NODES
children. B C
o If it has any child then
it will be counted as D E F G
internal node
Otherwise, It will H I J K L
be counted as external
node EXTERNAL NODES
54
ALGORITHM
Count the number of external and internal nodes in
a Binary Tree using the pre-order traversal
Step 1: If Root = Null Then
Print : “Tree is Empty”
Exit
Else
Set Pointer = Root
[End If]
Step 2: Initialize an empty stack by pushing Null into
it and setting the stack variable Top to55 1
CONTINUED….
Step 3: Initialize the variable Internal=0 and External=0
Step 4: Repeat while Pointer ≠ Null
1) If Pointer Right ≠ Null Then
Push Pointer Right onto the stack by
incrementing stack variable Top
[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