Beispiel #1
0
    def test_rank(self):
        """ Following tree structure:
                (x)
               /   \
             (t)   (y)
            /  \      \
          (r)  (u)    (z)
          / \    \
        (q) (s)  (v)

        This will yield: [q, r, s, t, u, v, x, y, z]
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        u_node = x_node.insert('u')
        v_node = x_node.insert('v')
        r_node = x_node.insert('r')
        s_node = x_node.insert('s')
        q_node = x_node.insert('q')

        self.assertEqual(q_node.rank(), 0, 'q is on index 0')
        self.assertEqual(r_node.rank(), 1, 'r is on index 1')
        self.assertEqual(s_node.rank(), 2, 's is on index 2')
        self.assertEqual(t_node.rank(), 3, 't is on index 3')
        self.assertEqual(u_node.rank(), 4, 'u is on index 4')
        self.assertEqual(v_node.rank(), 5, 'v is on index 5')
        self.assertEqual(x_node.rank(), 6, 'x is on index 6')
        self.assertEqual(y_node.rank(), 7, 'y is on index 7')
        self.assertEqual(z_node.rank(), 8, 'z is on index 8')
    def test_rank(self):
        """ Following tree structure:
                (x)
               /   \
             (t)   (y)
            /  \      \
          (r)  (u)    (z)
          / \    \
        (q) (s)  (v)

        This will yield: [q, r, s, t, u, v, x, y, z]
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        u_node = x_node.insert('u')
        v_node = x_node.insert('v')
        r_node = x_node.insert('r')
        s_node = x_node.insert('s')
        q_node = x_node.insert('q')

        self.assertEqual(q_node.rank(), 0, 'q is on index 0')
        self.assertEqual(r_node.rank(), 1, 'r is on index 1')
        self.assertEqual(s_node.rank(), 2, 's is on index 2')
        self.assertEqual(t_node.rank(), 3, 't is on index 3')
        self.assertEqual(u_node.rank(), 4, 'u is on index 4')
        self.assertEqual(v_node.rank(), 5, 'v is on index 5')
        self.assertEqual(x_node.rank(), 6, 'x is on index 6')
        self.assertEqual(y_node.rank(), 7, 'y is on index 7')
        self.assertEqual(z_node.rank(), 8, 'z is on index 8')
    def test_select(self):
        """ Following tree structure:
                (x)
               /   \
             (t)   (y)
            /  \      \
          (r)  (u)    (z)
          / \    \
        (q) (s)  (v)

        This will yield: [q, r, s, t, u, v, x, y, z]
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        u_node = x_node.insert('u')
        v_node = x_node.insert('v')
        r_node = x_node.insert('r')
        s_node = x_node.insert('s')
        q_node = x_node.insert('q')

        self.assertEqual(x_node.select(0), q_node, 'q is on index 0')
        self.assertEqual(x_node.select(1), r_node, 'r is on index 1')
        self.assertEqual(x_node.select(2), s_node, 's is on index 2')
        self.assertEqual(x_node.select(3), t_node, 't is on index 3')
        self.assertEqual(x_node.select(4), u_node, 'u is on index 4')
        self.assertEqual(x_node.select(5), v_node, 'v is on index 5')
        self.assertEqual(x_node.select(6), x_node, 'x is on index 6')
        self.assertEqual(x_node.select(7), y_node, 'y is on index 7')
        self.assertEqual(x_node.select(8), z_node, 'z is on index 8')
        self.assertEqual(x_node.select(10), None, 'there is no node on index 10')
        self.assertEqual(x_node.select(-5), None, 'there is no node on index -5')
Beispiel #4
0
    def test_merge(self):
        """ Given two binary search trees check if they are correctly merged:

                (y)       (a)               (c)
               /   \         \             /   \
             (x)   (z)  +    (b)    =   (a)    (y)
                                \         \    / \
                                (c)      (b) (x) (z)
              (first)     (second)        (result)
        """
        first = BinarySearchTreeNode('y')
        first.insert('x')
        first.insert('z')

        second = BinarySearchTreeNode('a')
        second.insert('b')
        second.insert('c')

        result = first.merge(second)

        self.assertEqual(result.key, 'c', 'root is c')
        self.assertEqual(result.left.key, 'a', 'left child of c is a')
        self.assertEqual(result.right.key, 'y', 'right child of c is y')
        self.assertEqual(result.left.right.key, 'b', 'right child of a is b')
        self.assertEqual(result.right.left.key, 'x', 'left child of y is x')
        self.assertEqual(result.right.right.key, 'z', 'right child of y is z')
Beispiel #5
0
    def test_height(self):
        """ Using the following tree:
                    (x)
                   /   \
                 (t)   (y)
                /  \      \
              (r)  (u)    (z)
              / \    \
            (q) (s)  (v)
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        u_node = x_node.insert('u')
        v_node = x_node.insert('v')
        r_node = x_node.insert('r')
        s_node = x_node.insert('s')
        q_node = x_node.insert('q')

        self.assertEqual(x_node.height(), 3, 'x is root so its height is 3')
        self.assertEqual(t_node.height(), 2, 'height of t is 2')
        self.assertEqual(v_node.height(), 0, 'height of leaf v is 0')
        self.assertEqual(r_node.height(), 1, 'x is root so its height is 3')
        self.assertEqual(t_node.height(), 2, 'x is root so its height is 3')
Beispiel #6
0
    def test_common_ancestor(self):
        """ Uses following tree structure:
                (x)
               /   \
             (t)   (y)
            /  \      \
          (r)  (u)    (z)
          / \    \
        (q) (s)  (v)
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        u_node = x_node.insert('u')
        v_node = x_node.insert('v')
        r_node = x_node.insert('r')
        s_node = x_node.insert('s')
        q_node = x_node.insert('q')

        self.assertEqual(t_node.common_ancestor(v_node), t_node,
                         't is ancestor of v')
        self.assertEqual(v_node.common_ancestor(u_node), u_node,
                         'u is parent of v')
        self.assertEqual(q_node.common_ancestor(x_node), x_node, 'x is root')
    def test_height(self):
        """ Using the following tree:
                    (x)
                   /   \
                 (t)   (y)
                /  \      \
              (r)  (u)    (z)
              / \    \
            (q) (s)  (v)
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        u_node = x_node.insert('u')
        v_node = x_node.insert('v')
        r_node = x_node.insert('r')
        s_node = x_node.insert('s')
        q_node = x_node.insert('q')

        self.assertEqual(x_node.height(), 3, 'x is root so its height is 3')
        self.assertEqual(t_node.height(), 2, 'height of t is 2')
        self.assertEqual(v_node.height(), 0, 'height of leaf v is 0')
        self.assertEqual(r_node.height(), 1, 'x is root so its height is 3')
        self.assertEqual(t_node.height(), 2, 'x is root so its height is 3')
    def test_merge(self):
        """ Given two binary search trees check if they are correctly merged:

                (y)       (a)               (c)
               /   \         \             /   \
             (x)   (z)  +    (b)    =   (a)    (y)
                                \         \    / \
                                (c)      (b) (x) (z)
              (first)     (second)        (result)
        """
        first = BinarySearchTreeNode('y')
        first.insert('x')
        first.insert('z')

        second = BinarySearchTreeNode('a')
        second.insert('b')
        second.insert('c')

        result = first.merge(second)

        self.assertEqual(result.key, 'c', 'root is c')
        self.assertEqual(result.left.key, 'a', 'left child of c is a')
        self.assertEqual(result.right.key, 'y', 'right child of c is y')
        self.assertEqual(result.left.right.key, 'b', 'right child of a is b')
        self.assertEqual(result.right.left.key, 'x', 'left child of y is x')
        self.assertEqual(result.right.right.key, 'z', 'right child of y is z')
Beispiel #9
0
    def test_select(self):
        """ Following tree structure:
                (x)
               /   \
             (t)   (y)
            /  \      \
          (r)  (u)    (z)
          / \    \
        (q) (s)  (v)

        This will yield: [q, r, s, t, u, v, x, y, z]
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        u_node = x_node.insert('u')
        v_node = x_node.insert('v')
        r_node = x_node.insert('r')
        s_node = x_node.insert('s')
        q_node = x_node.insert('q')

        self.assertEqual(x_node.select(0), q_node, 'q is on index 0')
        self.assertEqual(x_node.select(1), r_node, 'r is on index 1')
        self.assertEqual(x_node.select(2), s_node, 's is on index 2')
        self.assertEqual(x_node.select(3), t_node, 't is on index 3')
        self.assertEqual(x_node.select(4), u_node, 'u is on index 4')
        self.assertEqual(x_node.select(5), v_node, 'v is on index 5')
        self.assertEqual(x_node.select(6), x_node, 'x is on index 6')
        self.assertEqual(x_node.select(7), y_node, 'y is on index 7')
        self.assertEqual(x_node.select(8), z_node, 'z is on index 8')
        self.assertEqual(x_node.select(10), None,
                         'there is no node on index 10')
        self.assertEqual(x_node.select(-5), None,
                         'there is no node on index -5')
Beispiel #10
0
    def test_get_max(self):
        bst = BinarySearchTreeNode('y', 1)
        bst.insert('x', 2)
        bst.insert('z', 3)

        expected = bst.get_max()
        actual = bst.right
        self.assertEqual(actual, expected, 'produced correct max node')
        self.assertEqual(actual.key, 'z', 'produced correct max node key')
Beispiel #11
0
    def test_get_max(self):
        bst = BinarySearchTreeNode('y', 1)
        bst.insert('x', 2)
        bst.insert('z', 3)

        expected = bst.get_max()
        actual = bst.right
        self.assertEqual(actual, expected, 'produced correct max node')
        self.assertEqual(actual.key, 'z', 'produced correct max node key')
Beispiel #12
0
    def test_lookup(self):
        bst = BinarySearchTreeNode('y', 1)
        bst.insert('x', 2)
        bst.insert('z', 3)

        actual = bst.lookup('x')
        expected = bst.left
        self.assertEqual(actual, expected, 'should return the entire object')

        actual = bst.lookup('t')
        self.assertIsNone(actual, 'should not find node with key t')
Beispiel #13
0
    def test_lookup(self):
        bst = BinarySearchTreeNode('y', 1)
        bst.insert('x', 2)
        bst.insert('z', 3)

        actual = bst.lookup('x')
        expected = bst.left
        self.assertEqual(actual, expected, 'should return the entire object')

        actual = bst.lookup('t')
        self.assertIsNone(actual, 'should not find node with key t')
Beispiel #14
0
    def test_is_subtree_of(self):
        """ Using the following tree structure and two test candidates:
                (x)
               /   \                    (u)
             (t)   (y)         (r)        \
            /  \      \       /  \        (v)
          (r)  (u)    (z)   (q)  (s)        \
          / \    \                          (w)
        (q) (s)  (v)
             (root)        (subject1)  (subject2)
        """
        root = BinarySearchTreeNode('x')
        root.insert('y')
        root.insert('z')
        root.insert('t')
        root.insert('u')
        root.insert('v')
        root.insert('r')
        root.insert('s')
        root.insert('q')

        subject1 = BinarySearchTreeNode('r')
        subject1 = BinarySearchTreeNode('q')
        subject1 = BinarySearchTreeNode('s')

        self.assertTrue(subject1.is_subtree_of(root),
                        'should find the subtree')

        subject2 = BinarySearchTreeNode('u')
        subject2 = BinarySearchTreeNode('v')
        subject2 = BinarySearchTreeNode('w')

        self.assertFalse(subject2.is_subtree_of(root),
                         'should not find the subtree')
Beispiel #15
0
    def test_is_subtree_of(self):
        """ Using the following tree structure and two test candidates:
                (x)
               /   \                    (u)
             (t)   (y)         (r)        \
            /  \      \       /  \        (v)
          (r)  (u)    (z)   (q)  (s)        \
          / \    \                          (w)
        (q) (s)  (v)
             (root)        (subject1)  (subject2)
        """
        root = BinarySearchTreeNode('x')
        root.insert('y')
        root.insert('z')
        root.insert('t')
        root.insert('u')
        root.insert('v')
        root.insert('r')
        root.insert('s')
        root.insert('q')

        subject1 = BinarySearchTreeNode('r')
        subject1 = BinarySearchTreeNode('q')
        subject1 = BinarySearchTreeNode('s')

        self.assertTrue(subject1.is_subtree_of(root), 'should find the subtree')

        subject2 = BinarySearchTreeNode('u')
        subject2 = BinarySearchTreeNode('v')
        subject2 = BinarySearchTreeNode('w')

        self.assertFalse(subject2.is_subtree_of(root), 'should not find the subtree')
Beispiel #16
0
    def test_insert(self):
        bst = BinarySearchTreeNode('x', 1)
        y_node = bst.insert('y', 2)
        u_node = bst.insert('u', 3)

        self.assertEqual(bst.key, 'x', 'root is the correct value')
        self.assertEqual(bst.left.key, 'u', 'right node is correct')
        self.assertEqual(bst.right.key, 'y', 'left node is correct')

        self.assertEqual(bst.parent, None, 'root has no parent')
        self.assertEqual(bst.left.parent, bst, 'left has root as parent')
        self.assertEqual(bst.right.parent, bst, 'right has root as parent')

        self.assertEqual(bst.size, 3, 'should update correct size of a node')

        actual = bst.insert('v', 4)
        self.assertEqual(actual.key, 'v', 'should insert with correct key')
        self.assertEqual(actual.value, 4, 'should insert with correct value')
        self.assertEqual(actual.parent, u_node, 'should have the correct parent')
Beispiel #17
0
    def test_insert(self):
        bst = BinarySearchTreeNode('x', 1)
        y_node = bst.insert('y', 2)
        u_node = bst.insert('u', 3)

        self.assertEqual(bst.key, 'x', 'root is the correct value')
        self.assertEqual(bst.left.key, 'u', 'right node is correct')
        self.assertEqual(bst.right.key, 'y', 'left node is correct')

        self.assertEqual(bst.parent, None, 'root has no parent')
        self.assertEqual(bst.left.parent, bst, 'left has root as parent')
        self.assertEqual(bst.right.parent, bst, 'right has root as parent')

        self.assertEqual(bst.size, 3, 'should update correct size of a node')

        actual = bst.insert('v', 4)
        self.assertEqual(actual.key, 'v', 'should insert with correct key')
        self.assertEqual(actual.value, 4, 'should insert with correct value')
        self.assertEqual(actual.parent, u_node,
                         'should have the correct parent')
Beispiel #18
0
    def test_max_depth(self):
        """ Using the following tree:
                    (x)
                   /   \
                 (t)   (y)
                /  \      \
              (r)  (u)    (z)
              / \    \
            (q) (s)  (v)
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        u_node = x_node.insert('u')
        v_node = x_node.insert('v')
        r_node = x_node.insert('r')
        s_node = x_node.insert('s')
        q_node = x_node.insert('q')

        self.assertEqual(x_node.max_depth(), (q_node, 3),
            'q node is the first leaf with maximum depth of 3')
Beispiel #19
0
    def test_max_depth(self):
        """ Using the following tree:
                    (x)
                   /   \
                 (t)   (y)
                /  \      \
              (r)  (u)    (z)
              / \    \
            (q) (s)  (v)
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        u_node = x_node.insert('u')
        v_node = x_node.insert('v')
        r_node = x_node.insert('r')
        s_node = x_node.insert('s')
        q_node = x_node.insert('q')

        self.assertEqual(x_node.max_depth(), (q_node, 3),
                         'q node is the first leaf with maximum depth of 3')
Beispiel #20
0
    def test_depth(self):
        """ Using the following tree:
                    (x)
                   /   \
                 (t)   (y)
                /  \      \
              (r)  (u)    (z)
              / \    \
            (q) (s)  (v)
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        u_node = x_node.insert('u')
        v_node = x_node.insert('v')
        r_node = x_node.insert('r')
        s_node = x_node.insert('s')
        q_node = x_node.insert('q')

        self.assertEqual(x_node.depth(), 0, 'x is root so its depth is 0')
        self.assertEqual(v_node.depth(), 3, 'v is leaf with depth 3')
Beispiel #21
0
    def test_depth(self):
        """ Using the following tree:
                    (x)
                   /   \
                 (t)   (y)
                /  \      \
              (r)  (u)    (z)
              / \    \
            (q) (s)  (v)
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        u_node = x_node.insert('u')
        v_node = x_node.insert('v')
        r_node = x_node.insert('r')
        s_node = x_node.insert('s')
        q_node = x_node.insert('q')

        self.assertEqual(x_node.depth(), 0, 'x is root so its depth is 0')
        self.assertEqual(v_node.depth(), 3, 'v is leaf with depth 3')
Beispiel #22
0
    def test_rotate_left(self):
        """ Uses following tree structure, test rotate left between
        nodes t and u:
                    (x)                  (x)
                   /   \                /   \
                 (t)   (y)            (u)   (y)
                /  \      \   =>     /  \      \
              (r)  (u)    (z)      (t)  (v)    (z)
              / \    \             /
            (q) (s)  (v)         (r)
                                /  \
                              (q)  (s)
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        u_node = x_node.insert('u')
        v_node = x_node.insert('v')
        r_node = x_node.insert('r')
        s_node = x_node.insert('s')
        q_node = x_node.insert('q')

        t_node.rotate('left')

        self.assertEqual(u_node.parent, x_node, 'parent of u is now x')
        self.assertEqual(x_node.left, u_node, 'left child of x is u')
        self.assertEqual(u_node.left, t_node, 'left node of u is t')
        self.assertEqual(t_node.parent, u_node, 'parent of t is u')
        self.assertEqual(u_node.right, v_node, 'right node of u if v')
        self.assertEqual(v_node.parent, u_node, 'parent of v is u')
        self.assertEqual(t_node.parent, u_node, 'parent of t is u')
        self.assertEqual(t_node.left, r_node, 'left child of t is r')
        self.assertEqual(r_node.parent, t_node, 'parent node of r is t')

        # Test sizes of the newly rotated nodes
        self.assertEqual(t_node.size, 4, 't can now reach 4 nodes')
        self.assertEqual(u_node.size, 6, 'u can now reach 6 nodes')
Beispiel #23
0
    def test_rotate_left(self):
        """ Uses following tree structure, test rotate left between
        nodes t and u:
                    (x)                  (x)
                   /   \                /   \
                 (t)   (y)            (u)   (y)
                /  \      \   =>     /  \      \
              (r)  (u)    (z)      (t)  (v)    (z)
              / \    \             /
            (q) (s)  (v)         (r)
                                /  \
                              (q)  (s)
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        u_node = x_node.insert('u')
        v_node = x_node.insert('v')
        r_node = x_node.insert('r')
        s_node = x_node.insert('s')
        q_node = x_node.insert('q')

        t_node.rotate('left')

        self.assertEqual(u_node.parent, x_node, 'parent of u is now x')
        self.assertEqual(x_node.left, u_node, 'left child of x is u')
        self.assertEqual(u_node.left, t_node, 'left node of u is t')
        self.assertEqual(t_node.parent, u_node, 'parent of t is u')
        self.assertEqual(u_node.right, v_node, 'right node of u if v')
        self.assertEqual(v_node.parent, u_node, 'parent of v is u')
        self.assertEqual(t_node.parent, u_node, 'parent of t is u')
        self.assertEqual(t_node.left, r_node, 'left child of t is r')
        self.assertEqual(r_node.parent, t_node, 'parent node of r is t')

        # Test sizes of the newly rotated nodes
        self.assertEqual(t_node.size, 4, 't can now reach 4 nodes')
        self.assertEqual(u_node.size, 6, 'u can now reach 6 nodes')
Beispiel #24
0
    def test_rotate_right(self):
        """ Uses following tree structure, test rotate right between
        nodes r and t.
                    (x)                  (x)
                   /   \                /   \
                 (t)   (y)            (r)   (y)
                /  \      \   =>     /  \      \
              (r)  (u)    (z)      (q)  (t)    (z)
              / \    \                 /  \
            (q) (s)  (v)             (s)  (u)
                                            \
                                            (v)
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        u_node = x_node.insert('u')
        v_node = x_node.insert('v')
        r_node = x_node.insert('r')
        s_node = x_node.insert('s')
        q_node = x_node.insert('q')

        t_node.rotate('right')

        self.assertEqual(r_node.parent, x_node, 'x is parent of r')
        self.assertEqual(x_node.left, r_node, 'left child of x is r')
        self.assertEqual(r_node.left, q_node, 'q is left child of r')
        self.assertEqual(q_node.parent, r_node, 'parent of q is r')
        self.assertEqual(r_node.right, t_node, 'x is right child of r')
        self.assertEqual(t_node.parent, r_node, 'parent of r is t')
        self.assertEqual(t_node.left, s_node, 'left child of t is s')
        self.assertEqual(s_node.parent, t_node, 'new parent of s is t')
        self.assertEqual(u_node.parent, t_node, 'no change in the parent of u')

        # Test sizes of the newly rotated nodes
        self.assertEqual(t_node.size, 4, 't can now reach 4 nodes')
        self.assertEqual(r_node.size, 6, 'u can now reach 6 nodes')
Beispiel #25
0
    def test_rotate_right(self):
        """ Uses following tree structure, test rotate right between
        nodes r and t.
                    (x)                  (x)
                   /   \                /   \
                 (t)   (y)            (r)   (y)
                /  \      \   =>     /  \      \
              (r)  (u)    (z)      (q)  (t)    (z)
              / \    \                 /  \
            (q) (s)  (v)             (s)  (u)
                                            \
                                            (v)
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        u_node = x_node.insert('u')
        v_node = x_node.insert('v')
        r_node = x_node.insert('r')
        s_node = x_node.insert('s')
        q_node = x_node.insert('q')

        t_node.rotate('right')

        self.assertEqual(r_node.parent, x_node, 'x is parent of r')
        self.assertEqual(x_node.left, r_node, 'left child of x is r')
        self.assertEqual(r_node.left, q_node, 'q is left child of r')
        self.assertEqual(q_node.parent, r_node, 'parent of q is r')
        self.assertEqual(r_node.right, t_node, 'x is right child of r')
        self.assertEqual(t_node.parent, r_node, 'parent of r is t')
        self.assertEqual(t_node.left, s_node, 'left child of t is s')
        self.assertEqual(s_node.parent, t_node, 'new parent of s is t')
        self.assertEqual(u_node.parent, t_node, 'no change in the parent of u')

        # Test sizes of the newly rotated nodes
        self.assertEqual(t_node.size, 4, 't can now reach 4 nodes')
        self.assertEqual(r_node.size, 6, 'u can now reach 6 nodes')
Beispiel #26
0
 def test_diameter(self):
     """ Using the following tree structure:
             (x)
            /   \
          (t)   (y)
         /  \      \
       (r)  (u)    (z)
       / \    \
     (q) (s)  (v)
                \
                (w)
     """
     root = BinarySearchTreeNode('x')
     y_node = root.insert('y')
     z_node = root.insert('z')
     t_node = root.insert('t')
     u_node = root.insert('u')
     v_node = root.insert('v')
     w_node = root.insert('w')
     r_node = root.insert('r')
     s_node = root.insert('s')
     q_node = root.insert('q')
     self.assertEqual(root.diameter(), 7, 'max diameter of this tree is 6')
Beispiel #27
0
    def test_common_ancestor(self):
        """ Uses following tree structure:
                (x)
               /   \
             (t)   (y)
            /  \      \
          (r)  (u)    (z)
          / \    \
        (q) (s)  (v)
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        u_node = x_node.insert('u')
        v_node = x_node.insert('v')
        r_node = x_node.insert('r')
        s_node = x_node.insert('s')
        q_node = x_node.insert('q')

        self.assertEqual(t_node.common_ancestor(v_node), t_node, 't is ancestor of v')
        self.assertEqual(v_node.common_ancestor(u_node), u_node, 'u is parent of v')
        self.assertEqual(q_node.common_ancestor(x_node), x_node, 'x is root')
Beispiel #28
0
 def test_diameter(self):
     """ Using the following tree structure:
             (x)
            /   \
          (t)   (y)
         /  \      \
       (r)  (u)    (z)
       / \    \
     (q) (s)  (v)
                \
                (w)
     """
     root = BinarySearchTreeNode('x')
     y_node = root.insert('y')
     z_node = root.insert('z')
     t_node = root.insert('t')
     u_node = root.insert('u')
     v_node = root.insert('v')
     w_node = root.insert('w')
     r_node = root.insert('r')
     s_node = root.insert('s')
     q_node = root.insert('q')
     self.assertEqual(root.diameter(), 7, 'max diameter of this tree is 6')
Beispiel #29
0
    def test_successor(self):
        """ Following tree structure:
                (x)
               /   \
             (t)   (y)
               \      \
              (u)     (z)
                \
                (v)
        """
        x_node = BinarySearchTreeNode('x', 1)
        y_node = x_node.insert('y', 2)
        z_node = x_node.insert('z', 3)
        t_node = x_node.insert('t', 4)
        u_node = x_node.insert('u', 5)
        v_node = x_node.insert('v', 6)

        self.assertEqual(x_node.successor(), y_node, 'successor of x is y')
        self.assertEqual(y_node.successor(), z_node, 'successor of y is z')
        self.assertEqual(z_node.successor(), None, 'z has no successor')
        self.assertEqual(t_node.successor(), u_node, 'successor of t is u')
        self.assertEqual(u_node.successor(), v_node, 'successor of u is v')
        self.assertEqual(v_node.successor(), x_node, 'successor of v is x')
Beispiel #30
0
    def test_successor(self):
        """ Following tree structure:
                (x)
               /   \
             (t)   (y)
               \      \
              (u)     (z)
                \
                (v)
        """
        x_node = BinarySearchTreeNode('x', 1)
        y_node = x_node.insert('y', 2)
        z_node = x_node.insert('z', 3)
        t_node = x_node.insert('t', 4)
        u_node = x_node.insert('u', 5)
        v_node = x_node.insert('v', 6)

        self.assertEqual(x_node.successor(), y_node, 'successor of x is y')
        self.assertEqual(y_node.successor(), z_node, 'successor of y is z')
        self.assertEqual(z_node.successor(), None, 'z has no successor')
        self.assertEqual(t_node.successor(), u_node, 'successor of t is u')
        self.assertEqual(u_node.successor(), v_node, 'successor of u is v')
        self.assertEqual(v_node.successor(), x_node, 'successor of v is x')
Beispiel #31
0
    def test_post_order_traversal(self):
        """ Uses following tree structure:
                (x)
               /   \
             (t)   (y)
            /  \      \
          (r)  (u)    (z)
          / \    \
        (q) (s)  (v)
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        u_node = x_node.insert('u')
        v_node = x_node.insert('v')
        r_node = x_node.insert('r')
        s_node = x_node.insert('s')
        q_node = x_node.insert('q')

        expected = ['q', 's', 'r', 'v', 'u', 't', 'z', 'y', 'x']
        nodes = x_node.post_order_traversal()
        actual = map(lambda n: n.key, nodes)
        self.assertEqual(actual, expected, 'correct pre-order traversal')
Beispiel #32
0
    def test_post_order_traversal(self):
        """ Uses following tree structure:
                (x)
               /   \
             (t)   (y)
            /  \      \
          (r)  (u)    (z)
          / \    \
        (q) (s)  (v)
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        u_node = x_node.insert('u')
        v_node = x_node.insert('v')
        r_node = x_node.insert('r')
        s_node = x_node.insert('s')
        q_node = x_node.insert('q')

        expected = ['q', 's', 'r', 'v', 'u', 't', 'z', 'y', 'x']
        nodes = x_node.post_order_traversal()
        actual = map(lambda n: n.key, nodes)
        self.assertEqual(actual, expected, 'correct pre-order traversal')
Beispiel #33
0
    def test_delete(self):
        """ Following tree structure:
                (x)
               /   \
             (t)   (y)
            /  \      \
          (r)  (u)    (z)
          / \    \
        (q) (s)  (v)
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        u_node = x_node.insert('u')
        v_node = x_node.insert('v')
        r_node = x_node.insert('r')
        s_node = x_node.insert('s')
        q_node = x_node.insert('q')

        # Case 1: delete leaf.
        #         (x)                   (x)
        #        /   \                 /   \
        #      (t)   (y)             (t)   (y)
        #     /  \      \    =>     /  \      \
        #   (r)  (u)    (z)       (r)  (u)    (z)
        #   / \    \              / \
        # (q) (s)  (v)          (q) (s)
        v_node.delete()
        self.assertEqual(u_node.right, None, 'node u has no left child')
        self.assertEqual(v_node.parent, None, 'v was completely detached')

        # Case 2: delete internal node with one child.
        #         (x)                 (x)
        #        /   \               /   \
        #      (t)   (y)           (t)   (z)
        #     /  \     \   =>     /  \
        #   (r)  (u)   (z)      (r)  (u)
        #   / \                 / \
        # (q) (s)             (q) (s)
        y_node.delete()
        self.assertEqual(x_node.right, z_node, 'right child of x is now z')
        self.assertEqual(z_node.parent, x_node, 'parent of z is now x')
        self.assertEqual(y_node.parent, None, 'y was detached from its parent')
        self.assertEqual(y_node.right, None, 'y was completly detached from its right child')

        # Case 3, delete internal node with two children.
        #          (x)                 (x)
        #         /   \               /   \
        #       (t)   (z)           (s)   (z)
        #      /  \         =>     /  \
        #    (r)  (u)            (r)  (u)
        #   /  \                /
        # (q)  (s)            (q)
        t_node.delete()
        self.assertEqual(t_node.parent, None, 't was detached from parent')
        self.assertEqual(t_node.left, None, 't was detached from left child')
        self.assertEqual(t_node.right, None, 't was detached from right child')
        self.assertEqual(s_node.parent, x_node, 's new parent is x')
        self.assertEqual(s_node.left, r_node, 's left child is r')
        self.assertEqual(s_node.right, u_node, 's right child is u')
        self.assertEqual(r_node.right, None, 's was displaced from being right child of r')

        # Case 3, delete the root.
        #          (x)                 (u)
        #         /   \               /   \
        #       (s)   (z)           (s)   (z)
        #      /  \         =>     /
        #    (r)  (u)            (r)
        #   /                   /
        # (q)                 (q)
        x_node.delete()
        self.assertEqual(x_node.parent, None, 'root x was detached')
        self.assertEqual(x_node.left, None, 'root x was detached from left child')
        self.assertEqual(x_node.right, None, 'root x was detached from right child')
        self.assertEqual(u_node.parent, None, 'u is the new root')
        self.assertEqual(u_node.left, s_node, 'left child of root u is now s')
        self.assertEqual(u_node.right, z_node, 'right child of root us is now z')
Beispiel #34
0
    def test_is_identical(self):
        """ Using the following tree structure and two test candidates:
                (x)                (x)               (x)
               /   \              /   \             /   \
             (t)   (y)          (t)   (y)         (t)   (y)
            /  \      \        /  \      \       /  \      \
          (r)  (u)    (z)    (r)  (u)    (z)   (r)  (u)    (z)
          / \    \           / \    \          / \
        (q) (s)  (v)       (q) (s)  (v)      (q) (s)
             (root)            (subject1)        (subject2)
        """
        root = BinarySearchTreeNode('x')
        root.insert('y')
        root.insert('z')
        root.insert('t')
        root.insert('u')
        root.insert('v')
        root.insert('r')
        root.insert('s')
        root.insert('q')

        subject1 = BinarySearchTreeNode('x')
        subject1.insert('y')
        subject1.insert('z')
        subject1.insert('t')
        subject1.insert('u')
        subject1.insert('v')
        subject1.insert('r')
        subject1.insert('s')
        subject1.insert('q')

        self.assertTrue(root.is_identical(subject1), 'should detect identical trees')

        subject2 = BinarySearchTreeNode('x')
        subject2.insert('y')
        subject2.insert('z')
        subject2.insert('t')
        subject2.insert('u')
        subject2.insert('r')
        subject2.insert('s')
        subject2.insert('q')

        self.assertFalse(root.is_identical(subject2), 'should detect non-identical trees')
Beispiel #35
0
    def test_is_ballanced(self):
        """ Using the following tree structure:
                (x)                  (x)
               /   \                /   \
             (t)   (y)            (t)   (y)
            /  \      \          /  \      \
          (r)  (u)    (z)      (r)  (u)    (z)
          / \    \
        (q) (s)  (v)
                   \
                   (w)
            (unballanced)         (ballanced)
        """
        unballanced = BinarySearchTreeNode('x')
        unballanced.insert('y')
        unballanced.insert('z')
        unballanced.insert('t')
        unballanced.insert('u')
        unballanced.insert('v')
        unballanced.insert('w')
        unballanced.insert('r')
        unballanced.insert('s')
        unballanced.insert('q')
        self.assertFalse(unballanced.is_ballanced(),
            'subject tree is not ballanced')

        ballanced = BinarySearchTreeNode('x')
        ballanced.insert('y')
        ballanced.insert('z')
        ballanced.insert('t')
        ballanced.insert('u')
        ballanced.insert('r')
        self.assertTrue(ballanced.is_ballanced(),
            'subject tree is ballanced')
Beispiel #36
0
    def test_swap(self):
        """ Following tree structure is used:
                (x)
               /   \
             (t)   (y)
            /  \     \
          (r)  (u)   (z)
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        r_node = x_node.insert('r')
        u_node = x_node.insert('u')

        # 1. Swap leaf and it's parent node.
        #       (x)               (x)
        #      /   \             /   \
        #    (t)   (y)   =>    (r)   (y)
        #   /  \     \        /  \     \
        # (r)  (u)   (z)    (t)  (u)   (z)
        r_node.swap(t_node)
        self.assertEqual(r_node.parent, x_node, 'x is now parent of r')
        self.assertEqual(r_node.left, t_node, 't is left child of r')
        self.assertEqual(r_node.right, u_node, 'u is left child of r')
        self.assertEqual(t_node.parent, r_node, 'r is now parent of t')
        self.assertEqual(t_node.left, None, 't has no left child')
        self.assertEqual(t_node.right, None, 't has no right child')

        # 2. Swap leaf with another middle node.
        #       (x)               (x)
        #      /   \             /   \
        #    (r)   (y)   =>    (r)   (u)
        #   /  \     \        /  \     \
        # (t)  (u)   (z)    (t)  (y)   (z)

        u_node.swap(y_node)
        self.assertEqual(u_node.parent, x_node, 'x is now parent of u')
        self.assertEqual(u_node.left, None, 'u has no left child')
        self.assertEqual(u_node.right, z_node, 'z is right child of u')
        self.assertEqual(y_node.parent, r_node, 'r is now parent of y')
        self.assertEqual(y_node.left, None, 'y has no left child')
        self.assertEqual(y_node.right, None, 'y has no right child')

        # 3. Swap leaf with another leaf.
        #       (x)               (x)
        #      /   \             /   \
        #    (r)   (u)   =>    (r)   (u)
        #   /  \     \        /  \     \
        # (t)  (y)   (z)    (z)  (y)   (t)
        t_node.swap(z_node) #
        self.assertEqual(t_node.parent, u_node, 'u is now parent of t')
        self.assertEqual(t_node.left, None, 't has no left child')
        self.assertEqual(t_node.right, None, 't has no right child')
        self.assertEqual(z_node.parent, r_node, 'r is now parent of z')
        self.assertEqual(z_node.left, None, 'y has no left child')
        self.assertEqual(z_node.right, None, 'y has no right child')

        # 3. Swap leaf with root.
        #       (x)               (z)
        #      /   \             /   \
        #    (r)   (u)   =>    (r)   (u)
        #   /  \     \        /  \     \
        # (z)  (y)   (t)    (x)  (y)   (t)
        z_node.swap(x_node)
        self.assertEqual(z_node.parent, None, 'z is now a root so no parent')
        self.assertEqual(z_node.left, r_node, 'left child of z is r')
        self.assertEqual(z_node.right, u_node, 'right child of z is u')
        self.assertEqual(x_node.parent, r_node, 'r is now parent of x')
        self.assertEqual(x_node.left, None, 'x has no left child')
        self.assertEqual(x_node.right, None, 'x has no right child')
Beispiel #37
0
    def test_delete(self):
        """ Following tree structure:
                (x)
               /   \
             (t)   (y)
            /  \      \
          (r)  (u)    (z)
          / \    \
        (q) (s)  (v)
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        u_node = x_node.insert('u')
        v_node = x_node.insert('v')
        r_node = x_node.insert('r')
        s_node = x_node.insert('s')
        q_node = x_node.insert('q')

        # Case 1: delete leaf.
        #         (x)                   (x)
        #        /   \                 /   \
        #      (t)   (y)             (t)   (y)
        #     /  \      \    =>     /  \      \
        #   (r)  (u)    (z)       (r)  (u)    (z)
        #   / \    \              / \
        # (q) (s)  (v)          (q) (s)
        v_node.delete()
        self.assertEqual(u_node.right, None, 'node u has no left child')
        self.assertEqual(v_node.parent, None, 'v was completely detached')

        # Case 2: delete internal node with one child.
        #         (x)                 (x)
        #        /   \               /   \
        #      (t)   (y)           (t)   (z)
        #     /  \     \   =>     /  \
        #   (r)  (u)   (z)      (r)  (u)
        #   / \                 / \
        # (q) (s)             (q) (s)
        y_node.delete()
        self.assertEqual(x_node.right, z_node, 'right child of x is now z')
        self.assertEqual(z_node.parent, x_node, 'parent of z is now x')
        self.assertEqual(y_node.parent, None, 'y was detached from its parent')
        self.assertEqual(y_node.right, None,
                         'y was completly detached from its right child')

        # Case 3, delete internal node with two children.
        #          (x)                 (x)
        #         /   \               /   \
        #       (t)   (z)           (s)   (z)
        #      /  \         =>     /  \
        #    (r)  (u)            (r)  (u)
        #   /  \                /
        # (q)  (s)            (q)
        t_node.delete()
        self.assertEqual(t_node.parent, None, 't was detached from parent')
        self.assertEqual(t_node.left, None, 't was detached from left child')
        self.assertEqual(t_node.right, None, 't was detached from right child')
        self.assertEqual(s_node.parent, x_node, 's new parent is x')
        self.assertEqual(s_node.left, r_node, 's left child is r')
        self.assertEqual(s_node.right, u_node, 's right child is u')
        self.assertEqual(r_node.right, None,
                         's was displaced from being right child of r')

        # Case 3, delete the root.
        #          (x)                 (u)
        #         /   \               /   \
        #       (s)   (z)           (s)   (z)
        #      /  \         =>     /
        #    (r)  (u)            (r)
        #   /                   /
        # (q)                 (q)
        x_node.delete()
        self.assertEqual(x_node.parent, None, 'root x was detached')
        self.assertEqual(x_node.left, None,
                         'root x was detached from left child')
        self.assertEqual(x_node.right, None,
                         'root x was detached from right child')
        self.assertEqual(u_node.parent, None, 'u is the new root')
        self.assertEqual(u_node.left, s_node, 'left child of root u is now s')
        self.assertEqual(u_node.right, z_node,
                         'right child of root us is now z')
Beispiel #38
0
    def test_swap(self):
        """ Following tree structure is used:
                (x)
               /   \
             (t)   (y)
            /  \     \
          (r)  (u)   (z)
        """
        x_node = BinarySearchTreeNode('x')
        y_node = x_node.insert('y')
        z_node = x_node.insert('z')
        t_node = x_node.insert('t')
        r_node = x_node.insert('r')
        u_node = x_node.insert('u')

        # 1. Swap leaf and it's parent node.
        #       (x)               (x)
        #      /   \             /   \
        #    (t)   (y)   =>    (r)   (y)
        #   /  \     \        /  \     \
        # (r)  (u)   (z)    (t)  (u)   (z)
        r_node.swap(t_node)
        self.assertEqual(r_node.parent, x_node, 'x is now parent of r')
        self.assertEqual(r_node.left, t_node, 't is left child of r')
        self.assertEqual(r_node.right, u_node, 'u is left child of r')
        self.assertEqual(t_node.parent, r_node, 'r is now parent of t')
        self.assertEqual(t_node.left, None, 't has no left child')
        self.assertEqual(t_node.right, None, 't has no right child')

        # 2. Swap leaf with another middle node.
        #       (x)               (x)
        #      /   \             /   \
        #    (r)   (y)   =>    (r)   (u)
        #   /  \     \        /  \     \
        # (t)  (u)   (z)    (t)  (y)   (z)

        u_node.swap(y_node)
        self.assertEqual(u_node.parent, x_node, 'x is now parent of u')
        self.assertEqual(u_node.left, None, 'u has no left child')
        self.assertEqual(u_node.right, z_node, 'z is right child of u')
        self.assertEqual(y_node.parent, r_node, 'r is now parent of y')
        self.assertEqual(y_node.left, None, 'y has no left child')
        self.assertEqual(y_node.right, None, 'y has no right child')

        # 3. Swap leaf with another leaf.
        #       (x)               (x)
        #      /   \             /   \
        #    (r)   (u)   =>    (r)   (u)
        #   /  \     \        /  \     \
        # (t)  (y)   (z)    (z)  (y)   (t)
        t_node.swap(z_node)  #
        self.assertEqual(t_node.parent, u_node, 'u is now parent of t')
        self.assertEqual(t_node.left, None, 't has no left child')
        self.assertEqual(t_node.right, None, 't has no right child')
        self.assertEqual(z_node.parent, r_node, 'r is now parent of z')
        self.assertEqual(z_node.left, None, 'y has no left child')
        self.assertEqual(z_node.right, None, 'y has no right child')

        # 3. Swap leaf with root.
        #       (x)               (z)
        #      /   \             /   \
        #    (r)   (u)   =>    (r)   (u)
        #   /  \     \        /  \     \
        # (z)  (y)   (t)    (x)  (y)   (t)
        z_node.swap(x_node)
        self.assertEqual(z_node.parent, None, 'z is now a root so no parent')
        self.assertEqual(z_node.left, r_node, 'left child of z is r')
        self.assertEqual(z_node.right, u_node, 'right child of z is u')
        self.assertEqual(x_node.parent, r_node, 'r is now parent of x')
        self.assertEqual(x_node.left, None, 'x has no left child')
        self.assertEqual(x_node.right, None, 'x has no right child')
Beispiel #39
0
    def test_is_identical(self):
        """ Using the following tree structure and two test candidates:
                (x)                (x)               (x)
               /   \              /   \             /   \
             (t)   (y)          (t)   (y)         (t)   (y)
            /  \      \        /  \      \       /  \      \
          (r)  (u)    (z)    (r)  (u)    (z)   (r)  (u)    (z)
          / \    \           / \    \          / \
        (q) (s)  (v)       (q) (s)  (v)      (q) (s)
             (root)            (subject1)        (subject2)
        """
        root = BinarySearchTreeNode('x')
        root.insert('y')
        root.insert('z')
        root.insert('t')
        root.insert('u')
        root.insert('v')
        root.insert('r')
        root.insert('s')
        root.insert('q')

        subject1 = BinarySearchTreeNode('x')
        subject1.insert('y')
        subject1.insert('z')
        subject1.insert('t')
        subject1.insert('u')
        subject1.insert('v')
        subject1.insert('r')
        subject1.insert('s')
        subject1.insert('q')

        self.assertTrue(root.is_identical(subject1),
                        'should detect identical trees')

        subject2 = BinarySearchTreeNode('x')
        subject2.insert('y')
        subject2.insert('z')
        subject2.insert('t')
        subject2.insert('u')
        subject2.insert('r')
        subject2.insert('s')
        subject2.insert('q')

        self.assertFalse(root.is_identical(subject2),
                         'should detect non-identical trees')
Beispiel #40
0
    def test_is_ballanced(self):
        """ Using the following tree structure:
                (x)                  (x)
               /   \                /   \
             (t)   (y)            (t)   (y)
            /  \      \          /  \      \
          (r)  (u)    (z)      (r)  (u)    (z)
          / \    \
        (q) (s)  (v)
                   \
                   (w)
            (unballanced)         (ballanced)
        """
        unballanced = BinarySearchTreeNode('x')
        unballanced.insert('y')
        unballanced.insert('z')
        unballanced.insert('t')
        unballanced.insert('u')
        unballanced.insert('v')
        unballanced.insert('w')
        unballanced.insert('r')
        unballanced.insert('s')
        unballanced.insert('q')
        self.assertFalse(unballanced.is_ballanced(),
                         'subject tree is not ballanced')

        ballanced = BinarySearchTreeNode('x')
        ballanced.insert('y')
        ballanced.insert('z')
        ballanced.insert('t')
        ballanced.insert('u')
        ballanced.insert('r')
        self.assertTrue(ballanced.is_ballanced(), 'subject tree is ballanced')