Esempio n. 1
0
    def test_count_root(self):
        root = BSTNode(10, 10, None)
        tree = BST(root)
        node = BSTNode(15, 10, None)
        tree.AddKeyValue(15, 10)

        self.assertEqual(tree.Count(), 2)
 def test1_empty_node_init(self):
     """BSTNodes are initialized with key, value, left and right instance variables. (0p)"""
     node = BSTNode(1)
     self.assertEqual(node.key, 1)
     self.assertIsNone(node.value)
     self.assertIsNone(node.left)
     self.assertIsNone(node.right)
     node = BSTNode(1, 'a')
     self.assertEqual(node.value, 'a')
    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))
Esempio n. 4
0
    def test_findmaxnode(self):
        node = BSTNode("a")
        node.add("b")
        node.add("c")
        assert str(node.findmaxnode()) == "c"

        node = BSTNode("e")
        node.add("g")
        node.add("o")
        node.add("h")
        node.add("k")
        node.add("m")
        node.add("l")
        assert str(node.findmaxnode()) == "o"
Esempio n. 5
0
 def test_add(self, capsys):
     node = BSTNode(STestClass("Memento", "11/10/2000"))
     assert node._element == STestClass("Memento", "11/10/2000")
     node.add(STestClass("Melvin and Howard", "19/09/1980"))
     node.add(STestClass("Melvin and Howard", "21/03/2007"))
     node.add(STestClass("Mellow Mud", "21/09/2016"))
     node.add(STestClass("Melody", "21/03/2007"))
Esempio n. 6
0
    def test_create_node(self):
        root = BSTNode(0, None)
        self.assertTrue(root.left == None)
        self.assertTrue(root.right == None)
        self.assertTrue(root.value == 0)
        self.assertTrue(root.ref == None)

        node1 = BSTNode(1, root)
        self.assertTrue(node1.left == None)
        self.assertTrue(node1.right == None)
        self.assertTrue(node1.value == 1)
        self.assertTrue(node1.ref == root)

        root.right = node1
        logging.debug("BST root: %s" % str(root.to_list()))
        logging.debug("BST node1: %s" % str(node1.to_list()))
    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)
Esempio n. 8
0
 def test_string(self, capsys):
     node = BSTNode(STestClass("Memento", "11/10/2000"))
     node.add(STestClass("Melvin and Howard", "19/09/1980"))
     node.add(STestClass("Mellow Mud", "21/09/2016"))
     node.add(STestClass("Melody", "21/03/2007"))
     print(node)
     capt = capsys.readouterr()
     assert capt.out == "Mellow Mud, Melody, Melvin and Howard, Memento\n"
Esempio n. 9
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
Esempio n. 10
0
 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."
     )
Esempio n. 11
0
 def random_tree(self, size=20, min_val=-100, max_val=100):
     """
     : Generate a Binary Search Tree of random integers. This tree is composed
     : of `BinarySearchTree` nodes strung together, rather than an actual instance
     : `BinarySearchTree`. Used for testing `BinarySearchTreeNode`'s methods.
     """
     values = [random.randint(min_val, max_val) for i in range(size)]
     minimum = min(values)
     maximum = max(values)
     index = random.randint(0, size - 1)
     # This creates a duplicate of whichever value lies at `index`.
     root = BSTNode(values[index])
     for val in values:
         _insert(root, BSTNode(val))
     obj = SimpleNamespace(
         root=root, minimum=minimum, maximum=maximum, values=values
     )
     return obj
Esempio n. 12
0
    def test_search_node(self):
        node = BSTNode(STestClass("A", "a"))
        node.add(STestClass("G", "g"))
        node.add(STestClass("H", "h"))
        node.add(STestClass("B", "b"))
        node.add(STestClass("Z", "z"))

        search_node = node.search_node(STestClass("G", "g"))
        assert search_node._element.full_str() == "G: g"
Esempio n. 13
0
 def test_bst_node_init(self):
     """
     : Test the __init__ method of the BSTNode.
     """
     bst_node = BSTNode(5)
     self.assertEqual(bst_node.key, 5)
     self.assertEqual(bst_node.left, None)
     self.assertEqual(bst_node.right, None)
     self.assertEqual(bst_node.parent, None)
Esempio n. 14
0
 def consistent_tree(self):
     """
     : Generate a Binary Search Tree of consistent integers. This tree is composed
     : of `BinarySearchTree` nodes strung together, rather than an actual instance
     : `BinarySearchTree`. Used for testing `BinarySearchTreeNode`'s methods.
     """
     root_val = 6
     values = [-10, -6, 43, 65, 12, 43, 90, 3, 71, 3, -234, 94, 5]
     minimum = min(values)
     maximum = max(values)
     root = BSTNode(root_val)
     for val in values:
         _insert(root, BSTNode(val))
     # Append `root_val` to `values` for full list of tree's values.
     values.append(root_val)
     obj = SimpleNamespace(
         root=root, minimum=minimum, maximum=maximum, values=values
     )
     return obj
Esempio n. 15
0
    def test_size(self):
        node = BSTNode("e")
        assert node.size() == 1

        node.add("g")
        node.add("o")
        node.add("h")
        node.add("k")
        node.add("m")
        node.add("l")
        assert node.size() == 7
Esempio n. 16
0
    def test_search(self):
        node = BSTNode(STestClass("A", "a"))
        node.add(STestClass("G", "g"))
        node.add(STestClass("H", "h"))
        node.add(STestClass("B", "b"))
        node.add(STestClass("Z", "z"))

        search_item = node.search_node(STestClass("C", "c"))
        assert search_item is None

        search_item = node.search(STestClass("H", "h"))
        assert search_item.full_str() == "H: h"
Esempio n. 17
0
    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()
Esempio n. 18
0
    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()
Esempio n. 19
0
    def add(self, title, date, runtime):
        """Add a new movie to the library.

        Args:
            title - the title of the movie
            date - the date the movie was released
            runtime - the running time of the movie

        Returns:
            the movie file that was added, or None
        """
        add_movie = Movie(title, date, runtime)
        if self.bst is None:
            self.bst = BSTNode(add_movie)
            return add_movie
        return self.bst.add(add_movie)
Esempio n. 20
0
    def test_print_traversals(self):
        # WARNING:  Tests are for Print()
        # Debug calls to Print() in functions will cause failure

        stdout_ = sys.stdout  # Keep previous value
        sys.stdout = io.StringIO()

        self.bst = BSTNode(1)
        self.bst.insert(8)
        self.bst.insert(5)
        self.bst.insert(7)
        self.bst.insert(6)
        self.bst.insert(3)
        self.bst.insert(4)
        self.bst.insert(2)

        self.bst.in_order_print(self.bst)

        output = sys.stdout.getvalue()
        self.assertEqual(output, "1\n2\n3\n4\n5\n6\n7\n8\n")

        sys.stdout = io.StringIO()
        self.bst.bft_print(self.bst)
        output = sys.stdout.getvalue()
        self.assertTrue(output == "1\n8\n5\n3\n7\n2\n4\n6\n" or
                        output == "1\n8\n5\n7\n3\n6\n4\n2\n")

        sys.stdout = io.StringIO()
        self.bst.dft_print(self.bst)
        output = sys.stdout.getvalue()
        self.assertTrue(output == "1\n8\n5\n7\n6\n3\n4\n2\n" or
                        output == "1\n8\n5\n3\n2\n4\n7\n6\n")

        sys.stdout = io.StringIO()
        self.bst.pre_order_dft(self.bst)
        output = sys.stdout.getvalue()
        self.assertEqual(output, "1\n8\n5\n3\n2\n4\n7\n6\n")

        sys.stdout = io.StringIO()
        self.bst.post_order_dft(self.bst)
        output = sys.stdout.getvalue()
        self.assertEqual(output, "2\n4\n3\n6\n7\n5\n8\n1\n")

        sys.stdout = stdout_  # Restore stdout
Esempio n. 21
0
    def add(self, title, date, runtime):
        """ Add a new move to the library.

        Args:
            title - the title of the movie
            date - the date the movie was released
            runtime - the running time of the movie

        Returns:
            the movie file that was added, or None
        """
        # method body goes here
        # you need to create the Movie object, then add it to the BST,
        # take what is returned from that method, and then decide what to
        # return here.
        # Remember to handle the case where the bst is empty.
        # check to see if bst is None -
        movie = Movie(title, date, runtime)
        if self.bst is None:
            self.bst = BSTNode(movie)
            return movie
        return self.bst.add(movie)
import time
from bst import BSTNode

start_time = time.time()

f = open('names_1.txt', 'r')
names_1 = f.read().split("\n")  # List containing 10000 names
f.close()

f = open('names_2.txt', 'r')
names_2 = f.read().split("\n")  # List containing 10000 names
f.close()

duplicates = []  # Return the list of duplicates in this data structure
bst = BSTNode(names_1[0])
# Replace the nested for loops below with your improvements
for name_1 in names_1:
    bst.insert(name_1)

for name_2 in names_2:
    if bst.contains(name_2):
        duplicates.append(name_2)
# for name_1 in names_1:
#     for name_2 in names_2:
#         if name_1 == name_2:
#             duplicates.append(name_1)

end_time = time.time()
print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print (f"runtime: {end_time - start_time} seconds")
Esempio n. 23
0
 def test_handle_dupe_insert(self):
     self.bst2 = BSTNode(1)
     self.bst2.insert(1)
     self.assertEqual(self.bst2.right.value, 1)
Esempio n. 24
0
    def test_is_bst_none(self):
        tree = BSTNode(None)

        self.assertFalse(is_BST(tree))
Esempio n. 25
0
    def test_is_bst_single_value(self):
        tree = BSTNode(0)

        self.assertTrue(is_BST(tree))
Esempio n. 26
0
    def test_is_not_bst(self):
        tree = BSTNode(3)
        tree.left_branch = BSTNode(2)
        tree.right_branch = BSTNode(1)

        self.assertFalse(is_BST(tree))
Esempio n. 27
0
    def test_is_bst(self):
        tree = BSTNode(2)
        tree.left_branch = BSTNode(1)
        tree.right_branch = BSTNode(3)

        self.assertTrue(is_BST(tree))
Esempio n. 28
0
 def test_bst_attributes(self):
     tree = BSTNode(1)
     self.assertEqual(tree.root, 1)
     self.assertEqual(tree.left_branch, None)
     self.assertEqual(tree.right_branch, None)
names_2 = f.read().split("\n")  # List containing 10000 names
f.close()

duplicates = []  # Return the list of duplicates in this data structure

# O (n^2)
# This Method is 5.24 Seconds
# Replace the nested for loops below with your improvements
# for name_1 in names_1:
#     for name_2 in names_2:
#         if name_1 == name_2:
#             duplicates.append(name_1)

# 0(n log n) ?
# BST Method Done in 0.11 Seconds
bst = BSTNode("")
# Build BST
for name_2 in names_2:
    bst.insert(name_2)

for name_1 in names_1:
    if bst.contains(name_1):
        duplicates.append(name_1)

end_time = time.time()
print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print(f"runtime: {end_time - start_time} seconds")

# ---------- Stretch Goal -----------
# Python has built-in tools that allow for a very efficient approach to this problem
# What's the best time you can accomplish?  Thare are no restrictions on techniques or data
Esempio n. 30
0
 def setUp(self):
     self.bst = BSTNode(5)