Key Data Structures Q&A Guide
Key Data Structures Q&A Guide
Binary search is preferred over linear search for sorted arrays due to its efficiency. It reduces the search space by half with each comparison, resulting in a time complexity of O(log n), compared to linear search's O(n). This makes binary search significantly faster for large datasets .
Dijkstra's Algorithm finds the shortest path from a starting node to all other nodes in a weighted graph. It maintains a set of nodes whose shortest distance is known and iteratively selects the node with the smallest tentative distance, updating paths as more optimal paths are found. For example, starting from a node, paths are relaxed by checking adjacent nodes and updating distances accordingly .
Arrays are stored in contiguous memory locations, which allows for efficient indexing but requires a predetermined size. In contrast, a linked list consists of nodes, each containing data and a pointer to the next node, enabling dynamic memory allocation. Linked lists allow for easy modification of size without the need for reallocation but require sequential access to elements due to non-contiguous storage .
In tree structures, 'height' refers to the longest path from a node to a leaf, while 'depth' is the distance from the root to the node. The height of a tree is determined by its root node, whereas the depth is intrinsic to each node's position relative to the root .
To configure a B-tree of order 5, insert elements one by one, splitting nodes as necessary when they exceed the maximum of 4 keys. For the data set 78, 21, 11, etc., perform insertions maintaining sorted order within nodes and split nodes evenly, promoting median elements. The final B-tree structure should balance as keys are inserted without exceeding node constraints .
Recursion is a programming technique where a function calls itself to solve smaller instances of the same problem. One practical use is calculating factorials, where the factorial of n (n!) involves recursive calls to calculate (n-1)!. A recursive function terminates on reaching a base case like n=1 .
Asymptotic notations describe the performance of an algorithm as the input size goes to infinity. They are crucial in assessing the efficiency of algorithms beyond empirical testing. Examples include Big O (O(n)), Theta (Θ(n)), and Omega (Ω(n)), which classify algorithms based on upper, tight, and lower bounds of performance .
To convert an infix expression to postfix using a stack, operators are pushed onto a stack while operands are added directly to the postfix expression. Operators are popped from the stack and added to the postfix expression when a lower precedence operator appears. Evaluating the postfix expression '2 3 9 * + 2 3 ^ - 6 2 / +' results in 28.5 after performing the operations according to postfix order .
A 2-D array is stored in memory either in row-major or column-major order. In row-major order, the elements of each row are stored in consecutive memory locations, e.g., for a 2x2 array 'A', elements A[0][0], A[0][1] are stored first followed by A[1][0], A[1][1]. Similarly, column-major order stores elements column by column .
An abstract data type (ADT) encapsulates data and operations into a single logical unit without specifying the implementation details. This allows programmers to focus on high-level design rather than underlying mechanics. For example, a stack can be implemented as an ADT; it defines operations like push and pop without detailing whether the stack is implemented using arrays or linked lists .