Created
May 10, 2014 07:20
-
-
Save cryptohedge/139e2a1162b0dd1bc632 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class BinaryTree: | |
| def __init__(self,rootObj): | |
| self.key = rootObj | |
| self.leftChild = None | |
| self.rightChild = None | |
| def insertLeft(self,newNode): | |
| if self.leftChild == None: | |
| self.leftChild = BinaryTree(newNode) | |
| else: | |
| t = BinaryTree(newNode) | |
| t.leftChild = self.leftChild | |
| self.leftChild = t | |
| def insertRight(self,newNode): | |
| if self.rightChild == None: | |
| self.rightChild = BinaryTree(newNode) | |
| else: | |
| t = BinaryTree(newNode) | |
| t.rightChild = self.rightChild | |
| self.rightChild = t | |
| def getRightChild(self): | |
| return self.rightChild | |
| def getLeftChild(self): | |
| return self.leftChild | |
| def setRootVal(self,obj): | |
| self.key = obj | |
| def getRootVal(self): | |
| return self.key | |
| r = BinaryTree('a') | |
| print(r.getRootVal()) | |
| print(r.getLeftChild()) | |
| r.insertLeft('b') | |
| print(r.getLeftChild()) | |
| print(r.getLeftChild().getRootVal()) | |
| r.insertRight('c') | |
| print(r.getRightChild()) | |
| print(r.getRightChild().getRootVal()) | |
| r.getRightChild().setRootVal('hello') | |
| print(r.getRightChild().getRootVal()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment