


Ninja has to implement a binary tree class from scratch. The Ninja can perform three types of queries on this binary tree.
All the Node values in the binary tree are different. All nodes are equally likely to be chosen.
For example:



The first line of input contains an integer 'T' representing the number of test cases. Then the test cases follow.
The next line of each test case contains an integer ‘Q’ representing the number of queries to Ninja.
The next line, ‘Q’ lines of each test case, contain a character and integer representing which type of query is to perform on the binary tree and the value of the node.
Output Format :
For each test case, return the value of the Random Node obtained from the get Random Node query.
Print the output of each test case in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10
1 <= ‘Q’ <= 10000
Char = {‘I’, ‘D’, ‘R’}
1 <= ‘VAL’ <= 100000
Time Limit: 1 second
1
5
I 2
I 4
I 5
D 4
R
Valid answer
For sample test case 1:



In this sample test case, the random nodes may be 2 or 5 because all nodes should be equally likely to be chosen. So you can print any one of them.
As the random node returned (here 2) exists in the tree, the output is a “Valid answer.
1
5
I 4
I 2
D 2
I 5
R
Valid answer
For sample test case 1:




In this sample test case, the random nodes may be 4 or 5 because all nodes should be equally likely to be chosen. So you can print any one of them.
As the random node returned (here 4) exists in the tree, the output is a “Valid answer”.
Think of the brute force approach.
In this problem, we have to implement a binary tree class from scratch. We have to deal with 3 queries: Insert a node in the binary tree, Delete a node in the binary tree and get a random node in the binary tree. So we make three functions to deal with these three queries.
For inserting a node in the binary tree, first, we do iterative level order traversal in the given binary tree using a queue. If we find a node whose left/right child is empty, we make a new node with ‘VAL’ as the data of the node and add this node into the left/right child in the binary tree.
For deleting a node in the binary tree, first, we start from the root of the binary tree then find the deepest and rightmost node of the binary tree and the node which we want to delete. Then we replace the deepest rightmost node value with the node to be deleted. Then we delete the deepest right node.
For finding a random node in the binary tree, first, we store the inorder traversal of the binary tree in an array/list. Then we generate a random number between 0 to a number of nodes - 1 and return the value which is present at that random index in the array/list.
Here is the algorithm :
Insertion in the binary tree :
Deletion in the binary tree :
Function DELETE_DEEPEST_NODE( ROOT,‘DEEPEST_NODE’):
Get random node from the binary tree:
O(N) where ‘N’ represents the number of nodes in the binary tree.
For inserting a node in the binary tree we traverse all the nodes.
For deleting a node in the binary tree we traverse all the nodes.
For finding a random node in the binary tree first we traverse all the nodes and store into an array/list ‘allNodes’ and then print a random node. So the overall time complexity is
O(N).
O(N) where ‘N’ represents the number of nodes in the binary tree.
For inserting a node in the binary tree we traverse all the nodes and store them into a queue.
For deleting a node in the binary tree we traverse all the nodes and stores them into a queue.
For finding a random node in the binary tree first we traverse all the nodes and store them into an array/list ‘ALL_NODES’ and then print a random node. So the overall space complexity is O(N).