Example #1
0
    def test_alternating_order_insertion(self):
        '''Test alternating order generation.'''
        
        # Minimum possible depth = [log2(n)]+1 where n=size of data, so here 4
        data = np.array([5, 1, 2, 7, 8, 3, 4, 6, 9])
        n = np.size(data)
        
        # Original list order
        self._test_tree_depth(data, 5)
        # Ordered insertion is worst case!
        sorted_data = sort(data)
        self._test_tree_depth(sorted_data, 9)
        # Alternating order is not so good either 
        self._test_tree_depth(sorted_data[alternating_order(n)], 5)
        # Neither does random order guarantee anything
        self._test_tree_depth(data[np.random.permutation(n)], 4, 9)

        # Best order. O(n log n) complexity to sort the list.
        self._test_tree_depth(sorted_data[optimal_insertion_order(n)], 4)
        self._test_tree_depth([8, 4, 2, 1, 3, 6, 5, 7, 9], 4)
        
        # Best order holds for a general list. All numbers in the list must be different.
        n = 100
        data = np.random.uniform(size=n)#np.random.permutation(n)
        sorted_data = sort(data)
        depth = int(np.floor(np.log2(n)))+1
        self._test_tree_depth(sorted_data[optimal_insertion_order(n)], depth)
        # Same code using a util function
        b = sequence_to_tree(sorted_data)
        assert_equal(b.depth(), depth, 'Wrong tree depth')
Example #2
0
 def test_alternating_order(self):
     '''Test alternating order generation.'''
     assert_equal(alternating_order(5), [2, 1, 3, 0, 4], 'Wrong alternating order for odd n')
     assert_equal(alternating_order(6), [3, 2, 4, 1, 5, 0], 'Wrong alternating order for even n')
     assert_equal(alternating_order(1), [0], 'Wrong alternating order for n=1')
     assert_equal(alternating_order(0), [], 'Wrong alternating order for n=0')