Skip to main content

Data Structures · Search trees and balanced trees

Let T be a BST whose nodes contain only key, left, and right fields (no parent…

Problem

Let \(T\) be a BST whose nodes contain only \(\mathrm{key}\), \(\mathrm{left}\), and \(\mathrm{right}\) fields (no parent pointers, no sizes). Design a single-pass algorithm that returns the \(k\)-th smallest key for a given \(k\) with \(1 \le k \le n\), using \(O(h)\) extra memory. You may use recursion or an explicit stack. Prove that your algorithm visits \(O(h + k)\) nodes in the worst case, and exhibit a family of BSTs and a choice of \(k\) showing that \(\Omega(h + k)\) nodes must be visited by any algorithm that only walks tree edges and has no extra augmenting fields.

Hint

Without sizes you cannot jump into the middle of a subtree. Inorder already names the \(k\)th key as the \(k\)th key it emits; stop there.

Check your work

Work the problem yourself first. Then open it in Training to check your answer and read the full worked solution.

The answer check and full solution for this problem come with ProofAnvil Practice membership ($19 USD monthly). See membership. Or start with the free Data Structures sample problem: Try the free sample problem.

More Data Structures practice problems

Back to Data Structures

An original ProofAnvil practice problem, written for this course. ProofAnvil is a practice course, not a homework-answer service.