Data Structures & AlgorithmsVery hardProduct company OA
B-tree with minimum degree t = 2 (a 2-3-4 tree, at most 3 keys per node) using the single-pass insertion that splits every FULL node on the way down. Keys 10..90 are inserted in increasing order. What is printed (root keys, its children's keys, total nodes, height)?
B-tree with minimum degree t = 2 (a 2-3-4 tree, at most 3 keys per node) using the single-pass insertion that splits every FULL node on the way down. Keys 10..90 are inserted in increasing order. What is printed (root keys, its children's keys, total nodes, height)?
T = 2
class Node:
def __init__(s, leaf=True): s.keys, s.kids, s.leaf = [], [], leaf
def split(x, i):
y = x.kids[i]; z = Node(y.leaf)
mid = y.keys[T - 1]
z.keys = y.keys[T:]; y.keys = y.keys[:T - 1]
if not y.leaf: z.kids = y.kids[T:]; y.kids = y.kids[:T]
x.kids.insert(i + 1, z); x.keys.insert(i, mid)
def ins_nonfull(x, k):
if x.leaf:
x.keys.append(k); x.keys.sort(); return
i = sum(1 for key in x.keys if key < k)
if len(x.kids[i].keys) == 2 * T - 1:
split(x, i)
if k > x.keys[i]: i += 1
ins_nonfull(x.kids[i], k)
root = Node()
splits = 0
for k in [10, 20, 30, 40, 50, 60, 70, 80, 90]:
if len(root.keys) == 2 * T - 1:
s = Node(False); s.kids = [root]; split(s, 0); root = s; splits += 1
ins_nonfull(root, k)
def count(x): return 1 + sum(count(c) for c in x.kids)
def height(x): return 0 if x.leaf else 1 + height(x.kids[0])
print(root.keys, [c.keys for c in root.kids], count(root), height(root))- A. [40] [[20], [60]] 7 2
- B. [40, 60] [[20], [50], [70, 80, 90]] 6 1
- C. [50] [[20, 30], [70, 80]] 7 2
- D. [40] [[20], [60, 80]] 8 2
Correct answer: A. [40] [[20], [60]] 7 2
Splitting proactively means a full node is split even if the new key would not have overflowed it. 10, 20, 30 fill the root; 40 splits it (20 up). 50 fills the right leaf [30, 40, 50]; 60 splits it (40 up). 70 → [50, 60, 70]; 80 splits it (60 up) → root [20, 40, 60] is now full. 90: the full root splits first (40 up, height 2), then [60]'s child [70, 80] receives 90. Result root [40], children [20] and [60], with leaves [10] [30] [50] [70, 80, 90] – 7 nodes. Sequential keys leave most nodes half-empty.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Building a min-heap from [9, 8, 7, 6, 5, 4, 3]: (a) seven sift-up insertions, (b) bottom-up heapify. What is printed (heaps and swap…
- Exact count of the innermost statement for n = 30 (j advances by i, k advances by j). What is printed? python c = 0 n = 30 for i in…
- A memoised recursion f(n) = f(n//2) + f(n//3) + f(n//6) is evaluated for n = 10¹². How many distinct calls (cache misses) are made? python…
- Patience-sorting tails array for the longest STRICTLY increasing subsequence, and the length for the non-decreasing variant. What is…
- Weighted interval scheduling (start, end, value): DP over jobs sorted by end time with binary search for the last compatible job, versus…
- Broken-profile DP: number of ways to tile a 3 × n board with 1 × 2 dominoes, column by column with a 3-bit mask of cells already covered…