Пример #1
0
def rbt_experiment(n, type='t'):
    tree = rbt.RBT()
    experiment_data = "{};".format(n)

    clk_start = time.time()
    results = get_results(n)
    for el in results:
        tree.insert(el)
    experiment_data += str(time.time()-clk_start)
    experiment_data += ';'

    clk_start = time.time()
    tree.insert("adelaida")
    experiment_data += str(time.time()-clk_start)
    experiment_data += ';'

    index = randint(0, len(results) - 1)
    clk_start = time.time()
    tree.delete(results[index])
    experiment_data += str(time.time()-clk_start)
    experiment_data += ';'

    index = randint(0, len(results) - 1)
    clk_start = time.time()
    tree.find(results[index])
    experiment_data += str(time.time()-clk_start)
    experiment_data += ';'

    clk_start = time.time()
    tree.tree_max(tree.root)
    experiment_data += str(time.time()-clk_start)
    experiment_data += ';'

    clk_start = time.time()
    tree.tree_min(tree.root)
    experiment_data += str(time.time()-clk_start)
    experiment_data += ';'

    index = randint(0, len(results) - 1)
    clk_start = time.time()
    tree.successor(results[index])
    experiment_data += str(time.time()-clk_start)
    experiment_data += ';'

    clk_start = time.time()
    tree.inorder_stat(tree.root)
    experiment_data += str(time.time()-clk_start)
    experiment_data += '\n'

    return experiment_data
Пример #2
0
    def load(self, file):
        t.load += 1
        try:
            with open(file, "r") as f:
                self.store = [rbt.RBT() for _ in range(400)]
                self.m = 400
                for line in f:
                    for word in line.split():
                        word = t.make_cut(word)
                        self.insert(word)
            return 1

        except FileNotFoundError:
            sys.stderr.write("File not found\n")
            return 0
Пример #3
0
def stat():
    with open('data/experiment.txt', 'r') as f:
        arg = f.read()
        arg = arg.split('\n')
    with open('data/with_dupl.txt', 'r') as f:
        arg_rep = f.read()
        arg_rep = arg_rep.split('\n')
    # "\n"
    arg.pop(-1)
    arg_rep.pop(-1)

    arg = arg[:900]
    arg_rep = arg_rep[:900]

    for n in range(100, 901, 100):
        words = arg.copy()
        words_rep = arg_rep.copy()

        for i in range(900 - n):
            words.pop(randint(0, (len(words) - 1)))
            words_rep.pop(randint(0, len(words_rep) - 1))

        root = bst.Node()
        rbt_root = rbt.RBT()
        hmap_root = hmap.HMAP_tree(n // 4)

        for i in range(len(words)):
            bst.insert(root, words[i])
            rbt_root.insert(words[i])
            hmap_root.insert(words[i])

        min_val = bst.tree_min(root).value
        max_val = bst.tree_max(root).value

        random_val = words[randint(0, len(words))]

        t.compares = 0
        bst.find(root, min_val)
        min_c = t.compares

        t.compares = 0
        bst.find(root, max_val)
        max_c = t.compares

        t.compares = 0
        bst.find(root, random_val)
        random_c = t.compares

        # n, minimum, maximum, random
        with open('data/bst_limits.txt', 'a') as f:
            f.write('{};{};{};{}\n'.format(n, min_c, max_c, random_c))

        t.compares = 0
        rbt_root.find(min_val)
        min_c = t.compares

        t.compares = 0
        rbt_root.find(max_val)
        max_c = t.compares

        t.compares = 0
        rbt_root.find(random_val)
        random_c = t.compares

        with open('data/rbt_limits.txt', 'a') as f:
            f.write('{};{};{};{}\n'.format(n, min_c, max_c, random_c))

        t.compares = 0
        hmap_root.find(min_val)
        min_c = t.compares

        t.compares = 0
        hmap_root.find(max_val)
        max_c = t.compares

        t.compares = 0
        hmap_root.find(random_val)
        random_c = t.compares

        with open('data/hmap_limits.txt', 'a') as f:
            f.write('{};{};{};{}\n'.format(n, min_c, max_c, random_c))

        root = bst.Node()
        rbt_root = rbt.RBT()
        hmap_root = hmap.HMAP_tree(n // 4)

        for i in range(len(words_rep)):
            bst.insert(root, words_rep[i])
            rbt_root.insert(words_rep[i])
            hmap_root.insert(words_rep[i])

        min_val = bst.tree_min(root).value
        max_val = bst.tree_max(root).value

        random_val = words[randint(0, len(words))]

        t.compares = 0
        bst.find(root, min_val)
        min_c = t.compares

        t.compares = 0
        bst.find(root, max_val)
        max_c = t.compares

        t.compares = 0
        bst.find(root, random_val)
        random_c = t.compares

        with open('data/bst_limits.txt', 'a') as f:
            f.write('{};{};{};{}\n'.format(n, min_c, max_c, random_c))

        t.compares = 0
        rbt_root.find(min_val)
        min_c = t.compares

        t.compares = 0
        rbt_root.find(max_val)
        max_c = t.compares

        t.compares = 0
        rbt_root.find(random_val)
        random_c = t.compares

        with open('data/rbt_limits.txt', 'a') as f:
            f.write('{};{};{};{}\n'.format(n, min_c, max_c, random_c))

        t.compares = 0
        hmap_root.find(min_val)
        min_c = t.compares

        t.compares = 0
        hmap_root.find(max_val)
        max_c = t.compares

        t.compares = 0
        hmap_root.find(random_val)
        random_c = t.compares

        with open('data/hmap_limits.txt', 'a') as f:
            f.write('{};{};{};{}\n'.format(n, min_c, max_c, random_c))
Пример #4
0
# Homework 06

import rbt as rb
import numpy as np

rbt = rb.RBT()
missed = []
with open('test01.txt') as inputfile:
    for line in inputfile:
        x = int(line)
        if x > 0:
            rbt.rb_insert(rb.Node(x))
        elif x < 0:
            min = rbt.bt_search(rbt.root, np.absolute(x))
            if min == -1:
                missed.append(x)
            else:
                rbt.rb_delete(min)
        else:
            break
inputfile.close()

inputfile = open('search01.txt', 'r')
outputfile = open('output01.txt', 'w')
for line in inputfile:
    x = int(line)
    rbt.search_write(x, outputfile)
outputfile.close()
inputfile.close()
Пример #5
0
 def __init__(self, m):
     self.store = [rbt.RBT() for _ in range(m)]
     self.m = m
Пример #6
0
def main():

    option = sys.argv

    if len(option) > 2:
        if option[1] == '--type':
            commands_count, commands = get_input()
            try:
                commands_count = int(commands_count)
            except ValueError:
                sys.stderr.write("Number of operations must be an integer!\n")
                return

            type = option[2]
            t.init_stats()
            clk_start = time.time()
            if type == 'bst':
                root = bst.Node()
                for command in commands:
                    try:
                        if len(command) != 0:
                            if command[0] == 'insert':
                                element = t.make_cut(command[1])
                                if element != -1:
                                    bst.insert(root, element)
                                else:
                                    sys.stderr.write("Invalid symbol in {} \n".format(element))
                                    continue
                            elif command[0] == 'load':
                                loaded = bst.load("data/{}".format(command[1]))
                                if loaded != 0:
                                    root = loaded
                            elif command[0] == 'delete':
                                result, new_root = bst.delete(root, command[1])
                                while result == 1:
                                    result, new_root = bst.delete(root, command[1])
                                    root = new_root
                            elif command[0] == 'find':
                                result, _ = bst.find(root, command[1])
                                print(result)
                            elif command[0] == 'min':
                                node = bst.tree_min(root)
                                if node.value != None:
                                    print(node.value)
                                else:
                                    print()
                            elif command[0] == 'max':
                                node = bst.tree_max(root)
                                if node.value != None:
                                    print(node.value)
                                else:
                                    print()
                            elif command[0] == 'successor':
                                result = bst.successor(root, command[1])
                                if result != 0:
                                    print(result)
                                else:
                                    print()
                            elif command[0] == 'inorder':
                                bst.inorder(root)
                                print()
                            else:
                                sys.stderr.write("Invalid command in ", command, ("\n"))
                                continue

                    except IndexError:
                        sys.stderr.write("Invalid command parameters in ", command, ("\n"))
                        continue

            elif type == 'rbt':
                tree = rbt.RBT()
                for command in commands:
                    if len(command) != 0:
                        if command[0] == 'insert':
                            element = t.make_cut(command[1])
                            if element != -1:
                                tree.insert(command[1])
                            else:
                                sys.stderr.write("Invalid symbol in {} \n".format(element))
                                continue
                        elif command[0] == 'load':
                            tree = rbt.RBT()
                            tree = tree.load("data/{}".format(command[1]))
                        elif command[0] == 'delete':
                            result = tree.delete(command[1])
                            while result == 1:
                                result = tree.delete(command[1])
                        elif command[0] == 'find':
                            result, _ = tree.find(command[1])
                            print(result)
                        elif command[0] == 'min':
                            minimum = tree.tree_min(tree.root)
                            if minimum != None:
                                print(minimum.value)
                            else:
                                print()
                        elif command[0] == 'max':
                            maximum = tree.tree_max(tree.root)
                            if maximum != None:
                                print(maximum.value)
                            else:
                                print()
                        elif command[0] == 'successor':
                            result = tree.successor(command[1]).value
                            if result != None:
                                print(result)
                            else:
                                print()
                        elif command[0] == 'inorder':
                            tree.inorder(tree.root)
                            print()
                        else:
                            sys.stderr.write("Invalid symbol in {} \n".format(command))
                            continue

            elif type == 'hmap':
                tree = hmap.HMAP_tree(100)
                for command in commands:
                    if len(command) != 0:
                        if command[0] == 'insert':
                            element = t.make_cut(command[1])
                            if element != -1:
                                tree.insert(command[1])
                            else:
                                sys.stderr.write("Invalid symbol in {} \n".format(element))
                                continue

                        elif command[0] == 'load':
                            result = tree.load("data/{}".format(command[1]))
                        elif command[0] == 'delete':
                            result = tree.delete(command[1])
                            while result == 1:
                                result = tree.delete(command[1])
                        elif command[0] == 'find':
                            print(tree.find(command[1]))
                        elif command[0] == 'min':
                            print()
                        elif command[0] == 'max':
                            print()
                        elif command[0] == 'successor':
                            print()
                        elif command[0] == 'inorder':
                            print()
                        else:
                            sys.stderr.write("Invalid symbol in {} \n".format(command))
                            continue


            else:
                sys.stderr.write("Invalid structure! Available: bst, rbt or hmap!\n")

            sys.stderr.write("\n")
            sys.stderr.write("Total time of excecution {}\n".format(time.time()-clk_start))
            print_results()

        else:
            sys.stderr.write("Invalid argument! Use --type !\n")
    else:
        sys.stderr.write("Not enough arguments! \n")