Skip to content

Instantly share code, notes, and snippets.

@cryptohedge
Created May 10, 2014 07:20
Show Gist options
  • Select an option

  • Save cryptohedge/139e2a1162b0dd1bc632 to your computer and use it in GitHub Desktop.

Select an option

Save cryptohedge/139e2a1162b0dd1bc632 to your computer and use it in GitHub Desktop.

Revisions

  1. @gtrsh gtrsh created this gist May 10, 2014.
    47 changes: 47 additions & 0 deletions lab04-btree.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    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())