Question: Please write in python. Binary search tree (BST) is a special type of binary tree that satisfies the binary search property, i.e., the key in
Please write in python.
Binary search tree (BST) is a special type of binary tree that satisfies the binary search property, i.e., the key in each node must be greater than any key stored in the left sub-tree, and less than any key stored in the right sub-tree. Your task is to implement a BST class, satisfying the following requirements (you can create more methods/attributes if needed):
1) Must have an insert (self, value) function that inserts a new node with a given value into the BST. You may assume that the values to be stored in the tree are integers.
2) Must have a get total height(self) function that computes the sum of the heights of all nodes in the tree. Your get total height function should run in O(n) time in the worst case, where n refers to the total number of nodes in the tree.
3) Must have a delete(self, value) function that could be used to delete one node from the BST by its value, recursively.
4) Write a save(self) and a restore(self, input string) function for your BST class. These two functions can transfer a BST into a string and reconstruct it back to the same tree.
5) Write test code in the main function, covering all functions mentioned above.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
