Binary trees is a Tree that has two children for the node.The tree can be empty too with None. The children are called left and right. It's interesting to see how flexible the Tree class can be. You can use the Tree class to apply to specific designs of the format of the Tree class. Through the tree, you can visit the nodes in different ways.
You can visit the tree inorder:
-visit the left subtree inorder
- visit this node itself
- visit the right subtree inorder
You can visit the tree preorder:
-visit this node itself
- visit the left subtree in preorder
- visit the right subtree in preorder
You can visit the tree postorder:
- visit the left subtree in postorder
- visit the rightsubtree in postorder
- visit this node itself
We can design a class that has more conditions than just a binary tree. There is a type of binary tree called binary search trees (BST). The node.data are comparable. The nodes in left subtree are less than node.data. The nodes in the right subtree are more than node.data. This tree is really efficient if you want to go through a tree without looking at every single node.
Tuesday, 24 March 2015
Week 8: Linked Lists
We have been learning about linked lists in class. Linked list structures are as objects (nodes) with a value and a reference to other similar objects. You can also picture them as lists made up of an item (value) and a sub-list (rest). There is a class called linked list node that initialize one node and the next node.
class LLNode:
'''Node to be used in linked list
nxt: LLNode -- next node
None iff we're at end of list
value: object --- data for current node
'''
def __init__(self, value, nxt=None):
''' (LLNode, object, LLNode) -> NoneType
Create LLNode (self) with data value and successor nxt.
'''
self.value, self.nxt = value, nxt
In linked lists, there is a front and back. There is also size to measure how large the list is. To move through the list, LLNode is used to go on to the next node. In a way. linked lists work like recursion, but it doesn't use recursion in it's functions.
To walk through a linked list:
cur_node = self.front
while <some condition here...>:
# do something here...
cur_node = cur_node.nxt
Now, you can apply this formula when you need to walk through the linked list using a function.
class LLNode:
'''Node to be used in linked list
nxt: LLNode -- next node
None iff we're at end of list
value: object --- data for current node
'''
def __init__(self, value, nxt=None):
''' (LLNode, object, LLNode) -> NoneType
Create LLNode (self) with data value and successor nxt.
'''
self.value, self.nxt = value, nxt
In linked lists, there is a front and back. There is also size to measure how large the list is. To move through the list, LLNode is used to go on to the next node. In a way. linked lists work like recursion, but it doesn't use recursion in it's functions.
To walk through a linked list:
cur_node = self.front
while <some condition here...>:
# do something here...
cur_node = cur_node.nxt
Now, you can apply this formula when you need to walk through the linked list using a function.
Friday, 6 March 2015
Week 7: Assignments and Binary trees
For assignment 2, we have to write code for a game called tippy. Tippy uses a similar game view as tic-tac-toe. To win the game, you must form a z shape using x's or o's in any form of rotation. We create a strategy called minimax that chooses the best possible moves to win the game. Minimax is a recursive code which goes lists all the best moves from all the moves available. It assumes the opponent to also use the best possible moves, so minimax must choose the moves that will put the opponent in a losing position.We use recursion so minimax can look at all the game states not matter what game it is and choose the game state that will give the optimal score. In our case, the score will be 1 (for a win), 0 (for a tie), -1 (for a loss).
Lately, in class, we have been learning about trees. What are trees? A tree has set of nodes with directed edges between some pairs of nodes. One node is called a root. In trees, there are children and every child has exactly one parent. A leaf is a node with no children. An internal node is a node with one or more children. Many functions applied to the tree uses recursion to access the children.
general form of recursion:
if condition to detect a base case:
do something without recursion
else: # general case
do something that involves recursive call(s)
Usually, the base case occurs when there is a leaf and the general case will keep going through the tree until it hits the leaf. Recursion is extremely helpful in trees.
general form of recursion:
if condition to detect a base case:
do something without recursion
else: # general case
do something that involves recursive call(s)
Usually, the base case occurs when there is a leaf and the general case will keep going through the tree until it hits the leaf. Recursion is extremely helpful in trees.
Tuesday, 3 March 2015
Week 6: Object Oriented Programming Summary
For the past few weeks, we have learned a lot of the basic concepts of object oriented programming.
Classes
Classes was a concept also learned in CSC108. A class declaration is creates a new class of creating objects of that type. A class can contain various methods and different attributes. Lets use an example of the class Dog.
class Dog:
"""A class that contains the dog's name, age and species
"""
def __init__(self, name, age, species ):
""" (Dog, str, int, str) -> NoneType
Initialize the dog's name, age in years and the species
"""
self.name = name # instance variable unique to each instance
self.age = age
self.species = species
We first initialize our class by using the __init__ method. We initialize the dog's name, age and the species. The parameter self refers to the instance of the class. How does it work?
For example:
>>> dog = Dog('Max', 3, 'Poodle')
>>> dog.age
3
The self refers to the variable dog. We can use the self.name, self.age and self.species in our methods we create.
Methods
What types of properties does the dog class possess?
When it is the dog's birthday, the dog grows a year older. How do we change it's age? We can use a function to indicate that it's the dog's birthday and add a year to it's age.
def birthday(self):
""" (Dog) -> NoneType
Add a year to the dog's age
"""
self.age += 1
Now if we call birthday on dog, it will add an additional year to the dog.
>>> dog.birthday()
>>> dog.age = 4
A special case of methods are __str__, __eq__, and __repr__. __str__ can help create a string representation for the class. __eq__ can compare if two objects of the same class have the same properties, and __repr__ returns a str that is in Python code form which is easy to read for Python.
Inheritance
Perhaps we want to create a general class not only for dogs, but for all pets. This general class can be used for specific classes on specific animals. All pets have a name, age, and species so we can create a general class called Pet.
class Pet:
"""A class that contains the pet's name, age and species
"""
def __init__(self, name, age, species ):
""" (Pet, str, int, str) -> NoneType
Initialize the pet's name, age in years and the species
"""
self.name = name # instance variable unique to each instance
self.age = age
self.species = species
The class Dog can inherit all the properties of Pet.
class Dog(Pet):
"""A class that contains the dog's name, age and species
"""
def __init__(self, name, age, species ):
""" (Dog, str, int, str) -> NoneType
Initialize the dog's name, age in years and the species
"""
Pet.__init__(name, age, species)
Now the class Dog inherits all the attributes of the class Pet. You can do that with various other classes of pets such as cats, fishes, or even create a pet of your own!
Object oriented programming is very flexible and one can create a lot of different programs.
Subscribe to:
Posts (Atom)