예제 #1
0
 def test_Right(self):
     tree = BinaryTree()
     root = tree.add_root(BinaryTreeNode(5))
     self.create_binary_search_tree(tree, [3, 1, 2, 7, 6])
     # tree.Display()
     assert tree.right(root) != None
     assert tree.right(root).val == 7
 def test_Right(self):
     tree = BinaryTree()
     root = tree.add_root(BinaryTreeNode(5))
     self.create_binary_search_tree(tree, [3, 1, 2, 7, 6])
     # tree.Display()
     assert tree.right(root) != None
     assert tree.right(root).val == 7
예제 #3
0
    def test_First(self):
        tree = BinaryTree()
        root = tree.add_root(BinaryTreeNode(1))
        for i in range(2, 10):
            node = BinaryTreeNode(i)
            current = root
            while current.left != None:
                current = current.left
            current = tree.add_left(current, node)

        assert tree.first().val == 9
    def test_First(self):
        tree = BinaryTree()
        root = tree.add_root(BinaryTreeNode(1))
        for i in range(2,10):
            node = BinaryTreeNode(i)
            current = root
            while current.left != None:
                current = current.left
            current = tree.add_left(current, node)

        assert tree.first().val == 9
        def runner(current):
            # loop to remove every link that larger than average
            sum_weight, edges = mst_graph.sum_weight(current.val[0])
            # terminate when there is not edge to go
            if len(edges) is 0:
                return
            # sort weight desc
            edges.sort(key=lambda x: -x[2])
            avg_weight = sum_weight / len(edges)

            btree = BinaryTree()
            btree.add_root(BinaryTreeNode(current.val))
            left = btree.root
            right = None
            for edge in edges:
                # remove this link
                if edge[2] >= avg_weight:
                    # remove the link
                    mst_graph.double_unlink(edge[0], edge[1])
                    # add this link to the binary tree
                    if edge[0] in left.val:
                        parent = left
                    else:
                        parent = right
                    left = btree.add_left(parent, BinaryTreeNode(mst_graph.connected_with(edge[0])))
                    right = btree.add_right(parent, BinaryTreeNode(mst_graph.connected_with(edge[1])))
                # else or the last one
                if edge[2] < avg_weight or edge == edges[-1]:
                    # groups are the display output of the btree
                    groups = btree.leaves()
                    for group in groups:
                        new_node = MultiTreeNode(group)
                        # add new group to the leat of the multi tree
                        tree.add_child(current, new_node)
                        # recursively run it
                        runner(new_node)
                    # if the link's weight has become smaller than avg_weight,
                    # there is no need to keep going on
                    break
        def runner(current):
            # loop to remove every link that larger than average
            sum_weight, edges = mst_graph.sum_weight(current.val[0])
            # terminate when there is not edge to go
            if len(edges) is 0:
                return
            # sort weight desc
            edges.sort(key=lambda x: -x[2])
            avg_weight = sum_weight / len(edges)

            btree = BinaryTree()
            btree.add_root(BinaryTreeNode(current.val))
            for edge in edges:
                # remove this link
                if edge[2] >= avg_weight:
                    # remove the link
                    mst_graph.double_unlink(edge[0], edge[1])
                    # add this link to the binary tree
                    parent = btree.find(edge[0])
                    # if edge[0] in left.val:
                    #     parent = left
                    # else:
                    #     parent = right
                    left = btree.add_left(parent, BinaryTreeNode(mst_graph.connected_with(edge[0])))
                    right = btree.add_right(parent, BinaryTreeNode(mst_graph.connected_with(edge[1])))
                # else or the last one
                if edge[2] < avg_weight or edge == edges[-1]:
                    # groups are the display output of the btree
                    groups = btree.leaves()
                    for group in groups:
                        new_node = MultiTreeNode(group)
                        # add new group to the leat of the multi tree
                        tree.add_child(current, new_node)
                        # recursively run it
                        runner(new_node)
                    # if the link's weight has become smaller than avg_weight,
                    # there is no need to keep going on
                    break
예제 #7
0
    def test_find(self):
        tree = BinaryTree()
        root = tree.add_root(BinaryTreeNode([1, 2, 3, 4, 5]))
        a = tree.add_left(root, BinaryTreeNode([1, 2, 3]))
        b = tree.add_right(a, BinaryTreeNode([3]))
        a = tree.add_left(a, BinaryTreeNode([1, 2]))
        b = tree.add_right(a, BinaryTreeNode([2]))
        a = tree.add_left(a, BinaryTreeNode([1]))
        b = tree.add_right(root, BinaryTreeNode([4, 5]))
        a = tree.add_left(b, BinaryTreeNode([4]))
        b = tree.add_right(b, BinaryTreeNode([5]))

        assert tree.find(3).val == [3]
        assert tree.find(4).val == [4]
    def test_AddLeft(self):
        tree = BinaryTree()
        root = tree.add_root( BinaryTreeNode(1) )

        # complex adding case, because normal ones are covered with test_add()
        left = tree.add_left(root, BinaryTreeNode(2))
        assert root.right == None
        assert root.left.val == 2
        right = tree.add_right(root, BinaryTreeNode(3))

        tmp = tree.add_left(root, BinaryTreeNode(4))
        assert tmp.parent == root
        assert root.left == tmp
        assert tmp.left == left
        assert tmp.right == None
        assert root.right == right
예제 #9
0
    def test_AddLeft(self):
        tree = BinaryTree()
        root = tree.add_root(BinaryTreeNode(1))

        # complex adding case, because normal ones are covered with test_add()
        left = tree.add_left(root, BinaryTreeNode(2))
        assert root.right == None
        assert root.left.val == 2
        right = tree.add_right(root, BinaryTreeNode(3))

        tmp = tree.add_left(root, BinaryTreeNode(4))
        assert tmp.parent == root
        assert root.left == tmp
        assert tmp.left == left
        assert tmp.right == None
        assert root.right == right
    def test_AddRight(self):
        tree = BinaryTree()
        root = tree.add_root( BinaryTreeNode(1) )

        #complex adding case, becasue nromal ones are covered with test_add()
        leftNode = BinaryTreeNode(2)
        rightNode = BinaryTreeNode(3)
        right = tree.add_right(root, rightNode)
        assert root.right == rightNode
        assert root.left == None
        left = tree.add_left(root, leftNode)

        tmp = tree.add_right(root, BinaryTreeNode(4))
        assert tmp.parent == root
        assert root.right == tmp
        assert tmp.left == None
        assert tmp.right == right
        assert root.left == left
예제 #11
0
    def test_AddRight(self):
        tree = BinaryTree()
        root = tree.add_root(BinaryTreeNode(1))

        #complex adding case, becasue nromal ones are covered with test_add()
        leftNode = BinaryTreeNode(2)
        rightNode = BinaryTreeNode(3)
        right = tree.add_right(root, rightNode)
        assert root.right == rightNode
        assert root.left == None
        left = tree.add_left(root, leftNode)

        tmp = tree.add_right(root, BinaryTreeNode(4))
        assert tmp.parent == root
        assert root.right == tmp
        assert tmp.left == None
        assert tmp.right == right
        assert root.left == left
 def test_Left(self):
     tree = BinaryTree()
     root = tree.add_root(BinaryTreeNode(5))
     self.create_binary_search_tree(tree, [3, 1, 2, 7, 6])
     assert tree.left(root) != None
     assert tree.left(root).val == 3
 def test_leaves(self):
     tree = BinaryTree()
     root = tree.add_root(BinaryTreeNode(5))
     self.create_binary_search_tree(tree, [3,1,4,7,6,8])
     assert tree.leaves() == [1, 4, 6, 8]
 def test_inorder(self):
     tree = BinaryTree()
     root = tree.add_root(BinaryTreeNode(5))
     self.create_binary_search_tree(tree, [3, 1, 2, 7, 6])
     assert tree.inorder() == [1, 2, 3, 5, 6, 7]
예제 #15
0
 def test_inorder(self):
     tree = BinaryTree()
     root = tree.add_root(BinaryTreeNode(5))
     self.create_binary_search_tree(tree, [3, 1, 2, 7, 6])
     assert tree.inorder() == [1, 2, 3, 5, 6, 7]
예제 #16
0
 def test_leaves(self):
     tree = BinaryTree()
     root = tree.add_root(BinaryTreeNode(5))
     self.create_binary_search_tree(tree, [3, 1, 4, 7, 6, 8])
     assert tree.leaves() == [1, 4, 6, 8]
예제 #17
0
 def test_Left(self):
     tree = BinaryTree()
     root = tree.add_root(BinaryTreeNode(5))
     self.create_binary_search_tree(tree, [3, 1, 2, 7, 6])
     assert tree.left(root) != None
     assert tree.left(root).val == 3
 def test_AddRoot(self):
     tree = BinaryTree()
     root = tree.add_root(BinaryTreeNode(1))
     assert root == tree.root
     assert root.parent == None
     assert root.children == (None, None)
예제 #19
0
 def test_AddRoot(self):
     tree = BinaryTree()
     root = tree.add_root(BinaryTreeNode(1))
     assert root == tree.root
     assert root.parent == None
     assert root.children == (None, None)