Saturday, 7 March 2015

Week 8: Binary Tree

In this week's slog, I'm going to talk about what we learned last week, Binary Tree Node. 
Binary term will be very familiar to techy person and yes it has to do something with 0 and 1.
Binary Tree can contain two children per node: left and right. 
Which means, initializing method will look like this:
class BTNode:
    """
    Binary Tree
    """
   
    def __init__(self, data, left=None, right=None):
        """(BTNode, object, BTNode, BTNode) -> NoneType
        """
        self.data, self.left, self.right = data, left, right

If we’re about to picture what BTNode looks like figuratively, it will be similar to the diagram below.
            self.data
               /      \
   self.left        self.right
   /          \           /         \  
left       right  left        right
(… and so on)

So now you guys are familiar with Binary Tree Node, let’s say this binary tree contains its element(integers) under the rule “parent node of two children nodes must be in between of two children’s numbers”.

In other words, if two children nodes have value of 4 and 8 respectively, their parent node’s value must be greater than 4 and smaller than 8.

In code wise, example should look like this: BTNode(7, BTNode(5), BTNode(9))
Maybe if we want to go deeper, we can try with 
BTNode(7, BTNode(5, BTNode(2), BTNode(6)), BTNode(9, BTNode(8))).

While you go deeper in Binary Tree Node or any other tree lists, you need to keep in mind that the rule must apply to every trees(even children’s trees).
That is why under BTNode(5), left node is 2 (smaller than its parent node 5 and 5’s parent node 7) and right node is 6(bigger than its parent node 5 but smaller than 5’s parent node 7). 
Moreover, same deal in BTNode(9): its left child has value of 8, smaller than 9 and bigger than 9’s parent node 7 since 9 is right child of 7.

Now, tree can be used in so many different ways but our coding for creating tree lists will be useless if we don’t have a code to evaluate things.
So, we will come up with a function that figures out whether BTNode contains the value we want to search or not. 

Basically, we want to figure out body function of below.

def contain(node, value):
    """(BTNode, object) -> BOOL
   
    Return a Boolean value indicating whether tree rooted at node contains value or not.

    >>> contain(None, 5)
    False
    >>> contain(BTNode(7, BTNode(5, BTNode(2), BTNode(6)), BTNode(9, BTNode(8))), 2)
    True
    """

First of all, if there is none of node to search in the answer will be False so,

    if node is None:
        return False

Second, this is where we need to use recursion method since we need to compare every node’s (even children’s) children values to search.  Meaning,

    elif value < node.data: #want to search left node since left is smaller than parent node
        return contain(node.left, value) #search the value by using recursion function
    elif value > node.data: #want to search right node
        return contain(node.right, value)#Also recursion incase children has its own BT
    else: #value is equal to node.data
        return True

Therefore, complete function of contain will look like:

def contain(node, value):
    """(BTNode, object) -> BOOL

    Return a Boolean value indicating whether tree rooted at node contains value or not.

    >>> contain(None, 5)
    False
    >>> contain(BTNode(7, BTNode(5, BTNode(2), BTNode(6)), BTNode(9, BTNode(8))), 2)
    True
    """
    if node is None:
        return False
    elif value < node.data:
        return contain(node.left, value)
    elif value > node.data:
        return contain(node.right, value)
    else: 
        return True

There are so many ways to use the Binary Tree list as and it is all up to coders(you guys!) who will create them. 
Hopefully you guys have a better understanding of Binary Tree List and next week I'm probably going to talk about Linked List.

Keeping slogs is actually a great way to review class materials and I believe this will help me to get higher marks in the test.

No comments:

Post a Comment