Interview Questions on Tree Data Structure (C Language)

📘 Basic Tree Concepts – Short Answer Interview Questions

  1. What is a tree data structure in C?
    A hierarchical data structure with nodes connected by edges, where each node has a parent and zero or more children.
  2. How is a tree node typically represented in C?
    Using a structure with data and pointers to child nodes (for binary trees, left and right pointers).
  3. What is the difference between a tree and a binary tree?
    Binary trees restrict each node to at most two children (left and right).
  4. What are the common tree traversal methods?
    In-order, pre-order, post-order (DFS), and level-order (BFS) traversals.
  5. What is the height of a tree?
    The number of edges on the longest path from root to a leaf node.
  6. How do you calculate the depth of a node?
    Number of edges from the node to the tree's root.
  7. What is a leaf node?
    A node with no children.
  8. What is a binary search tree (BST)?
    A binary tree where left subtree contains only nodes with values less than the parent, and right subtree contains greater values.
  9. What are the time complexities of BST operations?
    Search/Insert/Delete: O(h) where h is tree height (O(log n) for balanced trees).
  10. What is a complete binary tree?
    A tree where all levels are completely filled except possibly the last, which is filled left to right.
  11. What is a perfect binary tree?
    A tree where all interior nodes have exactly two children and all leaves are at the same level.
  12. How is a tree different from a graph?
    Trees are acyclic connected graphs with exactly one path between any two nodes.
  13. What is an AVL tree?
    A self-balancing BST where heights of left and right subtrees differ by at most 1.
  14. What is tree rotation?
    An operation to change the structure of a tree while preserving the BST property, used in balancing.
  15. How do you implement a tree in C without pointers?
    Using arrays where indices represent parent-child relationships (heap implementation).
  16. What is the maximum number of nodes at level 'l' in a binary tree?
    2l nodes (for level 0 being the root).
  17. What is a threaded binary tree?
    A tree where null pointers are replaced with threads to other nodes for efficient traversal.
  18. What is the advantage of using trees over arrays/linked lists?
    Faster search (O(log n)) compared to O(n) in arrays/lists when balanced.
  19. How do you delete a node from a BST?
    Three cases: node with no children (simple delete), one child (replace with child), two children (replace with inorder successor/predecessor).
  20. What is a trie (prefix tree)?
    A tree-like structure for storing strings where each node represents a character prefix.
  21. What is a B-tree?
    A self-balancing tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time.
  22. What is the difference between B-tree and binary tree?
    B-trees can have more than two children per node and are optimized for disk storage systems.
  23. What is a heap?
    A complete binary tree where parent nodes satisfy heap property (min-heap or max-heap).
  24. How do you represent a tree with more than two children per node?
    Using an array of child pointers or a linked list of children in each node.
  25. What is the time complexity to find the smallest element in a BST?
    O(h) where h is tree height (O(log n) if balanced), found by traversing leftmost path.
  26. What is the diameter of a tree?
    The longest path between any two nodes in the tree.
  27. What is a splay tree?
    A self-adjusting BST where recently accessed elements are moved to the root for faster access.
  28. What is a segment tree?
    A tree data structure for storing intervals or segments, useful for range queries.
  29. What is a Fenwick tree?
    A data structure that can efficiently update elements and calculate prefix sums in a table of numbers.
  30. What is the advantage of threaded binary trees?
    Allows in-order traversal without recursion or stack, saving memory.

📗 Advanced Tree Concepts – Short Answer Interview Questions

  1. How do you check if a binary tree is a BST?
    Perform in-order traversal and verify sequence is sorted, or recursively check BST property at each node.
  2. What is the time complexity of tree traversal algorithms?
    O(n) for all traversals as each node is visited exactly once.
  3. How do you balance a BST?
    Using rotations (AVL) or restructuring operations (Red-Black trees).
  4. What is a Red-Black tree?
    A self-balancing BST with color properties on nodes ensuring O(log n) operations.
  5. How do you find the lowest common ancestor (LCA) of two nodes?
    For BST: use BST properties. For general trees: use parent pointers or recursive search.
  6. What is the difference between B-tree and B+ tree?
    B+ trees store data only in leaf nodes and have linked leaves for sequential access.
  7. How do you serialize a binary tree?
    Convert to string representation (e.g., pre-order with markers for null nodes).
  8. What is Morris traversal?
    An in-order traversal that uses threading to avoid recursion/stack (O(1) space).
  9. How do you implement a priority queue using a heap?
    Use max-heap for max-priority queue or min-heap for min-priority queue.
  10. What is the time complexity of building a heap?
    O(n) using bottom-up heap construction.
  11. How do you find the kth smallest element in a BST?
    In-order traversal until k elements are visited (O(k) time).
  12. What is the advantage of B-trees for databases?
    Minimizes disk I/O by having high branching factor and storing multiple keys per node.
  13. How do you count the number of nodes in a complete binary tree without traversing?
    Use the property that size = 2h+1-1 for perfect trees, adjust for complete trees.
  14. What is the time complexity of finding the diameter of a tree?
    O(n) using depth-first search.
  15. How do you implement a trie in C?
    Using a structure with child pointers (array or linked list) and an end-of-word marker.
  16. What is the advantage of suffix trees?
    Efficient for string operations like pattern matching, longest common substring, etc.
  17. How do you find the maximum path sum in a binary tree?
    Recursive approach calculating local and global maximum sums.
  18. What is a Cartesian tree?
    A binary tree derived from a sequence that is heap-ordered and maintains sequence order.
  19. What are common applications of tree data structures?
    File systems, databases, compilers (syntax trees), networking (routing trees), AI (decision trees).
  20. How do you choose between different tree implementations?
    Based on operations needed: BST for search, AVL/RB for guaranteed balance, B-tree for disk storage, etc.