Пример #1
0
    def test_traverse(self):
        #
        # Binary Tree
        #
        # Standard
        node = Tree(n_children=2)
        node.split()
        node.get_child(0).split()
        node.get_child(0).get_child(1).remove()
        addresses = {
            'breadth-first': [[], [0], [1], [0, 0]],
            'depth-first': [[], [0], [0, 0], [1]]
        }

        for mode in ['depth-first', 'breadth-first']:
            count = 0
            for leaf in node.traverse(mode=mode):
                self.assertEqual(leaf.get_node_address(),\
                                 addresses[mode][count],\
                                 'Binary tree traversal incorrect.')
                count += 1

        #
        # Quadtree
        #
        node = Tree(n_children=4)
        node.split()
        node.get_child(1).split()
        node.get_child(1).get_child(2).remove()
        addresses = [[], [0], [1], [2], [3], [1, 0], [1, 1], [1, 3]]
        count = 0
        for n in node.traverse(mode='breadth-first'):
            self.assertEqual(n.get_node_address(), addresses[count],\
                             'Incorrect address.')
            count += 1
Пример #2
0
    def test_traverse(self):
        #
        # Binary Tree
        #
        # Standard
        node = Tree(2)
        forest = Forest([node])

        node.split()
        node.get_child(0).split()
        node.get_child(0).get_child(1).remove()
        addresses = {
            'breadth-first': [[0], [0, 0], [0, 1], [0, 0, 0]],
            'depth-first': [[0], [0, 0], [0, 0, 0], [0, 1]]
        }

        for mode in ['depth-first', 'breadth-first']:
            count = 0
            for leaf in forest.traverse(mode=mode):
                self.assertEqual(leaf.get_node_address(),
                                 addresses[mode][count]),

                count += 1
        #
        # QuadTree
        #
        node = Tree(4)
        forest = Forest([node])
        node.split()
        node.get_child(1).split()
        node.get_child(1).get_child(2).remove()
        addresses = [[0], [0, 0], [0, 1], [0, 2], [0, 3], [0, 1, 0], [0, 1, 1],
                     [0, 1, 3]]
        count = 0
        for n in node.traverse(mode='breadth-first'):
            self.assertEqual(n.get_node_address(), addresses[count],\
                             'Incorrect address.')
            count += 1

        #
        # Forest with one quadtree and one bitree
        #
        bi = Tree(2)
        quad = Tree(4)
        forest = Forest([bi, quad])
        bi.split()
        bi.get_child(0).split()
        quad.split()
        addresses = {
            'breadth-first': [[0], [1], [0, 0], [0, 1], [1, 0], [1, 1], [1, 2],
                              [1, 3], [0, 0, 0], [0, 0, 1]],
            'depth-first': [[0], [0, 0], [0, 0, 0], [0, 0, 1], [0, 1], [1],
                            [1, 0], [1, 1], [1, 2], [1, 3]]
        }

        for mode in ['depth-first', 'breadth-first']:
            count = 0
            for leaf in forest.traverse(mode=mode):
                self.assertEqual(leaf.get_node_address(),
                                 addresses[mode][count])

                count += 1
Пример #3
0
for c in tree.get_children():
    c.split()

    for cc in c.get_children():
        cc.split()

print(tree.get_depth())

leaves = tree.get_leaves()
leaves.pop()

w = 0
max_depth = 0
for leaf in leaves:
    ld = leaf.get_depth()
    w += 2**(-leaf.get_depth())
    if ld > max_depth:
        max_depth = ld
h = 2**(-max_depth)

fig, ax = plt.subplots()

for node in tree.traverse(mode='breadth-first'):

    ax.plot([0, h / 4], [0, 0], 'k')
    ax.plot([h / 4, h / 4], [-w, w], 'k')
    ax.plot([h / 4, h / 2], [-w, -w], 'k')
    ax.plot([h / 4, h / 2], [w, w], 'k')
    ax.plot([h / 2, h / 2], [-w - w / 2, -w + w / 2], 'k')
    ax.plot([h / 2, h / 2], [w - w / 2, w + w / 2], 'k')
    ax.plot([h / 2, h / 2 + h / 8], [w - w / 2, w - w / 2], 'k')