0% found this document useful (0 votes)
18 views

CDS S2

The document provides solutions to 19 questions about data structures and binary trees. Some key points covered include: - The maximum height of an AVL tree with 7 nodes is 3. - Preorder and inorder traversals are sufficient to build a binary tree, but preorder and postorder are not. - Level order traversal resembles breadth first search as it uses a queue data structure. - The worst case complexity for operations on a general binary search tree is O(n) for all operations.

Uploaded by

Jaydeep S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

CDS S2

The document provides solutions to 19 questions about data structures and binary trees. Some key points covered include: - The maximum height of an AVL tree with 7 nodes is 3. - Preorder and inorder traversals are sufficient to build a binary tree, but preorder and postorder are not. - Level order traversal resembles breadth first search as it uses a queue data structure. - The worst case complexity for operations on a general binary search tree is O(n) for all operations.

Uploaded by

Jaydeep S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PROGRAMMING WITH DATA STRUCTURES

(SET: 2)

Solutions
1. What is the maximum height of any AVL tree with 7 nodes? Assume that the height of a tree
with single node is 0.

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

Solution: Option (b)

One AVL tree of height 3 is:

2. In a binary tree with n nodes every node has an odd no. of descendants. Every node is
considered to be its own descendant. What is the no. of nodes in the tree that has exactly one
child?

(a) 0 (b) 1
(c) (n-1)/ 2 (d) n-1

Solution: Option (a)

Explanation:

Such a node is not possible.

3. 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) None of these

Solution: Option (d)


1
Explanation:

A full binary tree is a tree in which every node other than the leaves has 2 children. A complete
binary tree is a binary tree in which every level, except possibly that last, is completely filled,
and all nodes are as far left as possible.

(a) is incorrect. For eg—the following binary tree is neither complete nor full:

(b) is incorrect. The following binary tree is complete but not full:

(c) is incorrect. Following binary tree is full, but not complete:

4. What are the main applications of a tree data structure?

(a) Manipulate hierarchical data (b) Make information easy to search


(c) Router algorithms (d) All of these

Solution: Option (d)

5. In a complete K-ary tree, every internal node has exactly K children or no child. The no. of
leaves in such a tree with n internal nodes is:

(a) nK (b) (n-1) K+1


(c) n(K-1) +1 (d) n(K-1)

Solution: Option (c)


2
Explanation :

Let N( n ) be the number of leaf nodes in K ary tree , having n iinternal nodes. The recurrence
equation is N(n) =N( n-1) + K-1 , now N(1) = K.

Solving the recurrence equation we get N( n ) = n( K-1) +1 .

6. The no. of leaf nodes in a rooted tree of n nodes, with each node having 0 or 3 children is:

(a) n/ 2 (b) (n-1)/ 3


(c) (n-1)/ 2 (d) (2n+1)/ 3

Solution: Option (d)

Explanation:

The total number of nodes given is n. Now the total number of nodes is divided into leaf+
internal nodes. Let c be the number of internal nodes, therefore we have n-c leaf nodes.Now
using the result we obtained in previous question we obtain that number of leaf nodes with c
internal nodes in a 3 ary tree (K=3) , is c(3-1) + 1= 2c+1 . Now, number of leaf nodes +number
of internal nodes= n , therefore

c+ 2c+1= n

implies c= (n-1) /3

From this we can find the number of leaf nodes.

7. A weight balanced tree is a binary tree in which for each node, the no. of nodes in the left sub
tree is atleast half and at most twice the no. of nodes in the right sub tree. The maximum possible
height of such a tree with n nodes is best described by which of the following?

(a) log2 n (b) log4/3 n


(c) log3 n (d) log3/2 n

Solution: Option (d)

Explanation:

The value can be approximated as the recurrence relation:


H(n)= H (2n/3) +1

8. A schema for storing a binary tree in an array X is as follows. Indexing of X starts at 1 instead
3
of 0. The root is stored at X[1]. For a node stored at X[i] the left childs if any is stored at X[2i]
and right child at X[2i + 1]. To be able to store any binary tree of n vertices, the minimum size of
X should be:

(a) log2 n (b) n


(c) 2n+1 (d) 2n-1

Solution: Option (d)

Explanation:

Take for eg. a binary tree is:

9. Postorder traversal of a given BST, produces the order:

10, 9, 23, 22, 27, 25, 15, 50, 95, 60, 40, 29

Which of the following is inorder traversal?

(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

Solution: Option (a)

Explanation:

Inorder traversal produces sorted order of elements of a BST. The inorder traversal is

9 10 15 22 23 25 27 29 40 50 60 95.

Using the postorder and inorder traversal we can get a unique BST .

4
10. Consider a node X in a Binary tree. Given that X has 2 children. Y be a inorder successor of
X. Which of the following is true about Y?

(a) Y has no right child (b) Y has no left child


(c) Y has both children (d) None of the above

Solution: Option (b)

Explanation:

Since X has both children, Y must be leftmost node in the right subtree of X, hence Y cannot
have a left child as that would contradict the question statement.

11. The height of a tree is the length of the longest root-to-leaf path in it. The max and min no. of
nodes in a binary tree of height 5 are:

(a) 63 and 6 (b) 64 and 5


(c) 32 and 6 (d) 31 and 5

Solution: Option (a)

Explanation:

Max. is→ 2(n+1) -1


Min. is→ n+1
Where h= height of the tree

12. A binary tree T has 20 leaves. The no. of nodes in T having 2 children is:

(a) 18 (b) 19
(c) 17 (d) any number between 10 and 20

Solution: Option (b)

Explanation:

The no. of nodes with 2 children is always one less than no. of leaves.

13. Consider a complete binary tree where the left and right subtrees of the root are max- heaps.
5
The lower bound of the no. of operations to convert the tree to a heap is:

(a) Ω (log n) (b) Ω (n)


(c) Ω (n log n) (d) Ω (n2)

Solution: Option (a)

Explanation:

It is simple to call max-heapify which recurses atmost through height of the heap.

14. The inorder and preorder traversal of a binary tree is—d b e a f c g and a b d e c f g
respectively. The postorder traversal of the binary tree is:

(a) d e b f g c a (b) e d b g f c a
(c) e d b f g c a (d) d e f g b c a

Solution: Option (a)

Explanation :

From the preo order and inorder traversal we can uniquely obtain a BST.

15. Which of the following pair of traversals is not sufficient to build a binary tree from the
given traversals?

(a) Preorder and Inorder (b) Preorder and Postorder


(c) Inorder and Postorder (d) None of the above

Solution: Option (b)

16. Which traversal of tree resembles the breadth first search of the graph?

(a) Preorder (b) Inorder


(c) Postorder (d) Levelorder

Solution: Option (d)

6
17. Which of the following tree traversal uses a queue data structure?

(a) Preorder (b) Inorder


(c) Postorder (d) Levelorder

Solution: Option (d)

Explanation:

Level order traversal is the implementation of breadth first search. And we use a queue for
breadth first search.

18. What is the worst case complexity for search, insert and delete operations in a general binary
search tree?

(a) 0 (n) for all


(b) 0 (log n) for all
(c) 0 (log n) for search and insert, 0(n) for delete
(d) 0 (log n) for search and 0 (n) for insert and delete

Solution: Option (a)

19. 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 BST (the height is the max. distance of a leaf node from the root)?

(a) 2 (b) 3
(c) 4 (d) 6

Solution: Option (b)

Explanation:

You might also like