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")
def test_rbt(): rbt = RBT(None) rbt.add(1) assert rbt.root.val == 1 rbt.add(2) assert rbt.root.val == 2 rbt.add(3) assert rbt.root.val == 2 assert rbt.root.left.color == Color.BLACK assert rbt.root.right.color == Color.BLACK
def test_rbt_get_range(my_tree_2): rbt = RBT(my_tree_2) low = 1 high = 6 res = rbt.get_range(low, high) assert res == [3, 5, 6] low = 0 high = 20 res = rbt.get_range(low, high) assert res == [3, 5, 6, 8, 10, 11, 13] low = 20 high = 0 res = rbt.get_range(low, high) assert res == [] low = 6.5 high = 7 res = rbt.get_range(low, high) assert res == [] low = 8 high = 8 res = rbt.get_range(low, high) assert res == [8]
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")
def test_rbt_del_max(): rbt = RBT(None) rbt.add(1) rbt.add(3) rbt.del_max() assert rbt.find(3) == False assert rbt.find(1) == True
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")
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")
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
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")
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")
def test_client(): client = Client(bst=RBT(None)) answers = [] with open('tests/83') as f: n = int(f.readline()) for q in f.readlines(): res = client.request_from_file(q) if res != None: # convert to str for easier comparison answers.append(str(res)) with open('tests/83.a') as f: truth = [] for a in f.readlines(): truth.append(a.strip()) assert answers == truth
if __name__ == "__main__": data_structures = ['bst', 'rbt', 'hmap'] if len(argv) < 3: exit('usage: ./main.py --type <' + '|'.join(data_structures) + '>') chosen_data_structure = argv[2] if argv[1] == '--type' and argv[2] not in data_structures: exit('invalid options') # default data structure data_structure = RBT() if chosen_data_structure == 'bst': data_structure = BST() elif chosen_data_structure == 'hmap': data_structure = HashTable(size=11549) # measure many different thigns actions_performed_count = dict() actions_performed_total_time = dict() actions_available = ['insert', 'load', 'delete', 'find', 'min', 'max', 'successor', 'inorder'] for a in actions_available: actions_performed_count[a] = 0 actions_performed_total_time[a] = 0
from rbt import RBT from llist import LList from time import time from random import randint from sys import argv, exit if __name__ == "__main__": if len(argv) < 2: exit('usage ./ex-1-hash-table-chained-tests.py <items quantity>') # initialize both structures llist = LList() rbt = RBT() # add this quantity of items items_count = int(argv[1]) # 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()
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
def test_rbt_del_min(): rbt = RBT(None) rbt.add(1) assert rbt.find(1) == True rbt.add(2) assert rbt.find(2) == True assert rbt.find(3) == False # pudb.set_trace() rbt.del_min() assert rbt.find(1) == False assert rbt.find(2) == True
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")