Example #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())
Example #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
Example #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.")
Example #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()
Example #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)
Example #7
0
 def test_search_unsuccesful(self):
     sl = SkipList.SkipList()
     sl.insert(1)
     self.assertIsNone(sl.search(2))
Example #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())
Example #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))

Example #10
0
from SkipList import *

sk = SkipList([3, 2, 23, 4, 2, -1, 4, 5, -13])
print(sk)
sk.erase(4)
print(sk)