Van Emde Boas Tree - Problem

The Van Emde Boas Tree (vEB Tree) is an advanced data structure that supports predecessor and successor queries on integers in O(log log U) time complexity, where U is the universe size.

Your task is to implement a Van Emde Boas Tree that supports the following operations:

  • insert(x): Insert integer x into the tree
  • delete(x): Remove integer x from the tree
  • predecessor(x): Find the largest integer less than x in the tree (-1 if none exists)
  • successor(x): Find the smallest integer greater than x in the tree (-1 if none exists)
  • contains(x): Check if integer x exists in the tree

Given a list of operations, execute them and return the results of all query operations (predecessor, successor, contains).

The universe size U is fixed at 2^16 = 65536, so all integers are in range [0, 65535].

Input & Output

Example 1 — Basic Operations
$ Input: operations = [["insert",5],["insert",10],["insert",15],["predecessor",12],["successor",12]]
Output: [10,15]
💡 Note: Insert 5, 10, 15 into the tree. Predecessor of 12 is 10 (largest element < 12). Successor of 12 is 15 (smallest element > 12).
Example 2 — Contains Query
$ Input: operations = [["insert",8],["contains",8],["contains",9]]
Output: [true,false]
💡 Note: Insert 8 into tree. Query contains(8) returns true, contains(9) returns false since 9 was never inserted.
Example 3 — No Predecessor
$ Input: operations = [["insert",20],["insert",30],["predecessor",15]]
Output: [-1]
💡 Note: Tree contains 20 and 30. Predecessor of 15 doesn't exist since all elements are ≥ 15, so return -1.

Constraints

  • 1 ≤ operations.length ≤ 1000
  • Each operation is one of: ["insert", x], ["delete", x], ["contains", x], ["predecessor", x], ["successor", x]
  • 0 ≤ x ≤ 65535 (fits in 16 bits)
  • All query operations (contains, predecessor, successor) return results

Visualization

Tap to expand
INPUT OPERATIONSvEB TREE PROCESSINGQUERY RESULTS["insert", 5]["insert", 10]["insert", 15]["predecessor", 12]["successor", 12]Universe: [0, 65535]Insert operations build treeQuery operations return results1Recursive DecompositionSplit universe into √U clusters2Summary StructureTrack non-empty clusters3Cluster NavigationFind predecessor/successor path4O(log log U) TimeOptimal complexity achievedTree structure: {5, 10, 15}Query 12: pred=10, succ=15predecessor(12)10successor(12)15Final Output[10, 15]Query results arrayOnly query operations return valuesKey Insight:Van Emde Boas trees achieve O(log log U) by recursively dividing the universe into √U clusters,creating a hierarchy where each level reduces problem size from U to √U instead of U to U/2.TutorialsPoint - Van Emde Boas Tree | Recursive Square-Root Decomposition
Asked in
Google 12 Microsoft 8 Facebook 6 Amazon 5
2.8K Views
Low Frequency
~45 min Avg. Time
89 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen