Ejemplo n.º 1
0
def find(root, value):
    if root is None: return None

    if root.value > value:
        return find(root.left, value)
    if root.value < value:
        return find(root.right, value)
    return root
Ejemplo n.º 2
0
def bst_experiment(n, type='t'):
    root = bst.Node()

    experiment_data = "{};".format(n)

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

    clk_start = time.time()
    bst.insert(root, "adelaida")
    experiment_data += str(time.time()-clk_start)
    experiment_data += ';'

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

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

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

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

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


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

    return experiment_data
Ejemplo n.º 3
0
def testInOrderInsert():
    root = bst.Node(500)
    for i in range(1000):
        bst.insert(root, i, 0)

    if bst.find(root, 100) is None:
        print("Shouldn't be None")
    else:
        print('Found 100')
    bst.inorder(root)
Ejemplo n.º 4
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))
Ejemplo n.º 5
0
import bst

tree = bst.create(2)
bst.add(tree, 6)
bst.add(tree, 7)
bst.add(tree, 2)
bst.add(tree, 5)
bst.add(tree, 3)
bst.add(tree, 8)
bst.add(tree, 9)

# print(tree)
print(bst.find(tree, 5))
Ejemplo n.º 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")
Ejemplo n.º 7
0
def testHeight():
    root = bst.Node(500)
    for i in range(1000):
        bst.insert(root, i, 0)
    print(bst.find(root, 199).height)