Пример #1
0
for item in myList:
    myAVLTree.put(item, item)
end = clock()
runTime = end - start
print("Time to add items 0 to %d is %.4f sec.\n" % (testSize - 1, runTime))

print("Starting to search for 0 to", testSize - 1)
start = clock()
for target in range(testSize):
    target in myAVLTree
# end for
end = clock()
runTime = end - start
print("Time to search for targets 0 to %d is %.4f sec.\n" %
      (testSize - 1, runTime))
print("AVL Tree Height after inserting in sorted order:", myAVLTree.height())
print("\n==============================================================\n")

myAVLTree = AVLTree()
testSize = 10000  # Number of items to search consisting of
myList = list(range(testSize))
shuffle(myList)
print("Starting to add SHUFFLED items 0 to", testSize - 1)
start = clock()
for item in myList:
    myAVLTree.put(item, item)
end = clock()
runTime = end - start
print("Time to add items 0 to %d is %.4f sec.\n" % (testSize - 1, runTime))

print("Starting to search for 0 to", testSize - 1)
Пример #2
0
def main():
    print "-:=Test AVL trees=:-"
    if (len(sys.argv) != 6):
        print "Use: ./test_avl.py <words_file_name> <meanings_file_name> <number_of_insertions> <number_of_search> <number of delete>"
        sys.exit(0)
    else:
        words_file_name = sys.argv[1]
        meanings_file_name = sys.argv[2]
        print "Words file: " + words_file_name + "\nMeanings file: " + meanings_file_name
        inserts = int(sys.argv[3])
        searchs = int(sys.argv[4])
        deletes = int(sys.argv[5])

        sys.stdout.write("Inserts: %s\t" % inserts)
        sys.stdout.write("Searchs: %s\t" % searchs)
        sys.stdout.write("Deletes: %s\n" % deletes)

        f = open(words_file_name, "r")
        f2 = open(meanings_file_name, "r")
        """ INSERTS PART """
        avl = AVLTree(node=AVLNode())
        #print "Is an empty tree: ", avl.is_empty()
        #print "Tree height: ", avl.height()
        t0_1 = time.time()
        t0 = time.strftime('%s')
        words = []
        for i in range(0, inserts):
            word = f.readline()
            #print "word read: " , word.strip()
            definition = f2.readline()
            #print "meaning read: ", definition.strip()
            if not word: break
            if not definition: break
            words.append(word.strip())
            print "words: ", words
            avl.modify(word.strip(), definition.strip())

        t1 = time.strftime('%s')
        print "words: ", words
        if inserts > 0:
            t1_1 = time.time()
            t_insert = t1_1 - t0_1
            print "Insertion process ends successfully! -> time ", t_insert
            print ""
        """ SEARCH PART """
        if searchs > 0:
            t0 = time.time()
            for s in range(0, searchs):
                #word = f.readline()
                word = words[random.randint(0, len(words) - 1)]
                avl.search(word)
            t1 = time.time()
            t_search = t1 - t0
            print "Search operations done! -> time ", t_search
            print ""
        """ DELETE PART """
        if deletes > 0 and deletes <= inserts:
            t0 = time.time()
            for s in range(0, deletes):
                word = words[random.randint(0, len(words) - 1)]
                print 'word to delete: ', word
                avl.delete(word)
            t1 = time.time()
            t_delete = t1 - t0
            print "Delete operations done! -> time ", t_delete
            print ""

        # Closing file descriptors
        f.close()
        f2.close()
        print "Resultant tree:"
        avl.draw_tree()
        print "Height tree: ", avl.height()