def test_inserting_unsorted_values_should_alter_root(self): """Testing the rotate methods""" rbt = RedBlackTree() rbt.insert(9) rbt.insert(11) rbt.insert(10) self.assertTrue(rbt.get_root().value == 10)
class RedblackMap(object): """An implementation of a hashtable using a BetterMap backed by a red black tree""" def __init__(self): self.map = RedBlackTree() logging.basicConfig( format="%(levelname)-10s %(asctime)s %(filename)s %(lineno)d %(message)s", level=logging.DEBUG ) self.log = logging.getLogger(sys.argv[0]) def get(self, k): """Looks up the key (k) and returns the corresponding value, or raises KeyError if the key is not found.""" return self.map.search_by_key(k) def add(self, k, v): """Resize the map if necessary and adds the new item.""" kv = KeyVal(k, v) self.map.insert(kv)
def test_redblacktree(): test_passed = True test_database = pickle.load( open( 'testdatabase.p', 'rb' ) ) test_database = test_database['redblacktree'] keys = test_database['keys'] result = test_database['result'] RBT = RedBlackTree() for key in keys: RBT.insert(key,None) nodes = [] nodes.append(RBT._root) nodes_checked = 0 while len(nodes) > 0: node = nodes.pop() if not node is None: nodes_checked += 1 nodes.append(node._left) nodes.append(node._right) target = result[node._key] if node._parent is None: if not target['p'] is None: test_passed = False else: if not node._parent._key == target['p']: test_passed = False if node._left is None: if not target['l'] is None: test_passed = False else: if not node._left._key == target['l']: test_passed = False if node._right is None: if not target['r'] is None: test_passed = False else: if not node._right._key == target['r']: test_passed = False if not node._black == target['b']: test_passed = False RBT = RedBlackTree() RBT.insert('1','a') RBT.insert('1','b') RBT.insert('1','c') if len(RBT.get('1')) != 3: test_passed = False return test_passed
else: csv_file = "Brooklyn_Public_Library_Catalog.csv" #Provide a script where you load the dataset print("Constructing dataset........", end='', flush=True) dataset = Dataset(csv_file, query_processor) dataset.load() print("Dataset Loaded!") #process it into an efficient binary search tree print("Constructing RB-Tree...", end='', flush=True) binary_search_tree = RedBlackTree() temp_dataset = dataset.get_token_bookids() for i in range(len(temp_dataset)): binary_search_tree.insert(temp_dataset[i][0], temp_dataset[i][1]) print("RB-Tree constructed!") #create a BK-tree print("Constructing BK-Tree...", end='', flush=True) bk_tree = BKTree(levenshtein_distance_DP) english_dictionary_list = dataset.get_dictionary() for i in range(len(english_dictionary_list)): bk_tree.insert(english_dictionary_list[i]) print("BK-Tree constructed!") if CONSTRUCT_DEBUG_MODE: pickle.dump(dataset, open("dataset.p", 'wb')) pickle.dump(binary_search_tree, open("binary_search_tree.p", 'wb')) pickle.dump(bk_tree, open("bk_tree.p", 'wb'))
def test_bfs(func, size): rbt = RedBlackTree() for i in range(1, size): rbt.insert(random_string(14)) this_func = eval(func) this_func(rbt.get_root(), visit_node)
def test_find_should_return_none_for_nonexistent_value(self): search_value = 5 rbt = RedBlackTree() for x in [7, 11, 19, 10]: rbt.insert(x) self.assertTrue(rbt.search(search_value) is None)
def test_find_should_work_for_existing_value(self): search_value = 9 rbt = RedBlackTree() for x in [7, 11, 19, 10, search_value]: rbt.insert(x) self.assertTrue(rbt.search(search_value))
def test_node_inserted_should_be_red(self): rbt = RedBlackTree() rbt.insert(9) rbt.insert(11) self.assertTrue(rbt.get_root().get_right().is_red())
def test_inserting_greater_value_should_create_right_node(self): rbt = RedBlackTree() rbt.insert(9) rbt.insert(11) self.assertTrue(rbt.get_root().get_right().value == 11)
def test_inserting_lesser_value_should_create_left_node(self): rbt = RedBlackTree() rbt.insert(5) rbt.insert(4) self.assertTrue(rbt.get_root().get_left().value == 4)
def test_first_node_should_be_black(self): rbt = RedBlackTree() rbt.insert(5) rbt.insert(4) self.assertTrue(rbt.get_root().is_black())