Last week, (week 9) we had our second and final test for the semester. The test covered topics from weeks 5-8, and concepts such as tree definitions (travelling through trees using postorder, inorder and preorder traversals), recursive functions, binary trees and binary search trees, as well as linked lists. We were also allowed an aid sheet. In general, I didn't find the test to be too difficult, some concepts, especially the newer ones, such as linked lists were harder to understand. Since the test occurred during lecture time, we did not go over much in class. In fact we had less than an hour for the lecture last week.
Earlier in the semester 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.
Finally, in class we went over, briefly a function for deleting data from a binary search tree (BST) rooted at node. In order to write a function for deletion of data, we had to first understand and write a general algorithm for delete. This is both to understand what we want the function to do and also to make writing it more simple and easier. Our algorithm for delete, is to first see if node is None, if True then return that node. Next, we want to check if the data is less than the data at the node (like we did in the insert function). If it is we delete it from the left child and return this node. Third, we check if the data is more than the node.data, and delete it if it is from the right child, returning this node. The fourth case we can have, is that the node has fewer than two children. If that is the case, then one must be None, and must therefore return the other one. Finally if the node has two non-None children (i.e there is data for both the right and left), then the data must be replaced with that of its largest child in the left subtree. That child is then deleted and the new node returned. Looking at the code, its clear that this is what happens, and that by writing out an algorithm first, it was easier to write the code.
No comments:
Post a Comment