Esempio n. 1
0
def test_uniquing():
    '''Test set with uniquing property'''
    all_good = True

    tree = red_black_set_mod.RedBlackTree()

    list_ = list(range(10)) + list(range(-5, 5)) + list(range(5, 15))
    for value in list_:
        tree.add(value)

        if tree.check() != True:
            sys.stderr.write(
                '%s: test_uniquing: tree does not check out ok\n' %
                (sys.argv[0], ))
            all_good = False
            return False

    set_ = set(list_)
    list_ = list(set_)
    list_.sort()

    list_of_tree = list(tree)
    if list_ != list_of_tree:
        sys.stderr.write('%s: test_uniquing: unique values are not correct\n' %
                         (sys.argv[0], ))
        print(list_)
        print(list_of_tree)
        all_good = False

    return all_good
Esempio n. 2
0
def test_similar():
    '''Test adding to a dictionary'''

    all_good = True

    dict_ = red_black_set_mod.RedBlackTree()
    for integer in range(5):
        dict_.add(integer)

    return all_good
Esempio n. 3
0
def test():
    '''Run the red black tree tests'''
    all_good = True

    rbset = red_black_set_mod.RedBlackTree()
    rbset_items = [6, 15, 16, 17, 5, 8, 0, 4, 10]
    for item in rbset_items:
        rbset.add(item)
    acopy = rbset.copy()

    dict_items = list(range(10))
    dict_ = red_black_dict_mod.RedBlackTree()
    for element in dict_items:
        dict_[element] = 2**element

    all_good &= test_entire_tree_ops(rbset, acopy)
    all_good &= test_adding(rbset, acopy)
    all_good &= test_tree_list_and_del_node(rbset, acopy)
    all_good &= test_min_and_max(rbset)
    all_good &= test_contains_and_find(rbset)
    all_good &= test_in_order(rbset, rbset_items)
    all_good &= test_pred_and_succ(dict_items, dict_)
    all_good &= test_check(rbset)
    all_good &= test_uniquing()
    all_good &= test_similar()
    all_good &= test_dict_add()
    all_good &= test_dict_find()
    all_good &= test_dict_setitem()
    all_good &= test_dict_getitem()
    all_good &= test_dict_delitem()
    all_good &= test_dict_iteritems()
    all_good &= test_dict_itervalues()
    all_good &= test_successor()

    if not all_good:
        sys.stderr.write('%s: One or more tests failed\n' % (sys.argv[0], ))
        sys.exit(1)
Esempio n. 4
0
'''
Created on Oct 2, 2014

@author: Calvin
'''
import red_black_set_mod
import Hash

keys = Hash.generateKeys(10000, 2000000000)
#case (i)...
table_one = Hash.HashTable(100000)
#case (ii)...
table_two = Hash.HashTable(1000)
#red black BST
rb_tree = red_black_set_mod.RedBlackTree()

#a) the inserts
table_one.insert(keys)
print "inserted 10,000 elements into table_one with 0 comparisons\n"

table_two.insert(keys)
print "inserted 10,000 elements into table_two with 0 comparisons\n"

totalCount = 0
for key in keys:
    node, count = rb_tree.add(key)
    totalCount += count
print "inserted 10,000 elements into rb_tree with {0} comparisons\n".format(
    str(totalCount))

#b) the deletes