Exemple #1
0
 def minimal_tree_recur(elements, low, high):
     if low == high:
         return None
     index = (low + high) // 2
     node = BSTNode(elements[index])
     node.left = minimal_tree_recur(elements, low, index)
     node.right = minimal_tree_recur(elements, index + 1, high)
     return node
    def test5_find_one(self):
        """Calling find for a node which exists should return that node. (1p)"""
        node = BSTNode(2)
        node.left = BSTNode(1)
        node.right = BSTNode(3)
        self.tree.root = node

        for node in (node, node.left, node.right):
            found_node = self.tree.find(node.key)
            self.assertIs(
                found_node, node,
                "If {0!r} exists in tree, calling tree.find({1}) should return that node, not {2!r}"
                .format(node, node.key, found_node))
    def setUp(self):
        # Root
        Q = BSTNode(4, "Q")
        # Internal node
        P = BSTNode(2, "P")
        # Leafs
        A = BSTNode(1, "A")
        B = BSTNode(3, "B")
        C = BSTNode(5, "C")

        # Construct a minimal tree
        Q.left = P
        Q.right = C
        P.left = A
        P.right = B

        #        Q
        #      /   \
        #     P     C
        #   /   \
        #  A     B

        self.nodes = (P, Q, A, B, C)
        self.tree = AVL()
    def setUp(self):
        # Root
        P = BSTNode(2, "P")
        # Internal node
        Q = BSTNode(4, "Q")
        # Leafs
        A = BSTNode(1, "A")
        B = BSTNode(3, "B")
        C = BSTNode(5, "C")

        # Construct a minimal tree
        P.left = A
        P.right = Q
        Q.left = B
        Q.right = C

        #       P
        #     /   \
        #    A     Q
        #        /   \
        #       B     C

        self.nodes = (P, Q, A, B, C)
        self.tree = AVL()
    def setUp(self):
        A = BSTNode(1, "A")
        B = BSTNode(3, "B")
        C = BSTNode(2, "C")

        B.left = C
        A.right = B

        #    A
        #     \
        #      B
        #     /
        #    C

        self.tree = AVL()
        self.nodes = (A, B, C)
 def test4_node_height_uneven_subtrees(self):
     """The height of a BSTNode with subtrees of uneven heights is the height of the taller subtree plus one. (0p)"""
     node = BSTNode(1)
     node.left = BSTNode(2)
     node.left.left = BSTNode(3)
     self.assertEqual(
         node.left.height(), 1,
         "Expected the height of a node with one child, which is a leaf, to be 1 but it was not."
     )
     node.right = BSTNode(4)
     self.assertEqual(
         node.right.height(), 0,
         "Expected the height of a leaf to be 0 but it was not.")
     self.assertEqual(
         node.height(),
         node.left.height() + 1,
         "Expected the height of a node with two children, left and right, of which right is a leaf and left has one child, which is a leaf, to be the height of left plus one but it was not."
     )
 def test3_node_height_one_child(self):
     """The height of a BSTNode with one child is 1 and the height of that child is 0. (0p)"""
     node = BSTNode(1)
     node.left = BSTNode(2)
     self.assertEqual(node.height(), 1)
     self.assertEqual(node.left.height(), 0)
def swap(a, b):
    a.data = b.data
    b.data = a.data
    a.data = b.data


def swap_nodes_bst(root, first, middle, last, prev):
    if root is not None:
        swap_nodes_bst(root.left, first, middle, last, prev)
        if prev and root.data < prev.data:
            if not first:
                first = prev
                middle = root
            else:
                last = root
        prev = root
        swap_nodes_bst(root.right, first, middle, last, prev)


if __name__ == "__main__":
    root = BSTNode(4)

    root.left = BSTNode(2)
    root.right = BSTNode(6)

    root.left.left = BSTNode(1)
    root.left.right = BSTNode(3)

    root.right.left = BSTNode(5)
    root.right.right = BSTNode(7)
    return get_pair_with_sum_rec(orignl_root, node.left, sum) or \
           get_pair_with_sum_rec(orignl_root, node.right, sum)


def has_other_pair(root, this_node, rem):
    if not root:
        return
    if root.data == rem and this_node!=root:
        return root.data
    elif root.data < rem:
        return has_other_pair(root.right, this_node, rem)
    elif root.data > rem:
        return has_other_pair(root.left, this_node, rem)

if __name__ == "__main__":
    root = BSTNode(4)

    root.left = BSTNode(2)
    root.right = BSTNode(6)

    root.left.left = BSTNode(1)
    root.left.right = BSTNode(3)

    root.right.left = BSTNode(5)
    root.right.right = BSTNode(7)

    sum = 4
    for sum in xrange(1, 15):
        print get_pair_with_sum(root, sum),
        print get_pair_with_sum_rec(root, root, sum)