0% found this document useful (0 votes)
46 views67 pages

Unit III Binary Tree, Representation and Traversals: 8/28/16 1 Department of Computer Science and Engineering

This document discusses binary trees and their representation. It defines a binary tree as a tree where each node has at most two children. There are different types of binary trees including left-skewed, right-skewed, strictly binary, complete binary, and extended binary trees. Binary trees can be represented sequentially using arrays or linked lists. The sequential representation stores nodes in an array while the linked representation uses pointers. Traversal methods like preorder, inorder and postorder are used to visit all nodes of a binary tree.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views67 pages

Unit III Binary Tree, Representation and Traversals: 8/28/16 1 Department of Computer Science and Engineering

This document discusses binary trees and their representation. It defines a binary tree as a tree where each node has at most two children. There are different types of binary trees including left-skewed, right-skewed, strictly binary, complete binary, and extended binary trees. Binary trees can be represented sequentially using arrays or linked lists. The sequential representation stores nodes in an array while the linked representation uses pointers. Traversal methods like preorder, inorder and postorder are used to visit all nodes of a binary tree.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 67

Unit III

Binary Tree, Representation and traversals

8/28/16

Department of Computer science


and engineering

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

Department of Computer science


and engineering

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

Department of Computer science


and engineering

Figure 3-8 Example of binary tree

8/28/16

Department of Computer science


and engineering

3.Types of binary tree

There are different types of binary trees and they are...


Left skewed binary tree - It has only left child node.
Right skewed binary tree - It has only right child node.
Strictly binary tree
Complete binary tree
Extended binary tree

8/28/16

Department of Computer science


and engineering

(Continued)

1. Left Skewed Binary Tree

8/28/16

2. Right Skewed Binary Tree

Department of Computer science


and engineering

(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

Department of Computer science


and engineering

Figure 3-9 Example of strictly binary tree

Strictly binary tree data structure is used to represent mathematical


expressions.

8/28/16

Department of Computer science


and engineering

Figure 3-10 Example of strictly binary tree represent in mathematical expression

8/28/16

Department of Computer science


and engineering

(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

Department of Computer science


and engineering

10

Figure 3-10 Example of complete binary tree

8/28/16

Department of Computer science


and engineering

11

(Continued)

5. Extended Binary Tree


A binary tree can be converted into Full Binary tree by adding dummy
nodes to existing nodes wherever required.
The full binary tree obtained by adding dummy nodes to a binary tree is
called as Extended Binary Tree.
In the below figure 3-11, a normal binary tree is converted into full binary
tree by adding dummy nodes (In pink color).

8/28/16

Department of Computer science


and engineering

12

Figure 3-11 Example of extended binary tree

8/28/16

Department of Computer science


and engineering

13

4. Binary Tree Representation

A binary tree data structure is represented using two methods. Those


methods are as follows...
Sequential Representation (using arrays)
Linked List Representation (using pointers

8/28/16

Department of Computer science


and engineering

14

Figure 3-12 Example of binary tree

8/28/16

Department of Computer science


and engineering

15

4.1 Sequential Representation


In sequential representation of binary tree, we use a one dimensional array
(1-D Array) to represent a binary tree.
To represent a binary tree of depth 'n' using sequential representation, we
need one dimensional array with a maximum size of 2n+1 - 1.
Once the size of the array has been determined the following method is
used to represent the tree.
1. An array can be used to store the node of binary tree.
2. Store the root in 1st location of the array.
3. If a node is in location n of the array store its left child at location 2n
and its right child at (2n+1).
Arrays are start at position 0; therefore instead of numbering the trees
nodes from 1 to n, we number them from 0 to n-1.
The two child of a node at position P are in positions 2P+1 and 2P+2.
8/28/16

Department of Computer science


and engineering

16

(Continued)

Consider the above figure 3-12, example of binary tree and it is


represented as follows...
N=3 so 2N+1 -1= 24-1 = 16-1 = 15 therefore 0 to 14 arrays needed for the
above example.

12

13

0
14

D F

G H I

10

11

Figure 3-13 Example of one dimensional representation of binary tree

8/28/16

Department of Computer science


and engineering

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

Department of Computer science


and engineering

18

4.2 Linked List Representation

Linked lists most commonly represent binary trees.


We use doubly linked list to represent a binary tree.
In a doubly linked list, every node consists of three fields.
The first field for storing left child address, the second field for
storing actual data and the third field for storing right child address.
In this linked list representation; a node has the following structure
shown in the figure 3-14 given below.

8/28/16

Department of Computer science


and engineering

19

Figure 3-14 Format of linked list representation of binary tree

8/28/16

Department of Computer science


and engineering

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

Department of Computer science


and engineering

21

Figure 3-15 Example of linked list representation of binary tree

8/28/16

Department of Computer science


and engineering

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

Department of Computer science


and engineering

23

Binary tree and its representation

8/28/16

Department of Computer science


and engineering

24

5. Binary Tree methods


1. info(p) It returns the content of nd. Where p is a pointer to a node and nd is a
node of binary tree.
2. left(p) It returns pointers to the left son of nd.
3. right(p) - It returns pointers to the right son of nd.
4. father(p) - It returns pointers to the father of nd.
5. brother(p) - It returns pointers to the brother of nd.
6. isLeft(p) It return the value true if nd is the a left son.
7. isRight(p) It return the value true if nd is the a right son.
8. makeTree(x) It creates a new binary tree consisting of a single node with
information field x and returns a pointer to that node.
9. setLeft(p,x) It accepts a pointer p to a binary tree node with no left son. It
creates a new a left son of node(p) with information field x.
10. setRight(p,x) It accepts a pointer p to a binary tree node with no right son. It
creates a new a right son of node(p) with information field x.
8/28/16

Department of Computer science


and engineering

25

6.Binary Tree Traversals


When we wanted to display a binary tree, we need to follow some order in
which all the nodes of that binary tree must be displayed.
In any binary tree displaying order of nodes depends on the traversal method.
Displaying (or) visiting order of nodes in a binary tree is called as Binary Tree
Traversal.
A binary tree traversal requires that each node of the tree be processed once and
only once in a predetermined sequence. Traversing a binary tree means moving
through all nodes.
Task performed while traversing binary tree are,

Visiting a node (denote by V)


Traversing left child (denote by L)
Traversing right child (denote by R)

Depending upon these tasks 6 possible combinations of traversals is possible.


They are all VLR, LVR, VRL, RVL, RLV and LRV.
8/28/16

Department of Computer science


and engineering

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

Department of Computer science


and engineering

27

Figure 3-15 Example of binary tree

8/28/16

Department of Computer science


and engineering

28

6.1 In - Order Traversal ( LVR)


In In-Order traversal, the root node is visited between left child and right
child.
In this traversal, the left child node is visited first, then the root node is
visited and later we go for visiting right child node.
This In-Order traversal is applicable for every root node of all subtrees
in the tree. This is performed recursively for all nodes in the tree.
Then visit the root node of T and then traverse the right sub-tree.
It follows the general strategy of Left-Root-Right. In this traversal, if T
is not empty, we first traverse (In-Order) the left sub-tree;
Steps
1.Traverse left sub tree
2. Visit root node
3. Traverse right sub tree
8/28/16

Department of Computer science


and engineering

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

Department of Computer science


and engineering

30

(Continued)

8/28/16

Department of Computer science


and engineering

31

(Continued)

8/28/16

Department of Computer science


and engineering

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

Department of Computer science


and engineering

33

6.2 Pre - Order Traversal ( VLR)

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

Department of Computer science


and engineering

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

Department of Computer science


and engineering

35

(Continued)

8/28/16

Department of Computer science


and engineering

36

(Continued)

8/28/16

Department of Computer science


and engineering

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

Department of Computer science


and engineering

38

6.3 Post - Order Traversal ( LRV)


In Post-Order traversal, the root node is visited after left child and
right child.
In this traversal, left child node is visited first, then its right child
and then its root node.
This is recursively performed until the right most nodes are visited.
Here in the figure 3-15 we have visited in the order of I - J - D - F B - K - G - H - C - A using Post-Order Traversal.
Steps
1.Traverse left sub tree
2. Traverse right sub tree
3. Visit root node
8/28/16

Department of Computer science


and engineering

39

(Continued)

8/28/16

Department of Computer science


and engineering

40

(Continued)

8/28/16

Department of Computer science


and engineering

41

(Continued)

That means here we have visited in the order of 3-6-5-9-12-10-7 using


Post-Order Traversal.

8/28/16

Department of Computer science


and engineering

42

8/28/16

Department of Computer science


and engineering

43

Construction of binary tree and its traversals

8/28/16

Department of Computer science


and engineering

44

Questions
1. Which of the following is a true about Binary Trees

A - Every binary tree is either complete or full.


B- Every complete binary tree is also a full binary tree.
C- Every full binary tree is also a complete binary tree.
D- No binary tree is both complete and full.
E - None of the above
2. The height of a binary tree is the maximum number of edges in any root to leaf path.
A.
B.
C.
D.

The maximum number of nodes in a binary tree of height h is:


2^h -1
2^(h-1) 1
2^(h+1) -1
2*(h+1)

8/28/16

Department of Computer science


and engineering

45

3. Postorder traversal of a given binary search tree, T produces the following


sequence of keys 10, 9, 23, 22, 27, 25, 15, 50, 95, 60, 40, 29 Which one of
the following sequences of keys can be the result of an in-order traversal of
the tree T?
A. 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95
B. 9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29
C. 29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95
D. 95, 50, 60, 40, 27, 23, 22, 25, 10, 9, 15, 29
4. A binary tree T has 20 leaves. The number of nodes in T having two
children is
A. 18
B. 19
C. 17
D. Any number between 10 and 20

8/28/16

Department of Computer science


and engineering

46

5. In a binary tree, the number of internal nodes of degree 1 is 5, and the


number of internal nodes of degree 2 is 10. The number of leaf nodes I
(A) 10
(B) 11
(C) 12
(D) 15

8/28/16

Department of Computer science


and engineering

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

Department of Computer science


and engineering

48

6. Consider the binary tree given below and give the In-order, Pre-order and
Post-order

8/28/16

Department of Computer science


and engineering

49

7. Given elements as 12,3,7,9,43,33


Construct the binary tree
Answer In-order, Pre-order and Post-order.

8/28/16

Department of Computer science


and engineering

50

Gate Questions
1.

A binary tree T has n leaf nodes. The number of nodes of degree 2 in T is


(a) log2n
(b) n-1
(c) n
(d) 2 n

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

Department of Computer science


and engineering

51

8/28/16

Department of Computer science


and engineering

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

Department of Computer science


and engineering

53

8/28/16

Department of Computer science


and engineering

54

4. A binary search tree is generated by inserting in order the following


integers:
50,
15,
62,
5,
20,
58,
91,
3,
8,
37,
60,
24
The number of nodes in the left subtree and right subtree of the root
respectively
is
(a) (4, 7)
(b) (7, 4)
(c) (8, 3)
(d) (3, 8)
5. A binary search tree contains the value 1, 2, 3, 4, 5, 6, 7, 8. The tree is
traversed in pre-order and the values are printed out. Which of the
following
sequences
is
a
valid
output?
(a)
5
3
1
2
4
7
8
6
(b)
5
3
1
2
6
4
8
7
(c)
5
3
2
4
1
6
7
8
(d)
5
3
1
2
4
7
6
8

8/28/16

Department of Computer science


and engineering

55

6. Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into


an initially empty binary search tree. The binary search tree uses the usual
ordering on natural numbers. What is the in-order traversal sequence of the
resultant tree?
a) 7 5 1 0 3 2 4 6 8 9
b) 0 2 4 3 1 6 5 9 8 7
c) 0 1 2 3 4 5 6 7 8 9
d) 9 8 6 4 2 3 0 1 5 7
7. The following numbers are inserted into an empty binary search tree in the
given order: 10, 1, 3, 5, 15, 12, 16. What is the height of the binary search
tree (the height is the maximum distance of a leaf node from the root)?
(a)
2
(b)
3
(c)
4
(d)
6

8/28/16

Department of Computer science


and engineering

56

8. The maximum number of binary trees that can be formed with three unlabeled nodes is:

(a) 1 (b) 5 (c) 4 (d) 3


9. The number of leaf nodes in a rooted tree of n nodes, with each node having 0 or 3
children is:
(b) n/2 (b) (n-1)/3 (c) (n-1)/2 (d) (2n+1)/3
10. Postorder traversal of a given binary search tree, T produces the following sequence of
keys 10, 9, 23, 22, 27, 25, 15, 50, 95, 60, 40, 29
Which one of the following sequences of keys can be the result of an in-order traversal of
the tree T?
(a) 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95
(b) 9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29
(c) 29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95
(d) 95, 50, 60, 40, 27, 23, 22, 25, 10, 9, 15, 29

8/28/16

Department of Computer science


and engineering

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

Department of Computer science


and engineering

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

Department of Computer science


and engineering

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

Department of Computer science


and engineering

60

4.Ans: option (b)


Explanation:
Difference between Binary Tree and Binary Search Tree.
Binary Tree - Tree in which each node has at most two child nodes.
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. Binary search tree is
used for efficient searching. The binary search tree for the above problem
is given below.

8/28/16

Department of Computer science


and engineering

61

8/28/16

Department of Computer science


and engineering

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

Department of Computer science


and engineering

63

8/28/16

Department of Computer science


and engineering

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

Department of Computer science


and engineering

65

8. Ans: option (b)


Explanation:
The maximum number of binary trees that can be formed with 'n' unlabeled
nodes is C(2n,n) / n+1.
9. Ans: option (d)
Explanation:
The number of leaf nodes for an n-ary tree where each node has n children or
no children is L = (n-1)I + 1, where L is the number of leaf nodes and I is
the number of internal nodes. See question number 7, for details.
Therefore for the above problem, L = (3 -1)I + 1
The total number of nodes is sum of leaf nodes and internal nodes (i.e. L + I )
Therefore, number of internal nodes, I = n L
L = 2 ( n- L ) + 1
L = 2n - 2L + 1
3L = 2n + 1
Therefore L = (2n +1)/3
8/28/16

Department of Computer science


and engineering

66

10. Ans: option (a)


Explanation:
As we have already seen in question number 11, In-order traversal of a
BST gives elements in increasing order. Hence option (a)

8/28/16

Department of Computer science


and engineering

67

You might also like