Thursday, 31 March 2016


Slog to submit
                                                             

This week  I am thinking on binary trees. Binary tree is a structure consisting of a a parent node and have a left and right children. So we have two children in total. If the tree have no children, then the two children would be None and None. This structure can hold million of structure and it is good for both storing and searching. It is good for storing a value because it only have the address of the tree node where it is  being store and it is good because we don't have to do a lot of memory allocation because we are only adding the memory addresses. It is good for searching because we have the values lesser than the node value on the left and the values bigger than the node value on the right.

Binary tree is good for running time. For example a height of h can store nodes up to 2^h-1 nodes. Think about that and we only have to do h operation in general in order to get to the node we want that is really fast. This means our computer runs more efficiently if we use binary trees.

The recursion method is really important for accessing binary tree. So this is how it work. Our base cases would be if we reach a leaf or None. We call a node a leaf when we see that this node has no children. The condition that left and right children are none are applied. If the node is None we stop recursion. The inductive case applies when we have left or right children is not None so this means the node is not a leaf. We use recursion by calling the method on left and right child. Those will eventually will reach a leaf or None and we done recursion. That's how the topic of recursion works on the tree. The code would be like:

def findheight(Tree):
      if Tree is None:
               return 0
      elif Tree.left is None and Tree,right is None:
               return 1
      else:
               return max(list(findheight(Tree.left), findheight(Tree.right)))

That's how it is done in an example and we can see it works well.

A Tree in general is actually a node with value and a bunch of children. Binary tree only have 2 children and binary tree is much efficient for searching and adding values. Tree would have empty list if it doesn't have children unlike binary tree we have None to indicate that it have 1 or 0 children

No comments:

Post a Comment