Unit III Binary Tree, Representation and Traversals: 8/28/16 1 Department of Computer Science and Engineering
Unit III Binary Tree, Representation and Traversals: 8/28/16 1 Department of Computer Science and Engineering
8/28/16
1. Definition
A binary tree is a tree, which is, either empty or consists of a root
node and two disjoint binary trees called the left sub-tree and right
sub-tree.
(Or)
A tree in which every node can have a maximum of two children is
called as Binary Tree.
(Or)
A binary tree is a tree in which no node can have more than two
subtrees.
In other words, a node can have zero, one, or two subtrees. These
subtrees are designated as the left subtree and right subtree.
Note: Each subtree is itself a binary tree.
8/28/16
2. Introduction
In a normal tree, every node can have any number of children.
Binary tree is a special type of tree data structure in which every
node can have a maximum of 2 children.
One is known as left child and the other is known as right child.
In a binary tree, every node can have either 0 children or 1 child or 2
children but not more than 2 children.
In a binary tree, no node can have more than two children.
So every binary tree is a tree, not every tree is a binary tree.
Note: A binary tree node cannot have more than two subtrees.
Figure 3-8 shows the example of binary tree.
8/28/16
8/28/16
8/28/16
(Continued)
8/28/16
(Continued)
3. Strictly Binary Tree
In a binary tree, every node can have a maximum of two children.
But in strictly binary tree, every node should have exactly two
children or none. That means every internal node must have exactly
two children.
A strictly binary tree can be defined as follows...
A binary tree in which every node has either two or zero number of
children is called Strictly Binary Tree.
Strictly binary tree is also called as Full Binary Tree or Proper
Binary Tree or 2-Tree is shown in the figure 3-9.
8/28/16
8/28/16
8/28/16
(Continued)
4. Complete Binary Tree
A complete binary tree is a binary tree in which all internal nodes have
degree and all leaves are at the same level.
(Or)
A binary tree in which every internal node has exactly two children and all
leaf nodes are at same level is called Complete Binary Tree.
In a binary tree, every node can have a maximum of two children. But in
strictly binary tree, every node should have exactly two children or none and
in complete binary tree all the nodes must have exactly two children and at
every level of complete binary tree there must be 2 level number of nodes.
For example at level 2 there must be 2 2 = 4 nodes and at level 3 there must
be 23 = 8 nodes.
Complete binary tree is also called as Perfect Binary Tree.
8/28/16
10
8/28/16
11
(Continued)
8/28/16
12
8/28/16
13
8/28/16
14
8/28/16
15
16
(Continued)
12
13
0
14
D F
G H I
10
11
8/28/16
17
(Continued)
Advantages
Given a child node, its parent node can be determined immediately. If
a child node is at location N in the array, then its parent is at location
N/2 i.e. simplicity.
It can be implemented easily in languages in which only static
memory allocation is directly available i.e. ease of implementation.
Disadvantages
Insertion or deletion of a node causes considerable data movement up
and down the array, using an excessive amount of processing time.
Wastage of memory due to partially filled trees i.e. wastage of space
and lot of unutilized space.
8/28/16
18
8/28/16
19
8/28/16
20
(Continued)
The above example of figure 3-12 binary tree represented using Linked list
representation is shown in the figure 3-15 as follows...
8/28/16
21
8/28/16
22
(Continued)
Advantages
Insert/ delete have no data movement but re arrangement of pointers.
A particular node can be placed at any location in the memory.
It is best for any type of trees.
It is flexible because the system take care of allocating and freeing of nodes.
Disadvantages
Difficult to determine the parent node.
Only spaces are wasted for storing null pointers.
It is difficult to understand.
Additional memory is needed for storing pointers.
Accessing
a
particular
node
is
not
easy.
8/28/16
23
8/28/16
24
25
26
(Continued)
Depending on the factor, 3 possible combinations are used.
There are three types of binary tree traversals.
In - Order Traversal (LVR)
Pre - Order Traversal (VLR)
Post - Order Traversal (LRV)
Consider the binary tree in figure 3-16 as given below as follows
8/28/16
27
8/28/16
28
29
(Continued)
In the above example figure 3-15 of binary tree, first we try to visit left child of root node 'A', but
A's left child is a root node for left subtree.
So we try to visit its (B's) left child 'D' and again D is a root for subtree with nodes D, I and J. And
finally we try to visit (Ds) left child 'I' and it is the left most child.
So first we visit 'I' then go for its root node 'D' and later we visit D's right child 'J'. With this we
have completed the left part of node B.
Then visit 'B' and next B's right child 'F' is visited. With this we have completed left part of node A.
Then visit root node 'A'.
With this we have completed left and root parts of node A. Then we go for right part of the node A.
In right of A again there is a subtree with root C. So go for left child of C and again it is a subtree
with root G. But G does not have left part so we visit 'G' and then visit G's right child K.
With this we have completed the left part of node C. Then visit root node 'C' and next visit C's right
child 'H' which is the right most child in the tree so we stop the process.
That means here we have visited in the order of I - D - J - B - F - A - G - K - C - H using In-Order
Traversal.
8/28/16
30
(Continued)
8/28/16
31
(Continued)
8/28/16
32
(Continued)
That means here we have visited in the order of 3-5-6-7-9-10-12
using In-Order Traversal.
8/28/16
33
In Pre-Order traversal, the root node is visited before left child and right
child nodes.
In this traversal, the root node is visited first, then its left child and later its
right child.
This pre-order traversal is applicable for every root node of all subtrees in
the tree.
Steps
1. Visit root node
2. Traverse left sub tree
3. Traverse right sub tree
8/28/16
34
(Continued)
In the above example figure 3-15 of binary tree, first we visit root node 'A' then
visit its left child 'B' which is a root for D and F.
So we visit B's left child 'D' and again D is a root for I and J.
So we visit D's left child 'I' which is the left most child. So next we go for visiting
D's right child 'J'.
With this we have completed root, left and right parts of node D and root, left parts
of node B. Next visit B's right child 'F'.
With this we have completed root and left parts of node A. So we go for A's right
child 'C' which is a root node for G and H.
After visiting C, we go for its left child 'G' which is a root for node K. So next we
visit left of G, but it does not have left child so we go for G's right child 'K'.
With this we have completed node C's root and left parts. Next visit C's right
child 'H' which is the right most child in the tree. So we stop the process.
That means here we have visited in the order of A-B-D-I-J-F-C-G-K-H using PreOrder Traversal.
8/28/16
35
(Continued)
8/28/16
36
(Continued)
8/28/16
37
(Continued)
That means here we have visited in the order of 7-5-3-6-10-9-12 using PreOrder Traversal.
8/28/16
38
39
(Continued)
8/28/16
40
(Continued)
8/28/16
41
(Continued)
8/28/16
42
8/28/16
43
8/28/16
44
Questions
1. Which of the following is a true about Binary Trees
8/28/16
45
8/28/16
46
8/28/16
47
Questions
1. What is Binary tree?
2. Names of tree traversal technique with examples.
3. Explain difference between complete binary tree ,strictly binary tree and
full binary tree.
4. How many number of binary tree is possible with n nodes?
5. What is the height of full binary tree with n nodes?
8/28/16
48
6. Consider the binary tree given below and give the In-order, Pre-order and
Post-order
8/28/16
49
8/28/16
50
Gate Questions
1.
2.
In the balanced binary tree in the figure given below, how many nodes
will become unbalanced when a node is inserted as a child of the node
g?
8/28/16
51
8/28/16
52
3. Which of the following sequences denotes the post order traversal sequence
of the tree of the above question 2?
(a) f e g c d b a
(b) g c b d a f e
(c) g c d b f e a
(d) f e d g c b a
8/28/16
53
8/28/16
54
8/28/16
55
8/28/16
56
8. The maximum number of binary trees that can be formed with three unlabeled nodes is:
8/28/16
57
1. Ans: option(b)
Explanation:
A binary tree is a tree data structure in which each node has at most two
child
nodes.
The number of subtrees of a node is called the degree of the node. In a
binary
tree,
all
nodes
have
degree
0,
1,
or
2.
The degree of a tree is the maximum degree of a node in the tree. A
binary tree is of degree 2.
8/28/16
58
2. Ans: option(b)
Explanation:
A binary tree T is balanced if:
1) Left subtree of T is balanced
2) Right subtree of T is balanced
3) The difference between heights of left subtree and right subtree is not more than 1.
After inserting a node as a child of g, find the balance factors of each node.
Balance Factor of node a = height of left subtree - height of right subtree = 2
Similarly the balance factors of the nodes are b = 2, c = 2, d = 0, g = 1, e = 1 and f = 0.
Nodes having balance factor 1, 0 and -1 are balanced nodes. All other nodes are
unbalanced nodes.
Note: Whenever a node gets unbalanced, all the nodes on the path from first unbalanced
node to till the root also gets unbalanced. In this problem first unbalanced node was c.
So all the nodes from c to root a got unbalanced (i.e. c,b and a.)
8/28/16
59
3. Ans: option(c)
Explanation:
As shown in the figure above start from the node a. Each node has
been numbered as 1,2,3. When u are finding post order start from the root
and consider the node only when u reach point 3. As shown above the red
line indicates the path through which the tree is traversed. The first node
that reaches point 3 on traversing is node g, then c, then d, then b, then f,
then e and then a. Hence we are considering point 3 for post order. If you
consider point 2 then u will get inorder, and if you consider point 1 u will
get preorder.
8/28/16
60
8/28/16
61
8/28/16
62
5.Ans:option(d)
Explanation:
Considering the pre-order traversal, try to construct the Binary Search Tree
(BST) for each option. We can construct the BST for option (d) only. Other
options will violate the BST conditions.
6. Ans: option (c)
Explanation:
Binary search tree - A binary tree in which the left child contains only
nodes with values less than the parent node, and the right child contains
only nodes with values greater than or equal to the parent.
8/28/16
63
8/28/16
64
7.Ans:option
(b)
Explanation:
The depth (or height) of a
tree is the length of the
path from the root to the
deepest node in the tree. A
rooted tree with only one
node (the root) has a depth
of zero.
8/28/16
65
66
8/28/16
67