Exemplo n.º 1
0
def run_examples():
    """
    Create some trees and apply various methods to these trees
    """
    tree_a = Tree("a", [])
    tree_b = Tree("b", [])
    print "Tree consisting of single leaf node labelled 'a'", tree_a
    print "Tree consisting of single leaf node labelled 'b'", tree_b

    tree_cab = Tree("c", [tree_a, tree_b])
    print "Tree consisting of three node", tree_cab

    tree_dcabe = Tree("d", [tree_cab, Tree("e", [])])
    print "Tree consisting of five nodes", tree_dcabe
    print

    my_tree = Tree("a", [
        Tree("b", [Tree("c", []), Tree("d", [])]),
        Tree("e", [Tree("f", [Tree("g", [])]),
                   Tree("h", []),
                   Tree("i", [])])
    ])
    print "Tree with nine nodes", my_tree

    print "The tree has", my_tree.num_nodes(), "nodes,",
    print my_tree.num_leaves(), "leaves and height",
    print my_tree.height()

    import poc_draw_tree
    poc_draw_tree.TreeDisplay(my_tree)
Exemplo n.º 2
0
def run_examples():
    """
    Create some trees and apply various methods to these trees
    """
    tree_a = NavTree("a", [])
    tree_b = NavTree("b", [])
    tree_cab = NavTree("c", [tree_a, tree_b])
    tree_e = NavTree("e", [])
    tree_dcabe = NavTree("d", [tree_cab, tree_e])

    print "This is the main tree -", tree_dcabe
    print "This is tree that contains b -", tree_b.get_root()

    import poc_draw_tree
    poc_draw_tree.TreeDisplay(tree_dcabe)

    print "The node b has depth", tree_b.depth()
    print "The node e has depth", tree_e.depth()
Exemplo n.º 3
0
def run_example():
    """
    Create and evaluate some examples of arithmetic expressions
    """

    one = ArithmeticExpression("1", [])
    two = ArithmeticExpression("2", [])
    three = ArithmeticExpression("3", [])
    print one
    print one.evaluate()
    
    one_plus_two = ArithmeticExpression("+", [one, two])
    print one_plus_two
    print one_plus_two.evaluate()
    
    one_plus_two_times_three = ArithmeticExpression("*", [one_plus_two, three])
    print one_plus_two_times_three
    
    import poc_draw_tree
    poc_draw_tree.TreeDisplay(one_plus_two_times_three)
    print one_plus_two_times_three.evaluate()