Thursday, 31 March 2016

This is a blog about arity of trees. Arity is the number of maximum children a node can have. It applies to trees. A binary tree have arity of 2 because it only have left and right tree. Trees can have a number of arity.

We learned that I can use a recursive method to find arity. The base case is i have no children which means that I return an arity of 0 and the induction case is that I call the function on every children of the node and append the number of children of this node. Then I call the max function on it. Eventually we will have the arity of the whole tree.

This is a blog about trees. Trees are data structures with nodes and they have children. They store a value and they we can visit their children through the nodes. This is good for storing multiple level of memory. In labs I just learned how to visit every node of the tree and I used recursion. The idea is simply calling on every children of the node and stops when the node say we don't have children anymore.

Using recursion in tree is awesome because we can visit every value and go to the every node unless the function reached a node with no children. This is good in a lot of ways. We can search every part of the tree and know how many node meet the requirement of our condition.


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

Thursday, 3 March 2016

This week I am thinking about recursion. I have encountered functions such as max, any, all, sum. Recursion consist of a base case and a continuous case. If I reach the end of the base case I stop and recursion keep calling itself if I have the continuous case.

I learned that If I need to keep calling my function to count the sum or count I need to use sum. I return 0 if I hit None and return 1 if I have an element. Then I use the sum function to sum them up. Any function tells if there's a true. It is useful when I want to see if I have a even number in my nested list and all function is useful when I want to see if all the list is even.