0% found this document useful (0 votes)
9 views4 pages

Tree_Practice_Questions_with_Solutions

The document contains a series of practice questions and solutions related to binary trees and AVL trees. It covers algorithms for tree traversals, tree construction, node insertion and deletion, and comparisons between binary search trees and AVL trees. Additionally, it discusses real-life applications of trees and provides examples of expression trees.

Uploaded by

malviyat42
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)
9 views4 pages

Tree_Practice_Questions_with_Solutions

The document contains a series of practice questions and solutions related to binary trees and AVL trees. It covers algorithms for tree traversals, tree construction, node insertion and deletion, and comparisons between binary search trees and AVL trees. Additionally, it discusses real-life applications of trees and provides examples of expression trees.

Uploaded by

malviyat42
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/ 4

Tree & AVL Tree Practice Questions with Solutions

Q1. Write an algorithm or recursive function to perform Inorder traversal of a binary tree. Dry-run it

Ans: Algorithm:

1. Traverse left subtree

2. Visit root

3. Traverse right subtree

Output: 2, 5, 7, 10, 15, 20

Q2. Construct the Binary Tree from Inorder: D B E A F C and Preorder: A B D E C F. Draw the final tr

Ans: Solution: Use preorder to pick root, then split inorder.

Final Tree:

/\

B C

/\ /

D EF

Q3. Write an algorithm for Level-Order Traversal using a queue. State its time and space complexity

Ans: Algorithm:

1. Use queue, start from root

2. Push left and right child

3. Repeat

Time: O(n), Space: O(n)

Q4. Write a recursive function to count the number of leaf nodes in a binary tree.
Tree & AVL Tree Practice Questions with Solutions

Ans: If node is null -> return 0

If left and right are null -> return 1

Else return left + right leaf count

Q5. Write a recursive algorithm to find the height of a binary tree.

Ans: Height = 1 + max(left_height, right_height). Skewed tree of n nodes has height n.

Q6. Define Full, Complete, Perfect, and Skewed Binary Trees with examples.

Ans: Full: All nodes 0/2 children

Complete: All levels filled left to right

Perfect: All internal nodes have 2 children

Skewed: Only one child repeatedly

Q7. Insert 40, 20, 60, 10, 30, 50, 70 into BST and draw final tree.

Ans: In-order insert:

40

/ \

20 60

/\ /\

10 30 50 70

Q8. Delete node 40 from above BST. Show updated BST.

Ans: Find inorder successor (50). Replace 40 with 50 and delete original 50.

Updated root = 50.


Tree & AVL Tree Practice Questions with Solutions

Q9. Construct AVL Tree by inserting: 9, 5, 10, 0, 6, 11, -1. Show all rotations.

Ans: Apply insertions. LR rotation at 5 needed. Final root = 9. Show stepwise tree.

Q10. What is balance factor in AVL? Show LR Rotation on inserting 50, 40, 45.

Ans: Balance Factor = height(left) - height(right). Insert -> 50, 40, 45 causes LR. First left rotate on 40's child,

then right rotate on 50.

Q11. Compare worst-case insertion time of BST and AVL.

Ans: BST: O(n) in skewed

AVL: O(log n) due to balancing

Q12. Mention four real-life applications of trees in CS.

Ans: 1. File systems (N-ary Tree)

2. Databases (B-trees)

3. Routing (Tries)

4. Expression parsing (Expr. Tree)

Q13. Build an expression tree for: (a + b) * (c - d), and write postfix/prefix.

Ans: Tree:

/\

+ -

/\/\

a bc d
Tree & AVL Tree Practice Questions with Solutions

Prefix: * + a b - c d

Postfix: a b + c d - *

Q14. Write non-recursive inorder traversal using stack.

Ans: Use stack. Push left till NULL, pop, visit, move right. Repeat.

Q15. Write 4-point differences: BST vs AVL, Level vs Inorder, Stack vs Queue (in context of trees).

Ans: - AVL is balanced, BST can be skewed

- Inorder is depth-first, level is breadth-first

- Stack: DFS, Queue: BFS

You might also like