def _rehash(self): """ --------------------------------------------------------- Increases the number of slots in the hashset and reallocates the existing data within the hashset to the new table. Use: hs._rehash() ------------------------------------------------------- Postconditions: Existing data is reallocated amongst the hashset table. ------------------------------------------------------- """ slots = self._slots * 2 + 1 add_slots = slots - self._slots for _ in range(add_slots): self._table.append(BST()) self._slots += 1 for slot in self._table: for key in slot: new_slot = self._find_slot(key) new_slot.insert(key) index = slot.index(key) slot.pop(index) self._total = self._LOAD_FACTOR * self._slots return
def bst_sort(a): """ ------------------------------------------------------- Sorts an array using the Tree Sort algorithm. Use: Sorts.bst_sort(a) ------------------------------------------------------- Preconditions: a - an array of comparable elements (?) Postconditions: Contents of a are sorted. ------------------------------------------------------- """ bst = BST() for v in a: bst.insert(v) a[:] = bst.inorder() return
def __init__(self, slots): """ ------------------------------------------------------- Initializes an empty HashSet of size slots. Use: hs = HashSet(slots) ------------------------------------------------------- Precondition: slots - number of initial slots in hashset (int > 0) Postconditions: Initializes an empty HashSet. ------------------------------------------------------- """ self._slots = slots self._table = [] self._total = int(self._slots * self._LOAD_FACTOR) for _ in range(self._slots): self._table.append(BST()) self._count = 0 return
""" ------------------------------------------------------- [filename].py [description of main program] ------------------------------------------------------- Author: Mason Cooper ID: 140328200 Email: [email protected] Version: 2015-03-20 ------------------------------------------------------- """ from utilities2 import array_to_bst, array_to_avl from bst_linked import BST from avl_linked import AVL bst = BST() avl = AVL() a = ['a', 'b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',] array_to_bst(a, bst) bst.levelorder() array_to_avl(a, avl) print(avl.is_valid())
_updated_="2018-03-15" ---------------------------------------------------- """ from bst_linked import BST from letter import Letter from asgn08 import comparison_total, do_comparisons def fill_letter_bst(bst, data): for i in data: letter = Letter(i) bst.insert(letter) return bst1 = BST() bst2 = BST() bst3 = BST() DATA1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" DATA2 = "MFTCJPWADHKNRUYBEIGLOQSVXZ" DATA3 = "ETAOINSHRDLUCMPFYWGBVKJXZQ" fill_letter_bst(bst1, DATA1) fill_letter_bst(bst2, DATA2) fill_letter_bst(bst3, DATA3) file_variable = open('gibbon.txt', 'r') do_comparisons(file_variable, bst1) do_comparisons(file_variable, bst2)
""" ---------------------------------------------------- q3.py Prints letter table ---------------------------------------------------- Author: Qadeer Assan ID: 160257370 Email: [email protected] _updated_="2018-03-16" ---------------------------------------------------- """ from asgn08 import letter_table, do_comparisons from bst_linked import BST from letter import Letter data = "ETAOINSHRDLUCMPFYWGBVKJXZQ" bst = BST() def fill_letter_bst(bst, data): for i in data: letter = Letter(i) bst.insert(letter) return fill_letter_bst(bst, data) file_variable = open('gibbon.txt', 'r') do_comparisons(file_variable, bst) letter_table(bst)
def setUp(self): self.m_sut = BST() pass
class TestBST(unittest.TestCase): def setUp(self): self.m_sut = BST() pass def testMinIterativeEmpty(self): actual = self.m_sut.min_i() self.assertIsNone( actual, "Not the correct maximum") def testMinIterativeSingleNode(self): value = random.randint(0,100) self.m_sut.insert(value) actual = self.m_sut.min_i() self.assertEqual(value, actual, "Not the correct maximum") def testMinIterativeFullTree(self): values = [41, 53, 76, 7, 59, 19, 92, 47, 43, 1, 13] for i in values: self.m_sut.insert(i) expected = min(values) actual = self.m_sut.min_i() self.assertEqual(expected, actual, "Not the correct maximum") def testMaxRecursiveEmpty(self): actual = self.m_sut.max_r() self.assertIsNone( actual, "Not the correct maximum") def testMaxRecursiveSingleNode(self): value = random.randint(0,100) self.m_sut.insert(value) actual = self.m_sut.max_r() self.assertEqual(value, actual, "Not the correct maximum") def testMaxRecursiveFullTree(self): values = [41, 53, 76, 7, 59, 19, 92, 47, 43, 1, 13] for i in values: self.m_sut.insert(i) expected = max(values) actual = self.m_sut.max_r() self.assertEqual(expected, actual, "Not the correct maximum") def testBST_delete_NoChildren(self): values = [41, 53, 76, 7, 59, 19, 92, 47, 43, 1, 13] for i in values: self.m_sut.insert(i) to_delete = 1 actual = self.m_sut.remove(to_delete) self.assertEqual(to_delete, actual, "Did not return correct deleted element") return def testBST_delete_EmptyBst(self): values = [] for i in values: self.m_sut.insert(i) actual = self.m_sut.remove(1) self.assertIsNone( actual, "Did not return correct deleted element") return def testBST_delete_SingleNode(self): values = [1] for i in values: self.m_sut.insert(i) to_delete = 1 actual = self.m_sut.remove(to_delete) self.assertEqual( to_delete,actual, "Did not return correct deleted element") return def testBST_delete_RightChild(self): values = [1,2] for i in values: self.m_sut.insert(i) to_delete = 1 actual = self.m_sut.remove(to_delete) self.assertEqual( to_delete,actual, "Did not return correct deleted element") return def testBST_delete_LeftChild(self): values = [2,1] for i in values: self.m_sut.insert(i) to_delete = 2 actual = self.m_sut.remove(to_delete) self.assertEqual( to_delete,actual, "Did not return correct deleted element") return def testBST_delete_node_one_child(self): values = [41, 53, 76, 7, 59, 19, 92, 47, 43, 1, 13] for i in values: self.m_sut.insert(i) to_delete = 19 actual = self.m_sut.remove(to_delete) self.assertEqual(to_delete, actual, "Did not return correct deleted element") def testBST_delete_two_children(self): values = [41, 53, 76, 7, 59, 19, 92, 47, 43, 1, 13] for i in values: self.m_sut.insert(i) to_delete = 53 actual = self.m_sut.remove(to_delete) self.assertEqual(to_delete, actual, "Did not return correct deleted element") return def testBST_delete_node_node_not_in_Tree(self): values = [41, 53, 76, 7, 59, 19, 92, 47, 43, 1, 13] for i in values: self.m_sut.insert(i) to_delete = 99 actual = self.m_sut.remove(to_delete) self.assertIsNone(actual, "Did not return correct deleted element")