Trees_part1
Trees_part1
Trees_part1
Tree Terminologies
struct node
{
struct node
*left;
int data;
struct node
*right;
}
PROPERTIES OF
BINARY TREE
PROPERTY-
01
Minimum number of nodes in a
binary tree of height H
=H+1
Example:
To construct a binary tree of
height = 4, we need at least 4
+ 1 = 5 nodes.
PROPERTY-
02
Maximum number of nodes in a binary
tree of height H
= -1
Example:
Maximum number of nodes in a binary
tree of height 3
= 23+1 – 1
= 16 – 1
= 15 nodes
PROPERTY-
03
Maximum number of nodes at
any level ‘L’ in a binary tree
= 2L
Example:
Maximum number of nodes at
level-2 in a binary tree
= 22
=4
PROPERTY-04
Total Number of leaf nodes in a Binary Tree
= Total Number of nodes with 2 children + 1
Here,
Valid Invalid
complete complete
binary tree binary tree
PERFECT
BINARY TREE
oIt is a Binary Tree in which all
internal nodes have 2
children, and all the leaf
nodes are at the same depth
or same level.
oTotal number of nodes in a
Perfect Binary Tree with
height H is 2^H - 1.
Valid perfect Invalid
binary tree perfect
binary tree
BALANCED
BINARY TREE
oIt is a Binary tree in which height of
the left and the right sub-trees of
every node may differ by at most 1.
oDiff=|ht. of left subtree- ht. of right
subtree|
oAVL Tree is well-known data structure
to generate/maintain Balanced Binary
Search Tree.
Valid Invalid
balanced perfect
binary tree binary tree
BINARY TREE
REPRESENTATI
ON
REPRESENTATION OF BINARY
TREE
There are two different methods for representing:
a) using array
oThe index 1 is holding the root, it has two children 5 and 16, they
are placed at location 2 and 3.
REPRESENTATION OF BINARY
TREE
oSome children are missing, so their place in array is left as blank.
oThis approach is good, and easily we can find the index of parent
and child, but it is not memory efficient.
oIt will occupy many spaces that has no use.
oThis representation is good for complete binary tree or full binary
tree.
REPRESENTATION OF BINARY
TREE
b) using linked list
BINARY TREE
TRAVERSAL
BINARY TREE TRAVERSAL
oTraversal 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.
oIn tree, relative order of traversing left subtree, right subtree &
visiting root can be different.
oTypes of traversals:
1. Preorder – VLR <visit root> <left subtree> <right subtree>
2. Inorder – LVR <left subtree> <visit root> <right subtree>
3. Postorder – LRV <left subtree> <right subtree> <visit root>
PREORDER TRAVERSAL
In this traversal method, the root node
is visited first, then the left subtree
and finally the right subtree.
Algorithm:
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left
subtree.
Step 3 − Recursively traverse right
subtree.
PREORDER TRAVERSAL
O/P:
A→B→D→E→C→F→
G
PREORDER TRAVERSAL
void preorder(struct
Code snippet node
*root)
{
if(root==null)
return;
printf(“%c”,root ->
data);
preorder(root ->
left);
preorder(root ->
O/P:
right); {30 , 20 , 15 , 5 , 18 , 25 , 40 , 35 , 50
} , 45 , 60}
PREORDER TRAVERSAL
O/P:
ABDCEGFHI
INORDER TRAVERSAL
In this traversal method, the left
subtree is visited first, then the root
and later the right sub-tree.
Algorithm:
Until all nodes are traversed −
Step 1 − Recursively traverse left
subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right
subtree.
INORDER TRAVERSAL
O/P:
D→B→E→A→F→C→G
INORDER TRAVERSAL
void inorder(struct
Code snippet node
*root)
{
if(root==null)
return;
inorder(root ->
left);
printf(“%c”,root ->
data);
O/P:
inorder(root ->
{5 , 15 , 18 , 20 , 25 , 30 , 35 , 40 , 45 , 50 ,
right);
60}
}
INORDER TRAVERSAL
O/P:
BDAGECHFI
POSTORDER 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:
Until all nodes are traversed −
Step 1 − Recursively traverse left
subtree.
Step 2 − Recursively traverse right
subtree.
Step 3 − Visit root node.
POSTORDER TRAVERSAL
O/P:
D→E→B→F→G→C→A
POSTORDER TRAVERSAL
void postorder(struct
Code snippet node
*root)
{
if(root==null)
return;
postorder(root ->
left);
postorder(root ->
right);
O/P:
printf(“%c”,root ->
{5 , 18 , 15 , 25 , 20 , 35 , 45 , 60 , 50 , 40 ,
data);
30}
}
POSTORDER TRAVERSAL
O/P:
DBGEHIFCA
EXAMPLE
Preorder Traversal-
Inorder Traversal-
Postorder Traversal-
Inorder Traversal-
24, 32, 40, 52, 58, 62, 69
Postorder Traversal-
32, 24, 40, 58, 69, 62, 52
CONSTRUCTING BINARY
TREE FROM TRAVERSAL
RESULTS
oWe can construct a binary tree if we are given atleast two traversal
results:
oIn pre-order, root is always displayed first
oIn post-order, root is always displayed last
oIn in-order, root is always displayed between left & right subtree
oApplying this logic recursively we can form a tree
EXAMPLE
In-oder: DBEAFCG
Pre-order: ABDECFG
EXAMPLE
In-oder: DBHEIAFJCG
Post-order: DHIEBJFGCA