示例#1
0
 def test_reverse(self):
     ll = data_structures.LinkedList()
     nodes = [data_structures.LinkedListNode(i) for i in range(10)]
     for node in nodes:
         ll.insert(node)
     ll.reverse()
     self.assertTrue(sorting.is_sorted([node.data for node in ll]))
示例#2
0
 def test_pop(self):
     pq = data_structures.BSTPriorityQueue()
     for i in [8, 2, 16, 4, 12, 34, 1]:
         pq.add(i, str(i))
     contents = []
     for _ in range(7):
         contents.append(pq.pop())
     self.assertTrue(sorting.is_sorted(contents))
示例#3
0
 def test_convert_to_ll(self):
     bst = data_structures.BST(10)
     sub_trees = [data_structures.BST(i) for i in
                  [5, 12, 4, 31, 2, 20, 11]]
     for sub_tree in sub_trees:
         bst.insert(sub_tree)
     ll = bst.convert_to_ll()
     self.assertTrue(sorting.is_sorted([node.data for node in ll]))
示例#4
0
    def test_iter(self):
        bst = data_structures.BST(10)
        sub_trees = [data_structures.BST(i) for i in
                     [5, 12, 4, 31, 2, 20]]
        for sub_tree in sub_trees:
            bst.insert(sub_tree)
        data_vals = []
        for sub_tree in bst:
            data_vals.append(sub_tree.data)
        self.assertTrue(sorting.is_sorted(data_vals))

        bst = data_structures.BST(random.random())
        sub_trees = [data_structures.BST(random.random())
                     for i in xrange(1000)]
        for sub_tree in sub_trees:
            bst.insert(sub_tree)
        data_vals = []
        for sub_tree in bst:
            data_vals.append(sub_tree.data)
        self.assertTrue(sorting.is_sorted(data_vals))
示例#5
0
 def build_from_sorted(sorted_list, minloc=None, maxloc=None):
     """Returns a BST object from a sorted list"""
     assert sorting.is_sorted(sorted_list)
     if minloc == None:
         minloc = 0
     if maxloc == None:
         maxloc = len(sorted_list) - 1
     if minloc > maxloc:
         return None
     elif minloc == maxloc:
         return BST(sorted_list[minloc])
     center = 2**int(math.log(maxloc - minloc + 1, 2)) - 1 + minloc
     bst = BST(sorted_list[center])
     bst.left = BST.build_from_sorted(sorted_list, minloc, center - 1)
     if bst.left != None:
         bst.left.parent = bst
     bst.right = BST.build_from_sorted(sorted_list, center + 1, maxloc)
     if bst.right != None:
         bst.right.parent = bst
     return bst
示例#6
0
def inplace_check(function, test_list):
    function(test_list)
    assert sorting.is_sorted(test_list)
示例#7
0
def check_stuff(function, test_list):
    assert sorting.is_sorted(function(test_list))