B-Tree Implementation - Problem
A B-Tree is a self-balancing tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time. B-Trees are commonly used in databases and file systems.
Implement a B-Tree of order M with the following operations:
- insert(key): Insert a key into the B-Tree
- search(key): Search for a key in the B-Tree, return true if found
- delete(key): Delete a key from the B-Tree
- range_query(start, end): Return all keys in the range [start, end]
A B-Tree of order M has the following properties:
- Each node has at most
Mchildren - Each internal node has at least
⌈M/2⌉children - The root has at least 2 children (unless it's a leaf)
- All leaves are at the same level
- Keys in each node are stored in sorted order
Input & Output
Example 1 — Basic Operations
$
Input:
operations = [["insert", 10], ["insert", 20], ["search", 10], ["range_query", 5, 25]], order = 3
›
Output:
[true, [10, 20]]
💡 Note:
Insert 10 and 20 into B-Tree. Search for 10 returns true. Range query [5,25] returns all keys in that range: [10, 20].
Example 2 — Delete Operation
$
Input:
operations = [["insert", 15], ["insert", 25], ["delete", 15], ["search", 15]], order = 3
›
Output:
[false]
💡 Note:
Insert 15 and 25, then delete 15. Searching for 15 returns false as it was deleted.
Example 3 — Range Query
$
Input:
operations = [["insert", 5], ["insert", 10], ["insert", 15], ["insert", 20], ["range_query", 8, 18]], order = 4
›
Output:
[[10, 15]]
💡 Note:
After inserting 5, 10, 15, 20, range query [8,18] returns keys in that range: [10, 15].
Constraints
- 1 ≤ operations.length ≤ 1000
- 3 ≤ order ≤ 100
- -106 ≤ key ≤ 106
- start ≤ end for range queries
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code