示例#1
0
 def test_search_with_3_strings(self):
     # Create a complete binary search tree of 3 items in level-order
     items = ['B', 'A', 'C']
     tree = BinarySearchTree(items)
     assert tree.search('A') == 'A'
     assert tree.search('B') == 'B'
     assert tree.search('C') == 'C'
     assert tree.search('D') is None
示例#2
0
 def test_search_with_3_items(self):
     # Create a complete binary search tree of 3 items in level-order
     items = [2, 1, 3]
     tree = BinarySearchTree(items)
     assert tree.search(1) == 1
     assert tree.search(2) == 2
     assert tree.search(3) == 3
     assert tree.search(4) is None
示例#3
0
 def test_search_with_7_items(self):
     # Create a complete binary search tree of 7 items in level-order
     items = [4, 2, 6, 1, 3, 5, 7]
     tree = BinarySearchTree(items)
     # for item in items:
     #     print(item)
     #     assert tree.search(item) == item
     assert tree.search(8) is None
 def test_delete(self):
     tree = BinarySearchTree([2, 4])
     assert tree.height() == 1
     tree.delete(4)
     assert tree.height() == 0
     tree.delete(2)
     assert tree.height() == 0
     items = [18, 1, 53, 40, 63]
     tree.insert(10)
     tree = BinarySearchTree(items)
     tree.delete(1)
     assert tree.search(1) is None
     tree.delete(10)
     assert tree.search(10) is None
     tree.insert(57)
     tree.insert(98)
     tree.delete(63)
     assert tree.search(63) is None
 def test_delete_ints(self):
     tree = BinarySearchTree([6, 4, 7, 3, 1, 20, 34])
     assert tree.root.data == 6
     assert tree.size == 7
     tree.delete(7)
     assert tree.size == 6
     assert tree.search(7) == None
     assert tree.items_in_order() == [1, 3, 4, 6, 20, 34]
     tree.delete(20)
     assert tree.size == 5
     assert tree.items_in_order() == [1, 3, 4, 6, 34]
示例#6
0
class RouteFinder(object):

    def __init__(self, file):
        self.routes = []
        with open(file, 'r') as f:
            contents = f.read().split()
            for content in contents:
                split = content.split(',')
                self.routes.append((split[0], split[1]))

        self.routes_tree = BinarySearchTree(self.routes)

    def find_best_route(self, number):
        return self._find_best_route_recursive(number)

    def _find_best_route_recursive(self, number):
        if number == '+':
            return None
        else:
            route = self.routes_tree.search(number)
            if route is not None:
                return route
            else:
                return self._find_best_route_recursive(number[:-1])
class TreeSet(object):
    """Class for sets implemented with binary trees"""
    def __init__(self, items=None):
        """Initialize a new Set"""
        self.tree = BinarySearchTree(items)
        # self.size = self.tree.size

    def __repr__(self):
        """Return a string representation of this set"""
        items = ['({!r})'.format(item) for item in self.tree.items_in_order()]
        return ', '.join(items)

    def __iter__(self):
        """Allow the set to be iterated over (i.e. in for loops)"""
        return iter([value for value in self.tree.items_level_order()])

    def __eq__(self, other):
        """Allow sets to be compared to other sets"""
        return self.tree.items_in_order() == other.tree.items_in_order()

    @property
    def size(self):
        """Use tree size property"""
        return self.tree.size

    def contains(self, item):
        """Return a boolean indicating whether item is in this set
        Best case time complexity: O(1) if item is in the tree root
        Worst case: O(logn) if item is a leaf
        """
        return self.tree.search(item) is not None

    def add(self, item):
        """Add the item to the set if not present
        Best case time complexity: O(1) if tree is empty
        Worst case: O(n) if tree is poorly balanced
        """
        if not self.contains(item):
            self.tree.insert(item)

    def remove(self, item):
        """Remove element from this set, if present, or else raise KeyError
        Best case time complexity: O(1) if item is in the tree root
        Worst case: O(logn) if item isn't there
        """
        if not self.contains(item):
            raise KeyError("Item not found")
        else:
            self.tree.delete(item)

    def union(self, other_set):
        """Return a new set that is the union of this set and other_set
        Best and worst case time complexity: O(n+m), where n is the size of
        self, and m is the size of other_set, because every item has to
        be accounted for
        """
        new_set = TreeSet(self)
        for item in other_set:
            new_set.add(item)
        return new_set

    def intersection(self, other_set):
        """Return a new set that is the intersection of this and other_set
        Best and worst case time complexity: O(m) where m is the size of
        other_set, because it iterates over the other set.
        """
        new_set = TreeSet()
        for item in other_set:
            if self.contains(item):
                new_set.add(item)
        return new_set

    def difference(self, other_set):
        """Return a new set that is the difference of this set and other_set
        Best and worst case time complexity: O(n + logm) where n is the size of
        self, and m is the size of other_set, because it iterates over self and
        checks if other_set contains the items
        """
        new_set = TreeSet()
        for item in self:
            if not other_set.contains(item):
                new_set.add(item)
        return new_set

    def is_subset(self, other_set):
        """Return a boolean indicating if other_set is a subset of this
        Best case time complexity: O(1) if other_set is larger than self
        Worst case: O(m) where m is the size of other_set, as it has to
        iterate over each element of it
        """
        if other_set.size > self.size:
            return False

        for item in other_set:
            if not self.contains(item):
                return False

        return True