def test_insert_right(self):
     t1 = BinaryTree("cypress")
     t1.insert_right("right branch")
     self.assertEquals(t1.rc.key, "right branch")
     t1.insert_right("new branch")
     self.assertEquals(t1.rc.key, "new branch")
     self.assertEquals(t1.rc.rc.key, "right branch")
 def test_get(self):
     t1 = BinaryTree("pine")
     t1.insert_right("right branch")
     t1.insert_left("left branch")
     self.assertEquals(t1.get_rc(), "right branch")
     self.assertEquals(t1.get_lc(), "left branch")
     self.assertEquals(t1.get_root(), "pine")
     t1.insert_right("right stick")
     t1.insert_left("left stick")
     t1.set_root("hemlock")
     self.assertEquals(t1.get_rc(), "right stick")
     self.assertEquals(t1.get_lc(), "left stick")
     self.assertEquals(t1.get_root(), "hemlock")
# 07.20.2016
# @totallygloria



from BinaryTreeClass import BinaryTree

def preorder(tree):

    if tree:
        print tree.get_root()
        preorder(tree.get_lc())
        preorder(tree.get_rc())


def postorder(tree):

    if tree is not None:
        postorder(tree.get_lc())
        postorder(tree.get_rc())
        print tree.get_root()


oak = BinaryTree("A")
oak.insert_left("B")
oak.insert_right("C")
oak.lc.insert_left("D")
oak.lc.insert_right("E")

preorder(oak)       # appears to work
postorder(oak)      # appears to work
Exemple #4
0
# Algorithms and Data Structures: Preorder Traversal of Binary Tree
# 07.20.2016
# @totallygloria

from BinaryTreeClass import BinaryTree


def preorder(tree):

    if tree:
        print tree.get_root()
        preorder(tree.get_lc())
        preorder(tree.get_rc())


def postorder(tree):

    if tree is not None:
        postorder(tree.get_lc())
        postorder(tree.get_rc())
        print tree.get_root()


oak = BinaryTree("A")
oak.insert_left("B")
oak.insert_right("C")
oak.lc.insert_left("D")
oak.lc.insert_right("E")

preorder(oak)  # appears to work
postorder(oak)  # appears to work