Monday, March 2, 2015

Week 7: ADT + Recursion

On my last blog post, I already discussed object-oriented programming. 
This week, I will blog about abstract data types (which will also include recursion!).

So far, we have learned about Trees, and how to access the information located at the children of the parent of the tree. 

If we have to access the information from the children numerous times, we would have to write a recursive method, which is, of course, down my weakest alley so far yet again. 

For example, last week in class, we looked at the bst_contains and evaluate methods.

bst_contains looks at a sorted tree, and sees if there is a specific value within that tree. If given a value at the node, all the numbers that are less than the value are sorted to the left and all the numbers greater than the value at the node will be sorted to the right. That is where the code should be looking.

It uses recursion to find the value in the tree. 
For example, if the value at the parent node is 5, and we are looking for the number 9, since 9 > 5, the code should go to the right of the node and call on bst_contains again. This means that the base case should have a statement that evaluates to false, and another to true. 

One base case we have is if the node is None (there are no children), then the statement will return false.

If the value is greater than the parent node, we will look at node.right (right of the parent), and keep calling on bst_contains until we either reach or will not reach the value we are trying to find. If the value is not in the tree, it will return false (due to the statement we wrote above in the last paragraph), but if the value is finally reached, we will have an else statement that says that if the value == node.data (the value we are looking for), then it will return True.


Next, we wrote an evaluate method. This also looks at the abstract data type Tree. 
The values contained at the nodes this time are not just values, but actually evaluation symbols as well, such as +, -, *, and /.

Look at 
def evaluate(b):

the base case is that if b has no children, then we just return b:
if b.left is None and b.right is None:
    return b

but if that is not the case, we have to format the information stored at the nodes in a way such that python can evaluate it. 

For example, if the parent node was '*' and the children nodes were 3.0 and 4.0, we want python to evaluate 3.0 * 4.0. 
So in the else statement (if the parent node is not a leaf): 

else:
    return eval(str(evaluate(b.left)) + b.data + str(evaluate(b.right))

Wowowwowowowwow so many nested methods. 
Previously, I said I wanted a statement that would say 3.0 * 4.0. 
However, if the children nodes were not values, but yet again another one of +, -, *, and /, we will have to use recursion and keep adding to the evaluation statement. 
We have to call evaluate again on the left and right nodes because we have to keep finding the children nodes until we reach a leaf, which is when the code will evaluate the base case if statement. 

Then we have to turn the statement into str because b.data is also a string, and we use string concatenation to form, for example, '3.0 * 4.0'.

eval will evaluate the string to return 12.0. 


That's abstract data types and recursion for you. 


No comments:

Post a Comment