Tuesday, March 17, 2015

Binary Search Trees

In lecture we went back to look at binary search trees, which we briefly looked at before when working with binary trees. A binary search tree is a binary tree where the value of the left child is less than the value of the parent node, and the value of the right child is greater than the value of the parent node.

When we worked on binary trees previously, the insert function to add nodes into the tree was always provided, and we never really talked about it. In lecture we finally got to take a closer look at this, as well as deleting nodes. Although there was no lab because of the strike, there was still a lab exercise, which provided the opportunity to experiment with, and further familiarize ourselves with the insert and delete functions.

I found the function delete a little tricky at first, but after playing around with it a little, I got it.

def delete(node, data):
    ''' (BTNode, data) -> BTNode
    Delete node with data if it exists and return the resulting tree.
    '''
    return_node = node
    if node is None:
        pass
    elif node.data > data:
        node.left = delete(node.left, data)
    elif node.data < data:
        node.right = delete(node.right, data)
    elif node.left is None:
        return_node = node.right
    elif node.right is None:
        return_node = node.left
    else:
        node.data = _find_max(node.left).data
        node.left = delete(node.left, node.data)
    return return_node

def _find_max(node: BTNode) -> BTNode:
    '''Find and return maximal node, assume node is not None'''
    return _find_max(node.right) if node.right else node

What this code does is basically removing a specific node from the tree if the tree contains the specific node, and then returns the resulting tree. If the node we are trying to remove is an internal node, we still want to keep it's children/the subtree under it, so we cannot simply locate the node and make it None. Instead, we replace the data of the node with that of its largest child in the left subtree, and then delete that child. This child is larger than all of the other nodes in the subtree, but is smaller than the node it is replacing. This way, the structure of a binary search tree - left node data is smaller than its parent's and right node data is larger than its parent's. An alternative would be to find the smallest node in the right subtree to replace the specific node we are trying to remove.

We can do this by replacing the code under 'else:' with:

        node.data = _find_min(node.right).data
        node.right = delete(node.right, node.data)

and use a helper function _find_min instead of _find_max
def _find_min(node: BTNode) -> BTNode:
    '''Find and return minimal node, assume node is not None'''
    return _find_min(node.left) if node.left else node

We were also prompted to think about the efficiency of functions on a binary search tree. For a regular binary tree with nodes randomly placed, to find a certain node in the worst case, we would have to traverse through all of the tree's nodes. However, with how the nodes are organised in a binary search tree, the worst case requires time proportional to the height of the tree, which would be a lot less than a regular tree with nodes that had more than one child. Overall, I found binary search trees to be fairly fascinating to think about and really fun to play around with.

We had our second test during our Wednesday lecture time slot. The test was a lot easier than I thought it would be. I don't think there was anything in particular that stood out, it was basically types of things that we had already done in either lecture or labs. 

No comments:

Post a Comment