Exemplo n.º 1
0
def test3():
    newTree = 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 printing")
Exemplo n.º 2
0
def test10():
    newTree = RBT()

    nums = [3, 6, 2, 4, 1, 7, 5]
    for i in nums:
        z = Node(nums[i])
        newTree.insert(z)

    newTree.printTree(newTree.root, 0)
    print("done printing")
Exemplo n.º 3
0
def test1():
    # put 15 elements on RBT
    newTree = RBT()
    n = 30
    used = []
    while (len(used) < n + 1):
        rand = random.randint(0, n)
        if rand not in used:
            z = Node(rand)
            newTree.insert(z)
            used.append(rand)

    newTree.checkTreePointers(newTree.root)
    newTree.printTree(newTree.root, 0)
    print("done printing")
Exemplo n.º 4
0
def test2():
    # test the cases for the tree from p.317
    newTree = RBT()

    nums = [11, 2, 14, 1, 7, 15, 5, 8, 4]
    colors = ["B", "R", "B", "B", "B", "R", "R", "R", "R"]
    for i in nums:
        z = Node(nums[i])
        z.c = colors[i]
        newTree.insert(z)

    newTree.printTree(newTree.root, 0)

    newTree.checkTreePointers(newTree.root)
    newTree.checkTreeProperties(newTree.root)
    print("done printing")
Exemplo n.º 5
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.º 6
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.º 7
0
    def insert(self, new_value: str) -> None:
        """Inserts a new value to the hash table.
        """
        index = self._get_index(new_value)
        # switch to RB-Tree when it seems necessary
        if self._data_length[index] == HASH_TABLE_RBT_SWITCH_THRESHOLD:
            # create a new RB-Tree and move all the values to it
            # from the original linked list
            rbt = RBT()
            node = self._data[index]._root
            while node is not None:
                rbt.insert(node.value)
                node = node.next
            self._data[index] = rbt
            # to avoid unnecessary switching to RB-Tree when it has already been done
            # make the data length basically unchangable
            # float('inf') + 123 = float('inf')
            self._data_length[index] = float('inf')

        self._data[index].insert(new_value)
        self._data_length[index] += 1

        self._nodes_count += 1
Exemplo n.º 8
0
def test0():
    # test the rotations ON rbt
    # x is root in L -> R, y is root in R -> L
    newTree = RBT()

    x = Node(4)
    y = Node(6)
    newTree.insert(x)
    newTree.insert(Node(3))
    newTree.insert(y)
    newTree.insert(Node(5))
    newTree.insert(Node(7))

    newTree.printTree(newTree.root, 0)
    print("done initial")

    newTree.leftRotate(x)
    newTree.printTree(newTree.root, 0)
    print("done left rotation")

    newTree.rightRotate(y)
    newTree.printTree(newTree.root, 0)
    print("done right rotation")
Exemplo n.º 9
0
    for line in stdin:
        # measure running time
        begin = time()

        # parse the inputted line
        input_data = line.split(' ', 1)
        action = input_data[0].replace('\n', '')
        data = None
        if len(input_data) > 1:
            data = input_data[1].replace('\n', '')

        # perform requested action
        if action == 'insert' and data is not None:
            parsed = parse_input_strings(data)
            if len(parsed) > 0:
                data_structure.insert(parsed)

        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:
Exemplo n.º 10
0
    # measure insertion times
    llist_insert_times = []
    rbt_insert_times = []

    for i in range(items_count):
        # t = i
        t = randint(0, items_count)
        # measure insertion time of a linked list
        begin = time()
        llist.insert(t)
        end = time()
        llist_insert_times.append(end-begin)
        # measure insertion time of a RB-Tree
        begin = time()
        rbt.insert(t)
        end = time()
        rbt_insert_times.append(end-begin)

    # calculate the average insertion time
    llist_insert_times_avg = sum(llist_insert_times)/len(llist_insert_times)
    rbt_insert_times_avg = sum(rbt_insert_times)/len(rbt_insert_times)

    # measure searching time
    t = randint(0, items_count)
    begin = time()
    x = llist.find(t)
    end = time()

    llist_find_time = end - begin