Write a Program to implement Binary Search tree as discussed in the Lecture.
The following methods are to be implemented in the Binary Search Tree.
1) insert(key), function which inserts a new element into the tree.
2) remove(key), function which removes the key from the Binary Search Tree
follow these conditions to while removing the element.
i) if the tree-node having key is a leaf node, delete that node.
ii) replace the desired tree node with its child, if that node has only one child.
iii) If the tree node having key, has two non-empty children then use its inorder - predecessor to replace the node. Inorder - predecessor for a tree node is the right most node in its left subtree.
You need to build a binary search tree by inserting elements in the given order.
Then perform insert and remove operations on that tree based on the operation specified as a character (i - insert, d - remove).
Input:
- 1st line has number of elements to be added to the Binary Search Tree (Initial number elements - N)
- 2nd line of the input contains the elements each separated by single whitespace.
- 3rd line has number of operations to be performed (k)
- 4th line has operation, key separated by a white space (i - insert, d - remove)
Sample Input:
4
8 7 15 2
2
i 10 d 2
Output:
The program should output the post order traversal of BST after all the given operations are performed. Separate the elements with a single whitespace. Print “NULL”(without quotes), if the tree is empty.
Output for the above Sample Input:
7 10 15 8
Constraints:
Elements in the binary Search Tree are unique.
0<=N≤ 20
0<=key<=10000 (Elements in Tree)
0<=k<=10Solution -:
After deadline is over