Beispiel #1
0
class TestInOrderWParent(unittest.TestCase):
    def setUp(self):
        self.root = MyBinTreeWithParent(1)
        self.left_child = MyBinTreeWithParent(1)
        self.left_child.put_left(2)
        self.children = MyBinTreeWithParent(1)
        self.children.put_left(2)
        self.children.put_right(3)
        self.tree = MyBinTreeWithParent(1)
        self.tree.put_left(2)
        self.tree.put_right(3)
        self.tree.left.put_left(4)
        self.tree.left.put_right(5)
        self.big_tree = MyBinTreeWithParent(1)
        self.big_tree.put_left(2)
        self.big_tree.put_right(3)
        self.big_tree.left.put_left(4)
        self.big_tree.left.put_right(5)
        self.big_tree.right.put_right(6)
        self.big_tree.left.left.put_right(7)
        #      1
        #     / \
        #    2   3
        #   / \   \
        #  4   5   6
        #   \
        #    7

    def test_leftmost_child(self):
        aa = leftmost_child(self.big_tree)
        self.assertEqual(leftmost_child(self.big_tree), self.big_tree.left.left, 'leftmost child of root')
        self.assertEqual(leftmost_child(self.big_tree.left), self.big_tree.left.left, 'leftmost child of root.left')
        self.assertEqual(leftmost_child(self.big_tree.right), self.big_tree.right, 'leftmost child of root.right')

    def test_next_inorder_node(self):
        self.assertEqual(next_inorder_node(self.root), None, 'root')
        self.assertEqual(next_inorder_node(self.big_tree), self.big_tree.right, 'root of big tree')
        self.assertEqual(next_inorder_node(self.big_tree.left.left), self.big_tree.left.left.right, 'left-left')
        self.assertEqual(next_inorder_node(self.big_tree.left.left.right), self.big_tree.left, 'left-left-right')
        self.assertEqual(next_inorder_node(self.big_tree.left.right), self.big_tree, 'left-right')
        self.assertEqual(next_inorder_node(self.big_tree.right.right), None, 'right-right')

    def test_empty(self):
        self.assertEqual(inorder_w_parent(None), [], 'None')

    def test_is_left_child(self):
        self.assertEqual(is_left_child(self.big_tree.left), 1)
        self.assertEqual(is_left_child(self.big_tree.left.left), 1)
        self.assertEqual(is_left_child(self.big_tree.right), 0)
        self.assertEqual(is_left_child(self.big_tree.left.right), 0)

    def test_many(self):
        self.assertEqual(inorder_w_parent(self.root), [1], 'just root')
        self.assertEqual(inorder_w_parent(self.left_child), [2, 1], 'root with left child')
        self.assertEqual(inorder_w_parent(self.children), [2, 1, 3], 'root with two children')
        self.assertEqual(inorder_w_parent(self.tree), [4, 2, 5, 1, 3], 'big tree')
        self.assertEqual(inorder_w_parent(self.big_tree), [4, 7, 2, 5, 1, 3, 6], 'even bigger tree')
Beispiel #2
0
class TestLCAFast(unittest.TestCase):
    def setUp(self):
        self.t = MyBinTreeWithParent(1)
        self.t.put_left(2)
        self.t.put_right(3)
        self.t.left.put_left(4)
        self.t.left.left.put_left(5)
        self.t.left.put_right(6)
        self.t.right.put_left(7)
        self.t.right.put_right(8)

    def test_one(self):
        self.assertEqual(lca_fast(self.t,self.t), self.t, 'just one node')
        self.assertEqual(lca_fast(self.t.left,self.t.left), self.t.left, 'same nodes')

    def test_many(self):
        self.assertEqual(lca_fast(self.t.left,self.t.right), self.t, 'left and right')
        self.assertEqual(lca_fast(self.t.left.left,self.t.right), self.t, 'left.left, right')
        self.assertEqual(lca_fast(self.t.left.right,self.t.left), self.t.left, 'left.right, left')
        self.assertEqual(lca_fast(self.t.left.left.left,self.t.left.right), self.t.left, 'left.left.left, left.right')
        self.assertEqual(lca_fast(self.t.left.left.left,self.t.right), self.t, 'left.left.left, right')
Beispiel #3
0
 def test_many(self):
     t = MyBinTreeWithParent(1)
     self.assertEqual(inorder_w_parent(t), [1], 'just root')
     t.put_left(2)
     self.assertEqual(inorder_w_parent(t), [2, 1], 'root with left child')
     t.put_right(3)
     self.assertEqual(inorder_w_parent(t), [2, 1, 3], 'root with two children')
     #      1
     #     / \
     #    2   3
     #   / \   \
     #  4   5   6
     #   \
     #    7
     t.left.put_left(4)
     t.left.put_right(5)
     self.assertEqual(inorder_w_parent(t), [4, 2, 5, 1, 3], 'big tree')
     t.right.put_right(6)
     self.assertEqual(inorder_w_parent(t), [4, 2, 5, 1, 3, 6], 'bigger tree')
     t.left.left.put_right(7)
     self.assertEqual(inorder_w_parent(t), [4, 7, 2, 5, 1, 3, 6], 'even bigger tree')