Explore foundational concepts of trees and graphs in programming fundamentals, including traversal, properties, and algorithms. Sharpen your understanding of key characteristics and operations in modern data structures.
What is the maximum number of nodes in a binary tree of height h?
Explanation: The maximum number of nodes in a binary tree of height h is 2^(h+1) – 1. This represents a perfectly balanced (full) binary tree. '2*h + 1' and 'h^2 + 1' do not reflect the exponential growth of nodes. '2^h – 1' is the total for a tree of height h-1, not h.
In a binary search tree (BST), an inorder traversal produces nodes in which order?
Explanation: Inorder traversal of a BST visits nodes in left-root-right order, producing values in sorted ascending order. 'Random order' and 'reverse order' are incorrect; 'level order' is achieved using breadth-first traversal, not inorder.
Which tree traversal is typically used to evaluate arithmetic expression trees?
Explanation: Postorder traversal evaluates subtrees before the root, ideal for expression tree evaluation where operands must be processed before their operators. Preorder and inorder do not guarantee this evaluation order; level order is used for breadth-first operations, not evaluation.
What is the time complexity of Depth First Search (DFS) in a graph with V vertices and E edges?
Explanation: DFS explores each vertex and edge once, giving O(V + E) time complexity. O(V^2), O(V*E), and O(E^2) are incorrect; these describe more resource-intensive operations uncommon in standard DFS.
Which data structure is most commonly used to implement Breadth-First Search (BFS) in graphs or trees?
Explanation: BFS uses a queue to maintain the order of processing nodes level by level. Stack is associated with DFS, array can store nodes but doesn't maintain desired order, and heap is not related to BFS traversal control.
What is the height of a tree with only a single node?
Explanation: A tree with one node (the root) has height 0, as height is the number of edges to the deepest leaf. Height 1 or 2 would require additional nodes; -1 is not a conventional tree height.
In a complete binary tree, all levels except possibly the last are completely filled. True or False?
Explanation: A complete binary tree fills every level except possibly the last, which is filled from left to right. 'False' contradicts the definition. The state is not contingent ('Sometimes True') nor undeterminable if the tree is described as complete.
Which algorithm is commonly used to find the shortest path in a weighted graph?
Explanation: Dijkstra's Algorithm efficiently finds shortest paths in weighted (non-negative) graphs. Prim's is for minimum spanning trees, Bubble Sort is for arrays, and DFS doesn't guarantee shortest paths.
Topological sorting is possible only in which type of graph?
Explanation: Topological sorting orders nodes when dependencies exist, possible only in directed acyclic graphs (DAGs). Graphs with cycles or undirected/complete graphs do not support meaningful topological order.
Which property holds for a Binary Search Tree (BST)?
Explanation: BSTs require left subtree values to be less than the root, and right subtree values to be greater. 'Root < Left < Right' is incorrect order, 'Left = Right > Root' is not required, and not all nodes must have two children.