예제 #1
0
    def test_level_order(self):
        t = Tree('r')
        r = t.root()
        a = t.add(r, 'a')
        b = t.add(a, 'b')
        c = t.add(a, 'c')
        d = t.add(a, 'd')
        e = t.add_between(r, a, 'e')
        f = t.add(e, 'f')

        self.assertEqual([[x.element() for x in level]
                          for level in t.level_traversal()],
                         [['r'], ['e'], ['a', 'f'], ['b', 'c', 'd']])
예제 #2
0
    def test_add_between(self):

        t = Tree('r')
        r = t.root()
        a = t.add(r, 'a')
        b = t.add(a, 'b')
        c = t.add(a, 'c')
        d = t.add(a, 'd')
        e = t.add_between(r, a, 'e')
        f = t.add(e, 'f')
        self.assertEqual([x.element() for x in t.bfs()],
                         ['r', 'e', 'a', 'f', 'b', 'c', 'd'])
        self.assertEqual(len(t), 7)
        self.assertEqual(t.height(), 3)
예제 #3
0
from tree.Tree import Tree

__author__ = 'lijiayan'

if __name__ == '__main__':
    tree = Tree()
    tree.add(1)
    tree.add(2)
    tree.add(3)
    tree.add(4)
    tree.add(5)
    tree.add(6)

    tree.breadth_travel()
    print("===========")
    tree.preOrder(tree.root)