Hello, Citizens of the World!
Today's topic:
bst_del_rec(tree, data)
Base Case = If we have a None tree return None
Recursive Case One = If data is less than tree data have it deleted from the left child.
Recursive Case Two = If data is greater than tree data have it deleted from the right child.
If none of the above if statements are true we have found the tree node that we need to delete.
Possible Cases
Case 1 = If the tree node does not have a left child, return the right child.
Case 2 = If the tree node does have a left child, find its largest node, and replace the tree node's data with the largest data that has been found so far. Then delete the largest value.
Recursive vs Iterative Functions:
Recursive:
A recursive function imposes a loop, then loop is implicit and Python is able to take care of it. This costs us time and memory, the cost may be negligible. Every single recursive function can be written in an iterative manner using explicit loops which may or may not require stacks. Though depending on the problem writing it iteratively is a bad idea as it could be extremely time consuming and illegible. This is because the problem may naturally be recursive.
No comments:
Post a Comment