You are on page 1of 7

Roll No

ST.JOSEPHS COLLEGE OF ENGINEERING


UNIT - II EXAMINATIONS, Oct- 2011
Branch
: M.C.A
Subject Code: MC9214
Time
: 1 hour and 30 min

Subject: Data Structures


Semester: I
Max Marks: 50

PART A
Answer all the questions

( 5 X 2 = 10 )

1) Define internal and external node.


Internal Node:
Structure of a node:
Struct Inode
{
Struct Inode *leftchild;
Int data;
Struct Inode *rightchild;
};
Structure of a node:
Struct Enode
{
Int data;
};
2) Draw expression tree for the expression:
A * B - (C + D) * (P / Q)
*

3) Write the difference between strictly binary tree and complete binary tree
Complete binary tree is a binary tree in which all the levels must be filled from the left to
right. After filling one level then only we can move on to the next level. Full and Complete
binary trees are different. All full binary trees are complete binary trees but not vice versa.
Strictly Binary Tree: Here each will have exactly two child nodes except the leaf nodes
4) What is the maximum number of nodes in a binary tree of depth k?
The max. no. of nodes in a binary tree of depth k is 2k-1.
Proof:
Max. no. of nodes in a binary tree of depth k =
= 20 + 21 + 22 + + 2k-1
= 2k 1
5) What are the ways to represent Binary tree in memory?
1. Array Representation
2. Linked List Representation

PART B

(2 X 16 +1 X 8 = 40)

6 a) Explain the algorithm for the construction of Huffman code with an example.

(16)

Construction of a Huffman tree:


1. Take two symbols which are of least frequency
2. Combine the two symbols to get one symbol(less frequency node as left child, more frequency
node as right child)
3. Repear steps 1 and 2 until we get a single symbol

Construction of a Huffman code:


1. Start at the leaf representing the symbol and the node is initialized to NULL
2. Climb up to the root, each time a left branch is climbed a zero is appended to the
beginning of the code
3. Each time a right branch is climbed a one is appended to the beginning of the code.
(OR)
6 b) i) Prove For any non-empty binary tree T, if n0 is the number of leaf nodes and n2 is the
number of nodes of degree 2, then n0= n2+1
(8)
The strictly binary tree with n leaf nodes contains 2n-1 nodes.
Proof:
No. of nodes = N
In a binary tree N = n0 + n1 +n2
Where n0, n1, n2 are no. of nodes of degree 0 , 1, 2 respectively.
In strictly binary tree, n1 = 0;
Hence, N = n0 + n2 eqn 1
By property III of binary tree,

n0 = n2 +1
n2 = n0 1
From eqn 1,
N = n 0 + n0 1
= 2n0 1
Hence N = 2n -1 ( because it is mentioned that no. of leaf nodes is n. Therefore, n0 = n )
Hence proved.
ii) Explain the various representations of binary tree in memory. Represent the following
binary tree using the above representations.
(8)

*
B

E
H

Representation of Binary tree in memory:


1.Array Representation
2. Linked List Representation
Array Representation:
o The root node is stored at location 0.
o Left child of the node at location i is stored at location 2i+1
o Right child of the node at location i is stored at location 2i+2
If the child is in ith location, its parent will be in (i-1)/2 th location.
Linked List Representation:
Structure of a node:
Struct treenode
{
Struct treenode *leftchild;
Int data;
Struct treenode *rightchild;
};
Draw the array representation and tree.

7 a) Explain the algorithm for the conversion of general tree to binary tree for the given tree.
(16)
A
B

C
E

Ans:
Illustrate with stack:

A
B

C
G

D
E
F

(OR)
7 b) Explain in detail about in order, pre order and post order tree traversal with algorithm
and examples. Find in order, pre order & post order for the following tree.
(16)
A
B

Tree Traversal:
Traversing means visiting each node only once. Tree traversal is a method for
visiting all the nodes in the tree exactly once.
Types of traversal:

In order Traversal
Pre order Traversal
Post order Traversal
In order Traversal: - ( L D R ) (Left Data Right )
(i)
(ii)
(iii)

Traverse the left sub tree in inorder


Visit the root
Traverse the right sub tree in inorder.

Recursive Algorithm for inorder traversal:


Void inorder(treenode *t)
{
If (t!=NULL)
{
Inorder(t->leftchild);
Printf(%d, t->data);
Inorder(t->rightchild);
}
}
Inorder traversal of a expression tree will give the infix expression.
Pre order Traversal: - ( D L R ) ( Data Left Right )
(i)
(ii)
(iii)

Visit the root


Traverse the left sub tree in preorder
Traverse the right sub tree in preorder.

Recursive Algorithm for preorder traversal:


Void preorder(treenode *t)
{
If (t!=NULL)
{
Printf(%d, t->data);
preorder(t->leftchild);
preorder(t->rightchild);
}
}
Preorder traversal of a expression tree will give the prefix expression.
Post order Traversal: - ( L R D ) ( Left Right - Data )
(i)
(ii)
(iii)

Traverse the left sub tree in postorder


Traverse the right sub tree in postorder.
Visit the root

Recursive Algorithm for postorder traversal:


Void postorder(treenode *t)
{
If (t!=NULL)
{
postorder(t->leftchild);
postorder(t->rightchild);
Printf(%d, t->data);
}
}
In order Traversal: DBEACHGI
Pre order Traversal:ABDECGHI
Post order Traversal:DEBHIGCA
8a) Given the In order and pre order, construct the binary tree.
Pre order : R A B D G C E K H I F
In order : D G B A H E I C F K R
Ans:

(8)

R
A

G
(OR)
b) Explain the various operations on a binary tree.
1.
2.
3.
4.
5.
6.
7.

(8)

Left (p):If p is a pointer to a node then left(p) gives the left child of the node
Right(p): If p is a pointer to a node then right(p) gives the right child of the node
Father(p):If p is a pointer to a node then father(p) gives the father of the node
Brother(p): If p is a pointer to a node then brother(p) gives the sibling/brother of the node
Isleft(p):It gives the value true if the node is the left child of some node
Isright(p):It gives the value true if the node is the right child of some node
Maketree(x)
It creates a new binary tree consisting of a single node with information x and returns a pointer to that
node
a. Left child

b. Data
c. Right child

It creates a node fill left child , right child to NULL and returns the address.
8.

Setleft(p,x):This function accepts a pointer p with no left child and it creates a new left child for p
with information x.
9. Setright(p,x):This function accepts a pointer p with no right child and it creates a new right child for p
with information x.

You might also like