Exemplo n.º 1
0
def test_rbt_delete():
    rbt = RBT(None)

    rbt.add(1)
    rbt.add(2)
    rbt.add(3)

    rbt.delete(2)
    assert rbt.find(2) == False
    assert rbt.find(1) == True
    assert rbt.find(3) == True

    rbt.add(2)
    rbt.delete(5)
    assert rbt.find(1) == True
    assert rbt.find(2) == True
    assert rbt.find(3) == False
Exemplo n.º 2
0
def test4():
    newTree = RBT()

    # insert into the RBT
    nums = [5, 7, 3, 6, 2, 8, 9, 0, 1, 10, 12, 11, 4]
    for i in nums:
        z = Node(nums[i])
        newTree.insert(z)
    newTree.printTree(newTree.root, 0)
    print("done inserting")

    # delete from the RBT
    scrambledNums = scrambled(nums)
    for i in scrambledNums:
        newTree.delete(scrambledNums[i])
        print("deleted: " + str(scrambledNums[i]))
    newTree.printTree(newTree.root, 0)
    print("done deleting")
Exemplo n.º 3
0
def test5():
    newTree = RBT()

    # insert into the RBT
    n = 10000
    nums = []
    while (len(nums) < n + 1):
        rand = random.randint(0, n)
        if rand not in nums:
            z = Node(rand)
            newTree.insert(z)
            nums.append(rand)
    newTree.printTree(newTree.root, 0)
    print("done inserting")

    # delete from the RBT
    for i in nums:
        newTree.delete(nums[i])
        print("deleted: " + str(nums[i]))
    newTree.printTree(newTree.root, 0)
    print("done deleting")
Exemplo n.º 4
0
        elif action == 'load':
            if data is None:
                print('file path is required')
            try:
                with open(data, 'r') as f:
                    for line in f:
                        for word in line.split():
                            parsed = parse_input_strings(word)
                            if len(parsed) > 0:
                                data_structure.insert(parsed)
            except FileNotFoundError as e:
                print('file not found', e)

        elif action == 'delete' and data is not None:
            data_structure.delete(data)

        elif action == 'find' and data is not None:
            print('1' if data_structure.find(data) is not None else '0')

        # following options are for BST and RBT only
        elif chosen_data_structure != 'hmap':
            if action == 'min':
                t = data_structure.minimum()
                if t is not None:
                    print(t.value)
                else:
                    print()

            elif action == 'max':
                t = data_structure.maximum()