Wednesday, March 9, 2016

Week 8

This week, we learned about Binary trees and Binary Search trees, a way of organizing data in trees such that they are easier and more efficient to search through. In class last week, we wrote a function called insert that inserted data starting at node, and returned the root. We examined the code (def insert) and talked about a new function for deleting data instead of inserting data. In the function for inserting data, we wrote a recursive function. Taking in a node and data, the function should insert the data at the node and return the root. Starting by assigning the node to a new variable, called return_node, we then wrote an if statement for the simplest example, where there is no node (if not node:), if that is the case, then the variable return_node should be assigned the new data. If the new data is less than the data at the node, then the data is inserted in the left node, else if it is greater than the data at node, then it is inserted to the right. This is a property of binary search trees (greater values are on the right and smaller values on the left of the node). Finally if there is nothing else to do, (else statement), return_node should be returned. This code is easy to understand, especially after breaking it down like I did above. By going through the code line by line and "reading" the if's, elif's and else statements, you can understand what the code does, even if there is no docstring description.

No comments:

Post a Comment