Sunday, 2 March 2014

Week 6 - Trees

Week 6 began with the talk about the concept of Trees in computer science. Essentially, Trees are a type of data structure that is used in computer science that is a way to store information. The trees that we did were binary trees starting out with the root with each root having children or leaves called nodes. Each node can have a value and the tree can be traversed recursively. Trees are a very convenient way to store information since the leaves of a binary tree can be treated as two different keys with values. The keys can be ordered so that the side of a tree can be keys with values that are smaller than another side. Thus, traversing a tree becomes very efficient since you can code it recursively to always go towards the smaller side of a tree in order to find the smallest or largest value. Due to the way a binary tree is implemented, you can search for values in the nodes recursively since you know which of the two leaves of a node has a certain property and recursing for long enough will get you the value that you are looking for. Trees can be thought of as a more complex list to store information in.

Like most things in python, trees can be written in a object oriented way. The first step is to initialize your tree by setting up the different nodes and seeing whether you want a binary tree or another type of tree. The rest of the functions can be anything that you want such as the length of your tree. In most cases, the traversing of your tree is done recursively. In the binary tree, each node contains 2 possible keys and you recursively decide which key to take depending on what you want. Eventually the recursion will take you to the value you need within the tree data structure.

No comments:

Post a Comment