예제 #1
0
 def test_delete_between(self):
     even = range(0, 20, 2)
     odd = range(1, 20, 2)
     numbers = even + odd
     numbers.sort()
     sl = SkipList.SkipList()
     for number in numbers:
         sl.insert(number)
     for number in odd:
         sl.delete(number)
     self.assertEqual(even, sl.skip_list_as_list())
예제 #2
0
def test_skip_list():
    skip_list = SkipList.SkipList()
    i = 0
    for i in range(10):
        skip_list.insert(i)
        i += 1
    skip_list.print_list()
    skip_list.print_nice()
    print
    for i in range(10):
        skip_list.delete(i)
        skip_list.print_nice()
        print
예제 #3
0
def compare_insertion_and_search():
    rounds = int(raw_input('How many rounds of test? '))
    insertions = int(
        raw_input('How many insertions (nodes in the structure)? '))
    top_percent = float(
        raw_input('How many percent of the values are searched? '))
    repeat = int(raw_input('How many times those values are searched? '))

    values = random.sample(range(insertions * 10), insertions)
    search_values = random.sample(values, int(top_percent * insertions / 100))
    searches = search_values * repeat
    random.shuffle(searches)

    for round in range(rounds):

        print '-' * 20
        sl = SkipList.SkipList()
        st = SplayTree.SplayTree()
        t = Treap.Treap()
        structures = [(sl, 'Skip list'), (st, 'Splay tree'), (t, 'Treap')]

        print '\nInsertions:'
        for structure in structures:
            time_begin = time.time()
            for key in range(insertions):
                structure[0].insert(key)
            print('Inserting ' + str(insertions) + ' values to empty ' +
                  structure[1] + ' took ' + str(time.time() - time_begin) +
                  ' seconds')

        print '\nSearches:'

        time_begin = time.time()
        for key in searches:
            sl.search(key)
        print('Searching ' + str(len(searches)) +
              ' values from skip list took ' + str(time.time() - time_begin) +
              ' seconds')

        time_begin = time.time()
        for key in searches:
            st.search(st.root, key)
        print('Searching ' + str(len(searches)) +
              ' values from splay tree took ' + str(time.time() - time_begin) +
              ' seconds')

        time_begin = time.time()
        for key in searches:
            t.search(t.root, key)
        print('Searching ' + str(len(searches)) + ' values from treap took ' +
              str(time.time() - time_begin) + ' seconds')
def main():

    # if the user sent specified 1 thing for the commandline
    if (len(sys.argv) == 2):

        # try to convert the thing to a positve number.
        try:

            count = int(sys.argv[1])

            if count < 0:
                sys.exit(0)
        except:
            print("Please enter in a positive whole number")
            sys.exit(0)

        #!!!Ziptree Test!!!

        start_time = time.time()

        ziptree = ZipTree(None)

        for i in range(count):
            temp = node(random.randint(0, count), None, None)
            ziptree.insert(temp)

        print("--- %s bytes --- ZipTree" % ziptree.getTreeSize())

        for i in range(count):
            temp = ziptree.find(i)
            while (temp is not None):
                ziptree.delete(temp)
                temp = ziptree.find(i)

        print("--- %s seconds --- ZipTree" % (time.time() - start_time))

        #!!!Skiplist Test!!!

        start_time = time.time()

        skiplist = SkipList(count, 0.5)

        for i in range(count):
            skiplist.insertElement(random.randint(0, count))

        print("--- %s bytes --- SkipList" % skiplist.getListSize())

        for i in range(count):
            while (skiplist.searchElement(i) is True):
                skiplist.deleteElement(i)

        print("--- %s seconds --- SkipList" % (time.time() - start_time))

        #!!!Treap Test!!!

        start_time = time.time()

        treap = Treap()

        for i in range(count):
            treap.insert(random.randint(0, count))

        print("--- %s bytes --- Treap" % treap.getTreeSize())

        for i in range(count):
            while (treap.find(i) is not None):
                treap.delete(i)

        print("--- %s seconds --- Treap" % (time.time() - start_time))

    else:
        print("This program takes in 1 postive integer value.")
예제 #5
0
# import random2 as r2 # learn more: https://python.org/pypi/random2
from SkipList import *

# print(r2.random())

s1 = SkipList()
n1 = NodoSkip(1)
s1.getCabeza()
# s1.inserta(n1)
# s1.print()
예제 #6
0
 def test_search_succesful(self):
     sl = SkipList.SkipList()
     for number in range(100):
         sl.insert(number)
     for number in [0, 11, 17, 27, 99, 50, 11, 17]:
         self.assertTrue(sl.search(number) == number)
예제 #7
0
 def test_search_unsuccesful(self):
     sl = SkipList.SkipList()
     sl.insert(1)
     self.assertIsNone(sl.search(2))
예제 #8
0
 def test_skip_list_as_list(self):
     sl = SkipList.SkipList()
     for key in range(10):
         sl.insert(key)
     self.assertEqual(range(10), sl.skip_list_as_list())
예제 #9
0
### Example ###

from SkipList import *

## Insert Keys

# Create a Skip List with N set to the 
# desired number of elements to be stored
a = SkipList(20)
lis = list(range(20, 0,-1))
for key in lis:
    a.insert(key)   
a.displayList()

## Check if a key is present
print ("\nDoes the list contain 3?", a.contains(3))

## Remove a key
a.remove(3)
a.displayList()
print ("\nDoes the list contain 3?", a.contains(3))

## Return the length of the skip list
print ("\nThe length of the skip list is :", len(a))

예제 #10
0
from SkipList import *

sk = SkipList([3, 2, 23, 4, 2, -1, 4, 5, -13])
print(sk)
sk.erase(4)
print(sk)
예제 #11
0
def main(elements, adjust, exp):
    '''
    Principal
    '''
    insertions         = elements
    searches           = int(0.5*elements)
    not_found_searches = int(0.25*searches)
    swaps_amount       = int(0.05*elements) # aumentando la cantidad de swaps de cada iteracion
    init               = False
    analizer           = ExperimentAnalyzer(exp)
    print('n :' + str(elements))
    print('experiments: ' + str(exp))

    for e in range(exp):
        sequence           = RandomSequence(elements, 10**5)

        while (not init) or abb_comps - abbr_comps > abbr_comps*adjust:
            if not init:
                mean_height = 0
                iteration   = 0
                init = True

            # Número de iteracion para acercar comportamiento de árboles
            iteration += 1
            # print('.'),
            sys.stdout.flush()

            # Se definen las estructuras
            abb_tree  = ABB()
            abbr_tree = ABBRandom()
            skip_list = SkipList()

            # Contadores
            abb_comps    = 0
            abbr_comps   = 0
            skip_comps   = 0

            abb_search_comps    = 0
            abbr_search_comps   = 0
            skip_search_comps   = 0

            # Lista para ABB
            abb_elements = sequence.k_swaps(swaps_amount)

            # Inserciones
            for i in range(insertions):
                abb_comps  += abb_tree.insert(abb_elements[i])
                abbr_comps += abbr_tree.insert(sequence.get_element(i))
                skip_comps += skip_list.insert(sequence.get_element(i))

            # Busquedas
            for i in range(searches):
                if i < not_found_searches:
                    elem = sequence.get_random_free_element()
                else:
                    elem = sequence.get_random_element()
                abb_search_comps  += abb_tree.search(elem)[1]
                abbr_search_comps += abbr_tree.search(elem)[1]
                skip_search_comps += skip_list.search(elem)[1]

            # Altura de skip list
            mean_height += 1.0*skip_list.max_height

            # Siguiente iteración
            sequence.elements = abb_elements

            # print(str(1.0*abb_comps/insertions) + ' ' + str(1.0*abbr_comps/insertions) + ' ' + str(1.0*skip_comps/insertions))

        # Suma de resultados promedio (n operaciones)
        # Resultados: Inserciones
        abb_comps  = 1.0*abb_comps/insertions
        abbr_comps = 1.0*abbr_comps/insertions
        skip_comps = 1.0*skip_comps/insertions

        # Resultados: Busquedas
        abb_search_comps  = 1.0*abb_search_comps/searches
        abbr_search_comps = 1.0*abbr_search_comps/searches
        skip_search_comps = 1.0*skip_search_comps/searches

        # Resultados: Altura de Skip List
        mean_height = 1.0*mean_height/iteration

        print('\ninsertions: ' + str(abb_comps) + ',' + str(abbr_comps) + ',' + str(skip_comps))
        print('searches: ' + str(abb_search_comps) + ',' + str(abbr_search_comps) + ',' + str(skip_search_comps))
        print('skiplist height: ' + str(mean_height))
        print('total swaps: ' + str(swaps_amount*iteration))

        analizer.add_abb_result(abb_comps, abb_search_comps, swaps_amount*iteration)
        analizer.add_abbr_result(abbr_comps, abbr_search_comps)
        analizer.add_skip_list_result(skip_comps, skip_search_comps, mean_height)

        init = False

    print('\n')
    analizer.compute_errors()
    analizer.show_results()
    print('\n')
예제 #12
0
def main(elements, exp):
    '''
    Principal
    '''
    insertions         = elements
    searches           = int(0.5*elements)
    not_found_searches = int(0.25*searches)
    analizer           = ExperimentAnalyzer(exp)
    print('n :' + str(elements))
    print('experiments: ' + str(exp))

    for e in range(exp):
        sequence = RandomSequence(elements, 10**5)
        swaps_amount = int(0.7*elements) # swaps

        # Contadores
        abb_comps    = 0
        abbr_comps   = 0
        skip_comps   = 0

        abb_search_comps    = 0
        abbr_search_comps   = 0
        skip_search_comps   = 0

        # Se definen las estructuras
        abb_tree  = ABB()
        abbr_tree = ABBRandom()
        skip_list = SkipList()

        # Lista para ABB
        abb_elements = sequence.k_swaps(swaps_amount)

        # Inserciones
        for i in range(insertions):
            abb_comps  += abb_tree.insert(abb_elements[i])
            abbr_comps += abbr_tree.insert(abb_elements[i])
            skip_comps += skip_list.insert(abb_elements[i])

        # Busquedas
        for i in range(searches):
            if i < not_found_searches:
                elem = sequence.get_random_free_element()
            else:
                elem = sequence.get_random_element()
            abb_search_comps  += abb_tree.search(elem)[1]
            abbr_search_comps += abbr_tree.search(elem)[1]
            skip_search_comps += skip_list.search(elem)[1]

        # Altura de skip list
        mean_height = 1.0*skip_list.max_height

        # Suma de resultados promedio (n operaciones)
        # Resultados: Inserciones
        abb_comps  = 1.0*abb_comps/insertions
        abbr_comps = 1.0*abbr_comps/insertions
        skip_comps = 1.0*skip_comps/insertions

        # Resultados: Busquedas
        abb_search_comps  = 1.0*abb_search_comps/searches
        abbr_search_comps = 1.0*abbr_search_comps/searches
        skip_search_comps = 1.0*skip_search_comps/searches

        print('\ninsertions: ' + str(abb_comps) + ',' + str(abbr_comps) + ',' + str(skip_comps))
        print('searches: ' + str(abb_search_comps) + ',' + str(abbr_search_comps) + ',' + str(skip_search_comps))
        print('skiplist height: ' + str(mean_height))
        print('total swaps: ' + str(swaps_amount))

        analizer.add_abb_result(abb_comps, abb_search_comps, swaps_amount)
        analizer.add_abbr_result(abbr_comps, abbr_search_comps)
        analizer.add_skip_list_result(skip_comps, skip_search_comps, mean_height)

    analizer.compute_errors()
    analizer.show_results()
    print('\n')