"""
A BinaryTree class implemented with node/reference technique.
Illustrates 3 types of tree traversal.
"""classBinaryTree:def__init__(self,rootObj):self.key=rootObjself.leftChild=Noneself.rightChild=NonedefinsertLeft(self,newNode):ifself.leftChild==None:self.leftChild=BinaryTree(newNode)else:t=BinaryTree(newNode)t.leftChild=self.leftChildself.leftChild=tdefinsertRight(self,newNode):ifself.rightChild==None:self.rightChild=BinaryTree(newNode)else:t=BinaryTree(newNode)t.rightChild=self.rightChildself.rightChild=tdefgetRightChild(self):returnself.rightChilddefgetLeftChild(self):returnself.leftChilddefsetRootVal(self,obj):self.key=objdefgetRootVal(self):returnself.keydefpreorder(self):print(self.key),ifself.leftChild:self.leftChild.preorder()ifself.rightChild:self.rightChild.preorder()definorder(self):ifself.leftChild:self.leftChild.inorder()print(self.key),ifself.rightChild:self.rightChild.inorder()defpostorder(self):ifself.leftChild:self.leftChild.postorder()ifself.rightChild:self.rightChild.postorder()print(self.key),if__name__=='__main__':root=BinaryTree('g')root.insertLeft('u')root.insertRight('o')subtree_left=root.getLeftChild()subtree_left.insertLeft('i')subtree_left.insertRight('d')subtree_right=root.getRightChild()subtree_right.insertLeft('p')subtree_right.insertRight('y')############################## g ## ____|____ ## u o ## __|__ __|__ ## i d p y ##############################print'\n Pre Order ==>\t ',root.preorder()print'\n In Order ==>\t ',root.inorder()print'\nPost Order ==>\t ',root.postorder()
Python primer (variables, conditionals, iteration)
mutability/immutability
scope and namespace
pass by value vs. pass by reference
review homework 1
Note on mutability/immutability and pass-by-value/pass-by-reference.
FACT 1: Python passes all arguments by reference.
FACT 2: Data types in python are either mutable or immutable.
GIVES THE FOLLOWING CONCLUSIONS:
If an immutable type is passed as an argument to a function, it is passed by reference. However, because the argument is immutable it cannot be reassigned a value. Thus, any modification to the argument is necessarily a COPY. This effectively indicates that immutable types are passed by value.
If a mutable type is passed as an argument to a function, it is passed by reference. Therefore, it can be modified directly. This follows the behavior consistent with what we mean by pass-by-reference.
2017-01-18 Week 1
Readings
DSAP (Data Structures and Algorithms in Python) ==> CHAPTER 1
Topics
course introduction and overview
Python primer 1 (variables, conditionals, iteration)