#8. Binary Search Tree
Binary Search Tree
Description
In this assignment, you will implement a Binary Search Tree (BST) that supports four fundamental operations: insertion, searching, and finding the predecessor and successor of a given key.
A Binary Search Tree is a node-based binary tree data structure which has the following properties:
- The left subtree of a node contains only nodes with keys lesser than the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- Both the left and right subtrees must also be binary search trees.
You will process a sequence of commands, each representing one of the following operations:
-
ins k: Insert an integer keykinto the BST. Ifkalready exists in the tree, this operation should have no effect. -
find k: Search for the keykin the BST. Outputtrueif it exists, andfalseotherwise. -
succ k: Find the successor ofk. The successor is defined as the smallest key in the tree that is strictly greater thank. If no such key exists, returnnull. -
pred k: Find the predecessor ofk. The predecessor is defined as the largest key in the tree that is strictly smaller thank. If no such key exists, returnnull.
To ensure the tree remains efficient for these operations, the insertion sequence is guaranteed to create a reasonably balanced tree, avoiding worst-case scenarios where the tree degenerates into a linked list.
For this problem, you can assume that the k in successor and predecessor queries is guaranteed to be present in the tree at the time of the query.
Format
Input
- The first line contains a single integer (), representing the number of operations to perform.
- The next lines each contain an operation, formatted as a string followed by an integer key (). The string will be one of
ins,find,succ, orpred.
Output
- For each
findoperation, printtrueorfalseon a new line. - For each
succorpredoperation, print the integer value of the successor or predecessor on a new line. If one does not exist, printnull. - The
insoperation should not produce any output.
Samples
5
ins 10
ins 5
ins 15
find 5
find 6
true
false
Explanation: After the three insertion operations (ins 10, ins 5, ins 15), the BST will have the following structure:
10
/ \
5 15
find 5: The key5exists in the tree as the left child of the root. So, the output istrue.find 6: The key6does not exist. A search would go from10to5, and since6 > 5and5has no right child, the search fails. So, the output isfalse.
13
ins 50
ins 30
ins 70
ins 20
ins 40
find 60
ins 60
ins 80
find 60
succ 40
pred 60
pred 20
succ 80
false
true
50
50
null
null
Explanation:
- The first five
insoperations build an initial tree. The firstfind 60is executed on this tree. Since60has not been inserted yet, the output isfalse. - After inserting
60and80, the final tree structure for the remaining queries is:
50
/ \
30 70
/ \ / \
20 40 60 80
find 60: Now60exists in the tree. The output istrue.succ 40: The smallest key in the tree greater than40is50. The output is50.pred 60: The largest key in the tree smaller than60is50. The output is50.pred 20: The smallest key in the tree is20. There is no key smaller than20. The output isnull.succ 80: The largest key in the tree is80. There is no key greater than80. The output isnull.
13
ins 50
ins 30
ins 70
ins 20
ins 40
find 60
ins 60
ins 80
find 60
succ 40
pred 60
pred 0
succ 80
succ 79
false
true
50
50
null
null
80
Hint
- A solution with overall complexity will be too slow. Since the tree is guaranteed to be reasonably balanced, each operation should ideally take about time, where is the current number of nodes in the tree.
- The test cases are structured as follows to help you incrementally build and test your solution:
- Test Cases 1-5: Small-scale inputs with only
insandfindoperations to check basic tree construction and search logic. - Test Cases 6-10: Large-scale inputs ( up to ) with only
insandfindoperations. These cases will test the efficiency of your insertion and search algorithms. - Test Cases 11-20: Full-scale test cases including all four operations (
ins,find,succ,pred) to test the correctness and efficiency of your complete implementation.
- Test Cases 1-5: Small-scale inputs with only
Limitation
1s, 128MiB for each test case.