Ejemplo n.º 1
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"
Ejemplo n.º 2
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"
Ejemplo n.º 3
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"
Ejemplo n.º 4
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"))
Ejemplo n.º 5
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"
Ejemplo n.º 6
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
Ejemplo n.º 7
0
class MovieLib:
    """A movie library.

    Implemented using a BST.
    """

    def __init__(self):
        """Initialise a movie library."""
        self.bst = None

    def __str__(self):
        """Return a string representation of the library.

        The string will be created by an in-order traversal.
        """
        if self.bst is not None:
            return str(self.bst)

    def size(self):
        """Return the number of movies in the library."""
        if self.bst is None:
            return 0
        return self.bst.size()

    def search(self, title):
        """Return Movie with matching title if there, or None.

        Args:
            title: a string representing a movie title.
        """
        if self.bst is not None:
            return self.bst.search(Movie(title))

    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)

    def remove(self, title):
        """Remove and return the a movie object with the given title, if there.

        Args:
            title - the title of the movie to be removed
        """
        if self.bst is not None:
            removed = self.bst.remove(Movie(title))
            if self.bst.size() == 0:  # If tree is now empty
                self.bst = None  # Set reference to None
            return removed

    def _testadd():
        library = MovieLib()
        library.add("Memento", "11/10/2000", 113)
        print(str(library))
        print("> adding Melvin and Howard")
        library.add("Melvin and Howard", "19/09/1980", 95)
        print(str(library))
        print("> adding a second version of Melvin and Howard")
        library.add("Melvin and Howard", "21/03/2007", 112)
        print(str(library))
        print("> adding Mellow Mud")
        library.add("Mellow Mud", "21/09/2016", 92)
        print(str(library))
        print("> adding Melody")
        library.add("Melody", "21/03/2007", 113)
        print(str(library))
        return library

    def _test():
        library = MovieLib()
        library.add("B", "b", 1)
        print("Library:", library)
        print("adding", "A")
        library.add("A", "a", 1)
        print("Library:", library)
        print("removing", "A")
        library.remove("A")
        print("Library:", library)
        print("adding", "C")
        library.add("C", "c", 1)
        print("Library:", library)
        print("removing", "C")
        library.remove("C")
        print("Library:", library)
        print("adding", "F")
        library.add("F", "f", 1)
        print("Library:", library)
        print("removing", "B")
        library.remove("B")
        print("Library:", library)
        print("adding", "C")
        library.add("C", "c", 1)
        print("Library:", library)
        print("adding", "D")
        library.add("D", "d", 1)
        print("Library:", library)
        print("adding", "C")
        library.add("C", "c", 1)
        print("Library:", library)
        print("adding", "E")
        library.add("E", "e", 1)
        print("Library:", library)
        print("removing", "B")
        library.remove("B")
        print("Library:", library)
        print("removing", "D")
        library.remove("D")
        print("Library:", library)
        print("removing", "C")
        library.remove("C")
        print("Library:", library)
        print("removing", "E")
        library.remove("E")
        print("Library:", library)
        print("adding", "L")
        library.add("L", "l", 1)
        print("Library:", library)
        print("adding", "H")
        library.add("H", "h", 1)
        print("Library:", library)
        print("adding", "I")
        library.add("I", "i", 1)
        print("Library:", library)
        print("adding", "G")
        library.add("G", "g", 1)
        print("Library:", library)
        print("removing", "L")
        library.remove("L")
        print("Library:", library)
        print("removing", "H")
        library.remove("H")
        print("Library:", library)
        print("removing", "I")
        library.remove("I")
        print("Library:", library)
        print("removing", "G")
        library.remove("G")
        print("Library:", library)
Ejemplo n.º 8
0
    def test_add_remove(self):
        node = BSTNode(STestClass("B", "b"))
        out = str(node)
        assert out == "B"

        node.add(STestClass("A", "a"))
        out = str(node)
        assert out == "A, B"

        node.remove(STestClass("A"))
        out = str(node)
        assert out == "B"

        node.add(STestClass("C", "c"))
        out = str(node)
        assert out == "B, C"

        node.remove(STestClass("C"))
        out = str(node)
        assert out == "B"

        node.add(STestClass("F", "f"))
        out = str(node)
        assert out == "B, F"

        node.remove(STestClass("B"))
        out = str(node)
        assert out == "F"

        node.add(STestClass("C", "c"))
        out = str(node)
        assert out == "C, F"

        node.add(STestClass("D", "d"))
        out = str(node)
        assert out == "C, D, F"

        node.add(STestClass("C", "c"))
        out = str(node)
        assert out == "C, D, F"

        node.add(STestClass("E", "e"))
        out = str(node)
        assert out == "C, D, E, F"

        node.remove(STestClass("B"))
        out = str(node)
        assert out == "C, D, E, F"

        node.remove(STestClass("D"))
        out = str(node)
        assert out == "C, E, F"

        node.remove(STestClass("C"))
        out = str(node)
        assert out == "E, F"

        node.remove(STestClass("E"))
        out = str(node)
        assert out == "F"

        node.add(STestClass("L", "l"))
        out = str(node)
        assert out == "F, L"

        node.add(STestClass("H", "h"))
        out = str(node)
        assert out == "F, H, L"

        node.add(STestClass("I", "i"))
        out = str(node)
        assert out == "F, H, I, L"

        node.add(STestClass("G", "g"))
        out = str(node)
        assert out == "F, G, H, I, L"

        node.remove(STestClass("L"))
        out = str(node)
        assert out == "F, G, H, I"

        node.remove(STestClass("H"))
        out = str(node)
        assert out == "F, G, I"

        node.remove(STestClass("I"))
        out = str(node)
        assert out == "F, G"

        node.remove(STestClass("G"))
        out = str(node)
        assert out == "F"

        assert node._properBST()
Ejemplo n.º 9
0
class MovieLib:
    """ A movie library.

    Implemented using a BST. 
    """
    def __init__(self):
        """ Initialise a movie library. """
        self.bst = None

    def __str__(self):
        """ Return a string representation of the library.

        The string will be created by an in-order traversal.
        """
        # method goes here
        if self.bst is not None:
            return str(self.bst)
        return None

    def size(self):
        """ Return the number of movies in the library. """
        # method goes here
        # calling bst
        if self.bst is not None:
            return self.bst.size()
        return None

    def search(self, title):
        """ Return Movie with matching title if there, or None.

        Args:
            title: a string representing a movie title.
        """
        # method goes here
        # Note that the BST requires an object to search for, and we don't
        # necessarily know the details of the movie we are looking for except
        # its title. But Movie objects are compared only on their titles (see
        # the __eq__ method above in teh Movie class).
        # Create a new Movie object with that title, and ise that to search
        # search the BST.

        if self.bst is not None:
            movie = Movie(title)
            return self.bst.search(movie)
        return None

    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)

    def remove(self, title):
        """ Remove and return the a movie object with the given title, if there.

        Args:
            title - the title of the movie to be removed
        """
        # method body goes here

        if self.bst is not None:
            movie = Movie(title)
            return self.bst.remove(movie)

    def _testadd():
        library = MovieLib()
        library.add("Memento", "11/10/2000", 113)
        print(str(library))
        print('> adding Melvin and Howard')
        library.add("Melvin and Howard", "19/09/1980", 95)
        print(str(library))
        print('> adding a second version of Melvin and Howard')
        library.add("Melvin and Howard", "21/03/2007", 112)
        print(str(library))
        print('> adding Mellow Mud')
        library.add("Mellow Mud", "21/09/2016", 92)
        print(str(library))
        print('> adding Melody')
        library.add("Melody", "21/03/2007", 113)
        print(str(library))
        return library

    def _test():
        library = MovieLib()
        library.add("B", "b", 1)
        print('Library:', library)
        print('adding', "A")
        library.add("A", "a", 1)
        print('Library:', library)
        print('removing', "A")
        library.remove("A")
        print('Library:', library)
        print('adding', "C")
        library.add("C", "c", 1)
        print('Library:', library)
        print('removing', "C")
        library.remove("C")
        print('Library:', library)
        print('adding', "F")
        library.add("F", "f", 1)
        print('Library:', library)
        print('removing', "B")
        library.remove("B")
        print('Library:', library)
        print('adding', "C")
        library.add("C", "c", 1)
        print('Library:', library)
        print('adding', "D")
        library.add("D", "d", 1)
        print('Library:', library)
        print('adding', "C")
        library.add("C", "c", 1)
        print('Library:', library)
        print('adding', "E")
        library.add("E", "e", 1)
        print('Library:', library)
        print('removing', "B")
        library.remove("B")
        print('Library:', library)
        print('removing', "D")
        library.remove("D")
        print('Library:', library)
        print('removing', "C")
        library.remove("C")
        print('Library:', library)
        print('removing', "E")
        library.remove("E")
        print('Library:', library)
        print('adding', "L")
        library.add("L", "l", 1)
        print('Library:', library)
        print('adding', "H")
        library.add("H", "h", 1)
        print('Library:', library)
        print('adding', "I")
        library.add("I", "i", 1)
        print('Library:', library)
        print('adding', "G")
        library.add("G", "g", 1)
        print('Library:', library)
        print('removing', "L")
        library.remove("L")
        print('Library:', library)
        print('removing', "H")
        library.remove("H")
        print('Library:', library)
        print('removing', "I")
        library.remove("I")
        print('Library:', library)
        print('removing', "G")
        library.remove("G")
        print('Library:', library)