def load_trees(entries): solutions_tree, student_tree = BST(), s.BST() for i in entries: solutions_tree.insert(i) student_tree.insert(i) if str(solutions_tree) != str(student_tree): raise NotImplementedError("BST.remove() cannot be tested " "until BST.insert() is correct") return solutions_tree, student_tree
def problem2(self, s): """Test BST.insert(). 15 Points.""" points = 0 # Empty tree (0 pts) tree1, tree2 = BST(), s.BST() self._strTest(tree1, tree2, "BST() failed initially!") # Inserting root (2 pts) tree1.insert(4) tree2.insert(4) points += 2 * self._strTest( tree1, tree2, "BST.insert(4) failed " "on root insertion.\nPrevious tree:\n[]") def test_insert(value, solTree, stuTree): oldTree = "\nPrevious tree:\n{}".format(solTree) solTree.insert(value) stuTree.insert(value) p = self._strTest(tree1, tree2, "BST.insert({}) failed{}".format(value, oldTree)) return p, solTree, stuTree # Inserting nonroot (9 pts) for i in [2, 1, 3, 10, 5, 6, 9, 7, 11]: p, tree1, tree2 = test_insert(i, tree1, tree2) points += p if p == 0: break if points >= 11: # Inserting already existing value (4 pts) def test_duplicate(value, stuTree): try: stuTree.insert(value) self.feedback += "\n\tBST.insert({}) failed ".format(value) self.feedback += "for {} already in tree".format(value) return 0 except ValueError: return 1 points += test_duplicate(4, tree2) points += test_duplicate(1, tree2) points += 2 * test_duplicate(7, tree2) else: self.feedback += "\nAll BST.remove() tests are likely to fail" self.feedback += "\n\tunless all BST.insert() tests pass!" return points
def problem2(self, s): """Test BST.insert(). 15 Points.""" points = 0 # Empty tree (0 pts) tree1, tree2 = BST(), s.BST() self._strTest(tree1, tree2, "BST() failed initially!") # Inserting root (2 pts) tree1.insert(4); tree2.insert(4) points += 2*self._strTest(tree1, tree2, "BST.insert(4) failed " "on root insertion.\nPrevious tree:\n[]") def test_insert(value, solTree, stuTree): oldTree = "\nPrevious tree:\n{}".format(solTree) solTree.insert(value); stuTree.insert(value) p = self._strTest(tree1, tree2, "BST.insert({}) failed{}".format(value, oldTree)) return p, solTree, stuTree # Inserting nonroot (9 pts) for i in [2, 1, 3, 10, 5, 6, 9, 7, 11]: p, tree1, tree2 = test_insert(i, tree1, tree2) points += p if p == 0: break if points >= 11: # Inserting already existing value (4 pts) def test_duplicate(value, stuTree): try: stuTree.insert(value) self.feedback += "\n\tBST.insert({}) failed ".format(value) self.feedback += "for {} already in tree".format(value) return 0 except ValueError: return 1 points += test_duplicate(4, tree2) points += test_duplicate(1, tree2) points += 2*test_duplicate(7, tree2) else: self.feedback += "\nAll BST.remove() tests are likely to fail" self.feedback += "\n\tunless all BST.insert() tests pass!" return points
def prob4_plots(N=12, verbose=False): """At each iteration, take n random items from a pre-determined subset. Time (separately) how long it takes to load a SinglyLinkedList, a BST, and an AVL with the data set of n items. Choose 5 random items from the data set. Time (separately) how long it takes to find all 5 items in each object. Create two log-log figures. The first figure plots the number of items in each dataset against the build time for each object. The second figure, plots the number of items against the search time for each object. """ # Initialize lists to hold results lls_build, lls_search = [], [] bst_build, bst_search = [], [] avl_build, avl_search = [], [] data = np.random.random(2**(N+1)) domain = 2**np.arange(3,N+1) # Get the values [start, start + step, ..., stop - step] for n in domain: if verbose: print("\rn = {}".format(n)), stdout.flush() # Initialize wordlist and data structures subset = data[:n] bst = BST() avl = AVL() lls = SinglyLinkedList() # Time the singly-linked list build begin = time() for item in subset: lls.append(item) lls_build.append(time() - begin) # Time the binary search tree build begin = time() for item in subset: bst.insert(item) bst_build.append(time() - begin) # Time the AVL tree build begin = time() for item in subset: avl.insert(item) avl_build.append(time() - begin) random_subset = np.random.choice(subset, size=5, replace=False) # Time the singly-linked list search begin = time() for target in random_subset: iterative_search(lls, target) lls_search.append(time() - begin) # Time the binary search tree search begin = time() for target in random_subset: bst.find(target) bst_search.append(time() - begin) # Time the AVL tree search begin = time() for target in random_subset: avl.find(target) avl_search.append(time() - begin) # Plot the data plt.clf() plt.title("Build Times") plt.loglog(domain,lls_build,'.-',lw=2,ms=10,basex=2,basey=2,label='Singly Linked List') plt.loglog(domain,bst_build,'.-',lw=2,ms=10,basex=2,basey=2,label='Binary Search Tree') plt.loglog(domain,avl_build,'.-',lw=2,ms=10,basex=2,basey=2,label='AVL Tree') plt.xlabel("n"); plt.ylabel("Seconds") plt.legend(loc='upper left') plt.savefig("BuildTimes.pdf") plt.clf() plt.title("Search Times") plt.loglog(domain,lls_search,'.-',lw=2,ms=10,basex=2,basey=2,label='Singly Linked List') plt.loglog(domain,bst_search,'.-',lw=2,ms=10,basex=2,basey=2,label='Binary Search Tree') plt.loglog(domain,avl_search,'.-',lw=2,ms=10,basex=2,basey=2,label='AVL Tree') plt.xlabel("n") plt.legend(loc='upper left') plt.savefig("SearchTimes.pdf") plt.clf()
def prob4_plots(N=12, verbose=False): """At each iteration, take n random items from a pre-determined subset. Time (separately) how long it takes to load a SinglyLinkedList, a BST, and an AVL with the data set of n items. Choose 5 random items from the data set. Time (separately) how long it takes to find all 5 items in each object. Create two log-log figures. The first figure plots the number of items in each dataset against the build time for each object. The second figure, plots the number of items against the search time for each object. """ # Initialize lists to hold results lls_build, lls_search = [], [] bst_build, bst_search = [], [] avl_build, avl_search = [], [] data = np.random.random(2**(N + 1)) domain = 2**np.arange(3, N + 1) # Get the values [start, start + step, ..., stop - step] for n in domain: if verbose: print("\rn = {}".format(n)), stdout.flush() # Initialize wordlist and data structures subset = data[:n] bst = BST() avl = AVL() lls = SinglyLinkedList() # Time the singly-linked list build begin = time() for item in subset: lls.append(item) lls_build.append(time() - begin) # Time the binary search tree build begin = time() for item in subset: bst.insert(item) bst_build.append(time() - begin) # Time the AVL tree build begin = time() for item in subset: avl.insert(item) avl_build.append(time() - begin) random_subset = np.random.choice(subset, size=5, replace=False) # Time the singly-linked list search begin = time() for target in random_subset: iterative_search(lls, target) lls_search.append(time() - begin) # Time the binary search tree search begin = time() for target in random_subset: bst.find(target) bst_search.append(time() - begin) # Time the AVL tree search begin = time() for target in random_subset: avl.find(target) avl_search.append(time() - begin) # Plot the data plt.clf() plt.title("Build Times") plt.loglog(domain, lls_build, '.-', lw=2, ms=10, basex=2, basey=2, label='Singly Linked List') plt.loglog(domain, bst_build, '.-', lw=2, ms=10, basex=2, basey=2, label='Binary Search Tree') plt.loglog(domain, avl_build, '.-', lw=2, ms=10, basex=2, basey=2, label='AVL Tree') plt.xlabel("n") plt.ylabel("Seconds") plt.legend(loc='upper left') plt.savefig("BuildTimes.pdf") plt.clf() plt.title("Search Times") plt.loglog(domain, lls_search, '.-', lw=2, ms=10, basex=2, basey=2, label='Singly Linked List') plt.loglog(domain, bst_search, '.-', lw=2, ms=10, basex=2, basey=2, label='Binary Search Tree') plt.loglog(domain, avl_search, '.-', lw=2, ms=10, basex=2, basey=2, label='AVL Tree') plt.xlabel("n") plt.legend(loc='upper left') plt.savefig("SearchTimes.pdf") plt.clf()