DSA VIVA QS
DSA VIVA QS
Module 4: Trees
Questions and Answers:
1. What is a tree?
A tree is a non-linear hierarchical data structure consisting of nodes, with one node as the root and zero or
more child nodes.
2. What are the different types of trees?
o Binary Tree
o Binary Search Tree (BST)
o AVL Tree
o Heap Tree
3. What is a binary tree?
A tree where each node has at most two children (left and right).
4. What are the properties of a binary tree?
o Maximum nodes at level l=2ll = 2^l
o Height hh: Maximum number of edges from the root to a leaf
5. What are tree traversals?
o In-order: Left, Root, Right
o Pre-order: Root, Left, Right
o Post-order: Left, Right, Root
6. What is a binary search tree (BST)?
A binary tree where the left child contains nodes with values less than the root, and the right child contains
nodes with values greater than the root.
7. How do you insert a node in a BST?
Recursively compare the value to be inserted with the current node and move left or right until the correct
position is found.
8. What is an AVL tree?
A self-balancing binary search tree where the height difference (balance factor) of left and right subtrees is at
most 1.
9. What is a threaded binary tree?
A binary tree where null pointers are replaced with pointers to the in-order predecessor or successor.
10. What are the applications of trees?
o Hierarchical data representation (e.g., file systems)
o Expression parsing
o Decision-making algorithms
11. What is a complete binary tree?
A binary tree where all levels except possibly the last are completely filled, and nodes are as left as possible.
12. What is a full binary tree?
A binary tree where every node has either 0 or 2 children.
13. What is a perfect binary tree?
A binary tree where all internal nodes have two children, and all leaves are at the same level.
14. What is a balanced binary tree?
A binary tree where the height difference between left and right subtrees is minimal.
15. What is a binary heap?
A complete binary tree used for heap sort, where the parent node is either greater than or smaller than its
child nodes (max heap or min heap).
16. What are the operations on a binary search tree?
o Insertion
o Deletion
o Searching
17. What is a height-balanced tree?
A tree where the height difference between subtrees of any node is at most 1.
18. What are threaded binary trees used for?
To efficiently traverse trees without recursion or stacks.
19. How is a binary tree implemented in memory?
Using linked representations with nodes containing data and pointers to child nodes.
20. What is the time complexity of searching in a binary search tree?
O(h)O(h), where hh is the height of the tree.
Additional Questions
Recursion
1. What is recursion?
A function that calls itself either directly or indirectly.
2. What are the two main parts of a recursive function?
o Base case: Stops the recursion.
o Recursive case: Calls the function itself.
3. What is direct recursion?
When a function directly calls itself.
Example:
4. void func() { func(); }
5. What is indirect recursion?
When a function calls another function, which in turn calls the original function.
Example:
6. void funcA() { funcB(); }
7. void funcB() { funcA(); }
8. What is tail recursion?
A recursive function where the recursive call is the last operation performed.
Example:
void tailRecursion(int n) {
if (n == 0) return;
tailRecursion(n - 1);
}
9. What is non-tail recursion?
A recursive function where the recursive call is not the last operation.
Example:
int factorial(int n) {
if (n == 1) return 1;
return n * factorial(n - 1);
}
10. What are the applications of recursion?
o Factorial calculation
o Fibonacci sequence
o Tower of Hanoi
o Depth-First Search (DFS) in graphs
11. What are the advantages and disadvantages of recursion?
o Advantages: Simplifies code for problems like tree traversals.
o Disadvantages: High memory usage due to function call stack.
Advanced Topics
14. What is a trie (prefix tree)?
A tree-like data structure used to store strings, often used in applications like autocomplete.
15. What is a graph?
A non-linear data structure consisting of nodes (vertices) connected by edges.
16. What are the types of graphs?
o Directed and Undirected
o Weighted and Unweighted
o Cyclic and Acyclic
17. What is Depth-First Search (DFS)?
A graph traversal technique that explores as far as possible along each branch before backtracking.
18. What is Breadth-First Search (BFS)?
A graph traversal technique that explores all neighbors at the current depth before moving deeper.
19. What is a spanning tree?
A subgraph of a graph that connects all vertices without any cycles.
General Concepts
20. What is the difference between a mutable and immutable object?
o Mutable: Can be modified after creation (e.g., lists in Python).
o Immutable: Cannot be modified after creation (e.g., strings in Python).