Ejemplo n.º 1
0
    def test_postfix_tokens(self):
        tree = BinaryTree()
        root = BinaryTreeNode('*')
        tree.root = root

        left = root.add_left('+')
        right = root.add_right('+')
        left.add_left('A')
        left.add_right('B')
        right.add_left('C')
        right.add_right('D')

        tokens = ['A', 'B', 'C', tree.root.label, '+', 'D', '+']
        result = tree.postfix_tokens()
        self.assertCountEqual(result, tokens)

        tree = BinaryTree()
        root = BinaryTreeNode('+')
        tree.root = root

        left = root.add_left('+')
        right = root.add_right('+')
        left.add_left('A')
        left.add_right('B')
        right.add_left('C')
        right.add_right('D')

        tokens = ['A', 'B', '+', 'C', '+', 'D', '+']
        result = tree.postfix_tokens()
        self.assertCountEqual(result, tokens)
Ejemplo n.º 2
0
    def _swap_subtrees(node_1: BinaryTreeNode, node_2: BinaryTreeNode,
                       tree_1: BinaryTree, tree_2: BinaryTree) -> None:
        """Swap parents and children of nodes.


        :param node_1:
        :param node_2:
        :param tree_1: tree corresponding to node 1
        :param tree_2: tree corresponding to node 2
        :return:
        """
        if node_1 == node_2:
            return

        if node_1 is None or node_2 is None:
            return

        if not node_1.has_parent() and not node_2.has_parent():
            return

        if not node_1.has_parent():
            tree_1.root = node_2
            if node_2.is_left_child():
                node_2.parent.left = node_1
            else:
                node_2.parent.right = node_1
        elif not node_2.has_parent():
            tree_2.root = node_1
            if node_1.is_left_child():
                node_1.parent.left = node_2
            else:
                node_1.parent.right = node_2
        else:
            if node_1.is_left_child():
                if node_2.is_left_child():
                    node_2.parent.left, node_1.parent.left = node_1, node_2
                else:
                    node_2.parent.right, node_1.parent.left = node_1, node_2
            else:
                if node_2.is_left_child():
                    node_2.parent.left, node_1.parent.right = node_1, node_2
                else:
                    node_2.parent.right, node_1.parent.right = node_1, node_2

        node_1.parent, node_2.parent = node_2.parent, node_1.parent
Ejemplo n.º 3
0
    def test_height(self):
        tree = BinaryTree()
        self.assertEqual(tree.height(), 0)

        tree.root = BinaryTreeNode('*')
        self.assertEqual(tree.height(), 1)

        left = tree.root.add_left(10)
        self.assertEqual(tree.height(), 2)
        right = tree.root.add_right(20)
        self.assertEqual(tree.height(), 2)

        ll = left.add_left(40)
        self.assertEqual(tree.height(), 3)
        left.add_right(50)
        self.assertEqual(tree.height(), 3)
        right.add_left(60)
        self.assertEqual(tree.height(), 3)
        right.add_right(70)
        self.assertEqual(tree.height(), 3)

        ll.add_left(80)
        self.assertEqual(tree.height(), 4)
Ejemplo n.º 4
0
    def _swap_mut_subtree(tree: BinaryTree,
                          r: int,
                          random_tree: BinaryTree) -> BinaryTree:
        """Add mutated subtree to original tree.

        :param tree:
        :param r:
        :param random_tree:
        :return:
        """
        # swap parents of nodes
        node = tree.select_postorder(r)
        if node.parent:
            if node.parent.left is node:
                node.parent.left = random_tree.root
            elif node.parent.right is node:
                node.parent.right = random_tree.root
            random_tree.root.parent = node.parent
            return tree
        else:
            new_tree = BinaryTree()
            new_tree.root = random_tree.root
            return new_tree
Ejemplo n.º 5
0
 def test_root(self):
     tree = BinaryTree()
     with self.assertRaises(TypeError):
         tree.root = 'bad type'