示例#1
0
文件: 4_9.py 项目: columkeane/ctci
    def test_case_0(self):
        """
            2
        1       3
        """
        btnroot = Node(2)
        btn1 = Node(1)
        btn3 = Node(3)
        btnroot.left = btn1
        btnroot.right = btn3

        for row in bst_sequences(btnroot):
            print(row)
示例#2
0
文件: 4_9.py 项目: columkeane/ctci
    def test_case_1(self):
        """
        0
            1
                2

        """
        btnroot = Node(0)
        btn1 = Node(1)
        btn2 = Node(2)
        btnroot.right = btn1
        btn1.right = btn2

        for row in bst_sequences(btnroot):
            print(row)
示例#3
0
文件: 4_8.py 项目: columkeane/ctci
    def test_solution(self):
        """
                                    0
                    1                               2

            3               4               5              6

        7       8       9       10      11      12      13      14

        """

        nodes = [Node(i) for i in range(15)]

        for n in range(7):
            nodes[n].left = nodes[2 * n + 1]
            nodes[n].right = nodes[2 * n + 2]

        input_values = [(0, 1), (0, 2), (7, 8), (1, 2), (7, 14), (3, 11),
                        (7, 10)]
        expected_values = [0, 0, 3, 0, 0, 0, 1]

        for value, expected in zip(input_values, expected_values):
            with self.subTest(nodes=(value), answer=expected):
                self.assertEqual(
                    first_common_ancestor(nodes[0], nodes[value[0]],
                                          nodes[value[1]]).element,
                    expected,
                )
示例#4
0
文件: 4_5.py 项目: calvh/ctci
    def test_is_bst_1(self):
        """
                                    7
                    3                               8

            2               4

        1

        """
        bst_tree = BST()

        for n in [7, 3, 8, 2, 4, 1]:
            bst_tree.add(Node(n))

        self.assertEqual(tree_is_bst(bst_tree), True)
示例#5
0
文件: 4_5.py 项目: calvh/ctci
    def test_is_bst_0(self):
        """
                                    0
                    1                               2

            3               4               5              6

        7       8       9       10      11      12      13      14

        """
        nodes = [Node(i) for i in range(15)]

        for n in range(7):
            nodes[n].left = nodes[2 * n + 1]
            nodes[n].right = nodes[2 * n + 2]

        not_bst = BT(nodes[0])

        self.assertEqual(tree_is_bst(not_bst), False)
示例#6
0
    def test_is_balanced_1(self):
        """
                                    0
                    1                               2

            3               4

        7       8       9       10

        """
        new_nodes = [Node(i) for i in range(15)]

        for n in [0, 1, 3, 4]:
            new_nodes[n].left = new_nodes[2 * n + 1]
            new_nodes[n].right = new_nodes[2 * n + 2]

        unbalanced_tree = Tree(new_nodes[0])

        self.assertEqual(tree_is_balanced(unbalanced_tree), False)
示例#7
0
    def setUp(self):
        """
                                    0
                    1                               2

            3               4               5              6

        7       8       9       10      11      12      13      14

        """
        nodes = [Node(i) for i in range(15)]

        for n in range(7):
            nodes[n].left = nodes[2 * n + 1]
            nodes[n].right = nodes[2 * n + 2]

        tree = Tree(nodes[0])

        self.nodes = nodes
        self.tree = tree
示例#8
0
文件: 4_9.py 项目: columkeane/ctci
    def test_case_2(self):
        """
                    5
            2               8
        1       4
        """
        btnroot = Node(5)
        btn1 = Node(1)
        btn2 = Node(2)
        btn4 = Node(4)
        btn5 = Node(5)
        btn8 = Node(8)
        btnroot.left = btn2
        btnroot.right = btn8
        btn2.left = btn1
        btn2.right = btn4

        for row in bst_sequences(btnroot):
            print(row)
示例#9
0
文件: 4_9.py 项目: columkeane/ctci
    def test_case_3(self):
        """
                    4
            2               6
        1       3       5       7

        """

        btnroot = Node(4)
        btn1 = Node(1)
        btn2 = Node(2)
        btn3 = Node(3)
        btn5 = Node(5)
        btn6 = Node(6)
        btn7 = Node(7)
        btnroot.left = btn2
        btnroot.right = btn6
        btn2.left = btn1
        btn2.right = btn3
        btn6.left = btn5
        btn6.right = btn7

        for row in bst_sequences(btnroot):
            print(row)