2. Introduction
● A data structure is a way of organizing, storing, and managing
data so that it can be used efficiently.
● Data structures are an important part of several computer
algorithms and programs.
● They help programmers in designing efficient software.
● Data structures are used in all domains of computer science such
as Artificial Intelligence and Operating systems.
3. Tree node structure
● C Structure to represent a node of a Tree:
struct TreeNode {
int info;
TreeNode * left;
TreeNode * right;
};
4. Binary Tree
● Binary Tree is a special form of a tree
● A tree in which each branch node has the same out- degree
● Binary tree is important and frequently used in various applications of
computer science
5. Complete Tree
● A tree with n nodes and of depth k is complete iff its nodes correspond to the
nodes which are numbered one to n in the full tree of depth k
● A binary tree of height, h, is complete iff it is empty or
● its left subtree is complete of height h – 1 and its right subtree is completely
full f height h – 2 or – its left subtree is completely full of height h-1 and its
right subtree is complete of height h – 1
● A binary tree is completely full if it is of height, h and has (2h+1 – 1) nodes
6. Full Binary Tree
● A binary tree is a full binary tree, if it contains maximum possible number
of nodes in all levels
● In a full binary tree each node has two children or no child at all
● Total number of nodes in full binary tree of height h is 2h+1 – 1
considering root at level 0.
● It can be calculated as by adding number of nodes of each level 2 0 + 2 1 +
2 2 + ………..+ 2 h = 2 h+1 – 1
7. ● Binary tree is a full binary tree, if it contains maximum possible
number of nodes in all levels
● In a full binary tree each node has two children or no child at all
● Total number of nodes in full binary tree of height h is 2h+1 –
1 considering root at level 0.
● It can be calculated as by adding number of nodes of each level 2
0 + 2 1 + 2 2 + ………..+ 2 h = 2 h+1 – 1
7
Trees 1/7/2021
Full Binary Tree
8. ● A binary tree is said to be a complete binary tree, if all its level,
except the last level, have maximum number of possible nodes, and
all the nodes of the last level appear as far left as possible
● In a complete binary tree all leaf nodes are at last and second last
level and levels are filled from left to right
Complete Binary Tree
Complete Binary Tree
8
Trees 1/7/2021
9. ● Every non-terminal node in a binary tree consists of non- empty
left subtree & right subtree, then such a tree is called Strictly
Binary Tree
Strictly Binary Tree
9
Trees 1/7/2021
10. ● A binary tree T consists of each node has 0 or 2 children is called
extended binary tree
● Node with 2 children are called internal nodes and nodes with 0
children are called external nodes
● Trees can be converted into extended trees by adding a node
10
Trees 1/7/2021
Binary Tree
11. ❖ Definition : A Binary Tree is either :
an empty tree; or
It consists of a node, called root and two children, left
and right, each of which are themselves binary trees
Binary Tree
11
Trees 1/7/2021
13. Some Properties of Binary Trees
● The height of binary trees can be mathematically predicted
● Given that we need to store N nodes in a binary tree, the maximum height
is
13
A tree with a maximum height is rare. It occurs when all of the nodes in the entire
tree have only one successor.
14. Some Properties of Binary Trees
● The minimum height of a binary tree is determined as follows:
14
For instance, if there are three nodes to be stored in the binary tree (N=3) then
Hmin
=2.
15. Some Properties of Binary Trees
● Given a height of the binary tree, H, the minimum number of nodes in
the tree is given as follows:
15
16. Some Properties of Binary Trees
● The formula for the maximum number of nodes is derived from the
fact that each node can have only two descendents. Given a height of
the binary tree, H, the maximum number of nodes in the tree is given
as follows:
16
17. Some Properties of Binary Trees
● The children of any node in a tree can be accessed by following only one
branch path, the one that leads to the desired node.
● The nodes at level 1, which are children of the root, can be accessed by
following only one branch; the nodes of level 2 of a tree can be accessed by
following only two branches from the root, etc.
● The balance factor of a binary tree is the difference in height between its left
and right subtrees:
17
18. 18
B=0 B=0 B=1 B=-1
B=0 B=1
B=-2 B=2
Balance of the tree
19. Binary trees are trees where the maximum degree of any node is two
1/7/2021
● Any general tree can be represented as a binary tree
using the ALL nodes general tree will be nodes of binary tree.
● The root of the Binary Tree is the Root of the Generic Tree.
● The left child of a node in the Generic Tree is the Left child of that
node in the Binary Tree.
● The right sibling of any node in the Generic Tree is the Right child
of that node in the Binary Tree.
Conversion Of A General Tree To Binary Tree
20. Binary trees are trees where the maximum degree of any node is two
1/7/2021
Conversion Of A General Tree To Binary Tree
21. Binary trees are trees where the maximum degree of any node is two
1/7/2021
Conversion Of A General Tree To Binary Tree
Below are the steps for the conversion of Generic Tree to Binary Tree:
1. As per the rules mentioned above, the root node of general tree A is the root
node of the binary tree.
2. Now the leftmost child node of the root node in the general tree is B and it is
the leftmost child node of the binary tree.
3. Now as B has E as its leftmost child node, so it is its leftmost child node in
the binary tree whereas it has C as its rightmost sibling node so it is its right
child node in the binary tree.
4. Now C has F as its leftmost child node and D as its rightmost sibling node,
so they are its left and right child node in the binary tree respectively.
22. Binary trees are trees where the maximum degree of any node is two
1/7/2021
Conversion Of A General Tree To Binary Tree
5. Now D has I as its leftmost child node which is its left child node in the binary tree
but doesn’t have any rightmost sibling node, so doesn’t have any right child in the
binary tree.
6. Now for I, J is its rightmost sibling node and so it is its right child node in the
binary tree.
7. Similarly, for J, K is its leftmost child node and thus it is its left child node in the
binary tree.
8. Now for C, F is its leftmost child node, which has G as its rightmost sibling node,
which has H as its just right sibling node and thus they form their left, right, and right
child node respectively.
23. Binary trees are trees where the maximum degree of any node is two
1/7/2021
Conversion of A General Tree To Binary Tree