def test_check_balance(self): bst = BstBalance(Node(5)) assert_equal(bst.check_balance(), True) bst.insert(3) bst.insert(8) bst.insert(1) bst.insert(4) assert_equal(bst.check_balance(), True) bst = BstBalance(Node(5)) bst.insert(3) bst.insert(8) bst.insert(9) bst.insert(10) assert_equal(bst.check_balance(), False) bst = BstBalance(Node(3)) bst.insert(2) bst.insert(1) bst.insert(5) bst.insert(4) bst.insert(6) bst.insert(7) assert_equal(bst.check_balance(), True) print('Success: test_check_balance')
def testHeightV2(self): node_2 = Node(2, None, None) node_7 = Node(7, None, None) node_4 = Node(4, node_2, node_7) node_6 = Node(6, None, None) node_5 = Node(5, node_4, node_6) bst = BST(node_5) self.assertEquals(2, bst.get_height(node_5))
def test_get(): root = Node('b', 5) root.left = Node('a', 3) root.right = Node('c', 2) bst = BST(root) assert bst.get('a') == 3 assert bst.get('c') == 2 assert bst.get('d') is None
def testRemoveLeafNode2(self): node_1 = Node(1, None, None) node_2 = Node(2, node_1, None) node_3 = Node(4, node_2, None) node_4 = Node(6, None, None) node_5 = Node(5, node_3, node_4) bst = BST(node_5) bst.removeNode(None, node_5, 1) self.assertEquals(node_2.left, None)
def testRemoveNodeHasOneChildren(self): node_1 = Node(1, None, None) node_2 = Node(2, node_1, None) node_3 = Node(4, node_2, None) node_4 = Node(6, None, None) node_5 = Node(5, node_3, node_4) bst = BST(node_5) bst.removeNode(None, node_5, 2) self.assertEquals(node_3.left.data, 1)
def BSTRandomTest(tree, size): """ Fills a BST of a given size, with random elements. Tests the contents of the BST with an array, after insertions and deletions. Returns Tree if the contents of the BST are identical to the array at all times. For BST class and subclass testing. """ passed = True ar = [] # insertions for i in range(size): v = random.randint(-999, 999) if v not in ar: ar.append(v) tree.insert(Node(v)) ar.sort() if not BSTCompare(tree, ar): BSTPrintErrorMessage(tree, ar, ", after insertions") passed = False # known deletions for i in range(len(ar) // 2): v = ar[i] ar.remove(v) n = tree.search(v) tree.delete(n) if not BSTCompare(tree, ar): BSTPrintErrorMessage(tree, ar, ", after known deletions") passed = False # random deletions for i in range(size // 2): v = random.randint(-1000, 1000) if v in ar: ar.remove(v) n = tree.search(v) tree.delete(n) if not BSTCompare(tree, ar): BSTPrintErrorMessage(tree, ar, ", after random deletions") passed = False # additional insertions for i in range(size // 4): v = random.randint(-1000, 1000) if v not in ar: ar.append(v) tree.insert(Node(v)) ar.sort() if not BSTCompare(tree, ar): BSTPrintErrorMessage(tree, ar, ", after second insertions") passed = False return passed
def main(): root = Node(1) insert(root, Node(0)) insert(root, Node(2)) for i in range(0, 9): insert(root, Node(random.randint(0, i))) levelOrder(root) print 'asdf' list_level(root, 3) pass
def baltree(root, list): if not list: return mid = len(list) / 2 root.data = list[mid] root.left = Node() root.right = Node() baltree(root.left, list[0: mid]) baltree(root.right, list[mid + 1:])
def main(): root1 = Node(0) root2 = Node(10) insert(root2, Node(5)) insert(root2, Node(7)) arr = [i for i in range(0, 9)] createBstFromSorted(root1, arr, 0, len(arr) - 1) levelOrder(root1) print 'root2' levelOrder(root2) print subtree(root1, root2) pass
def build_from_PostIn(postorder, inorder): if inorder and postorder: index = inorder.index(postorder.pop()) root = Node(inorder[index]) root.right = build_from_PostIn(postorder, inorder[index + 1:]) root.left = build_from_PostIn(postorder, inorder[:index]) return root
def testInsertThreeNodes(self): node_1 = Node(5, None, None) bst = BST(node_1) bst.insert(node_1, 4) bst.insert(node_1, 2) bst.insert(node_1, 1) self.assertEquals(node_1.left.left.left.data, 1)
def test_tree_level_lists(self): bst = BstLevelLists(Node(5)) bst.insert(3) bst.insert(8) bst.insert(2) bst.insert(4) bst.insert(1) bst.insert(7) bst.insert(6) bst.insert(9) bst.insert(10) bst.insert(11) levels = bst.create_level_lists() results_list = [] for level in levels: results = Results() for node in level: results.add_result(node) results_list.append(results) assert_equal(str(results_list[0]), '[5]') assert_equal(str(results_list[1]), '[3, 8]') assert_equal(str(results_list[2]), '[2, 4, 7, 9]') assert_equal(str(results_list[3]), '[1, 6, 10]') assert_equal(str(results_list[4]), '[11]') print('Success: test_tree_level_lists')
def main(): root = Node(6) left = Node(4) right = Node(5) l1 = Node(3) l2 = Node(5) insert(left, l1) insert(left, l2) insert(root, left) insert(root, right) levelOrder(root) print ancestor(root, l1, l2, [False], [False], [False]) print 'root is ' + str(left) pass
def main(): root = Node() arr = [i for i in range(0, 7)] createBstFromSorted(root, arr, 0, len(arr) - 1) levelOrder(root) print arr
def _create_min_bst(self, array, start, end): if end < start: return None mid = (start + end) // 2 node = Node(array[mid]) node.left = self._create_min_bst(array, start, mid - 1) node.right = self._create_min_bst(array, mid + 1, end) return node
def build_from_PreIn(preorder, inorder): if inorder and preorder: index = inorder.index(preorder[0]) root = Node(inorder[index]) root.left = build_from_PreIn(preorder[1:index + 1], inorder[:index]) root.right = build_from_PreIn(preorder[index + 1:], inorder[index + 1:]) return root
def createBstFromSorted(root, arr, start, end): if not root: return if start > end: root = None return mid = (start + end) / 2 root.data = arr[mid] if not start > (mid - 1): root.left = Node() createBstFromSorted(root.left, arr, start, mid - 1) if not (mid + 1) > end: root.right = Node() createBstFromSorted(root.right, arr, mid + 1, end)
def testInsertFourNodes(self): node_1 = Node(5, None, None) bst = BST(node_1) bst.insert(node_1, 4) bst.insert(node_1, 2) bst.insert(node_1, 1) bst.insert(node_1, 6) self.assertEquals(node_1.right.data, 6)
def test_bst_validate(self): bst = BstValidate(Node(5)) bst.insert(8) bst.insert(5) bst.insert(6) bst.insert(4) bst.insert(7) assert_equal(bst.validate(), True) bst = BstValidate(Node(5)) left = Node(5) right = Node(8) invalid = Node(20) bst.root.left = left bst.root.right = right bst.root.left.right = invalid assert_equal(bst.validate(), False) print('Success: test_bst_validate')
def BSTFromArray(arr): """ Creates a BST from the keys in a given array. type arr: List[] rtype: BST """ tree = BST() for k in arr: tree.insert(Node(k)) return tree
def test_bfs(self): bst = BstBfs(Node(5)) bst.insert(2) bst.insert(8) bst.insert(1) bst.insert(3) bst.bfs(self.results.add_result) assert_equal(str(self.results), '[5, 2, 8, 1, 3]') print('Success: test_bfs')
def test_height(self): bst = BstHeight(Node(5)) assert_equal(bst.height(bst.root), 1) bst.insert(2) bst.insert(8) bst.insert(1) bst.insert(3) assert_equal(bst.height(bst.root), 3) print('Success: test_height')
def test_bst_second_largest(self): bst = Solution(None) assert_raises(TypeError, bst.find_second_largest) root = Node(10) bst = Solution(root) node5 = bst.insert(5) node15 = bst.insert(15) node3 = bst.insert(3) node8 = bst.insert(8) node12 = bst.insert(12) node20 = bst.insert(20) node2 = bst.insert(2) node4 = bst.insert(4) node30 = bst.insert(30) assert_equal(bst.find_second_largest(), node20) root = Node(10) bst = Solution(root) node5 = bst.insert(5) node3 = bst.insert(3) node7 = bst.insert(7) assert_equal(bst.find_second_largest(), node7) print('Success: test_bst_second_largest')
def testFoundSearch(self): node_5 = Node(5, None, None) bst = BST(node_5) bst.insert(node_5, 4) bst.insert(node_5, 2) bst.insert(node_5, 1) bst.insert(node_5, 3) bst.insert(node_5, 6) bst.insert(node_5, 7) bst.insert(node_5, 8) bst.insert(node_5, 0) bst.printAllNodes() self.assertTrue(bst.search(node_5, 7))
def testRemoveNodeHasTwoChildrenRightSubtree(self): node_5 = Node(5, None, None) bst = BST(node_5) bst.insert(node_5, 4) bst.insert(node_5, 2) bst.insert(node_5, 1) bst.insert(node_5, 3) bst.insert(node_5, 6) bst.insert(node_5, 7) bst.insert(node_5, 8) bst.insert(node_5, 0) bst.removeNode(None, node_5, 7) self.assertFalse(bst.search(node_5, 7))
def print_Trees(first, last): trees = [] for i in range(first, last + 1): for j in print_Trees(first, i - 1): for y in print_Trees(i + 1, last): # print("hi") BT = Binary_Tree() root = Node(i) root.left = j root.right = y BT.root = root trees.append(root) return trees or [None]
def testNotFoundSearch(self): node_5 = Node(5, None, None) bst = BST(node_5) bst.insert(node_5, 4) bst.insert(node_5, 2) bst.insert(node_5, 1) bst.insert(node_5, 3) bst.insert(node_5, 6) bst.insert(node_5, 7) bst.insert(node_5, 8) bst.insert(node_5, 0) bst = BST(node_5) self.assertFalse(bst.search(node_5, 9))
def AVLFullTest(): for i in range(30): tree = AVL() passed = BSTRandomTest(tree, 1000) if passed: height = tree.height() arr = tree.inorderWalk() max_height = round(1.44 * math.log(len(arr), 2) - 1, 1) if height > max_height: s = "but height exceeded expectations: " else: s = "and height within expectations: " s += "actual " + str(height) + " vs. expected " + str(max_height) print "Passed test", i + 1, s tree = AVL() ar = [] for i in range(16): v = random.randint(-99, 99) if v not in ar: tree.insert(Node(v)) ar.append(v) ar.sort() print "Height =", tree.height() print "Max. expected height =", round(1.44 * math.log(len(ar) + 1, 2) - 1, 1) print "Minimum =", tree.minimum() print "Maximum =", tree.maximum() print "In-order Walk =", tree.inorderWalk() print "Array =", ar print "Tree =" print tree for i in range(3): index = random.randint(0, len(ar) - 1) v = ar[index] tree.delete(tree.search(v)) ar.remove(v) print "Height =", tree.height() print "Max. expected height =", round(1.44 * math.log(len(ar) + 1, 2) - 1, 1) print "Minimum =", tree.minimum() print "Maximum =", tree.maximum() print "In-order Walk =", tree.inorderWalk() print "Array =", ar print "Tree =" print tree
def test_dfs(self): bst = BstDfs(Node(5)) bst.insert(2) bst.insert(8) bst.insert(1) bst.insert(3) bst.in_order_traversal(bst.root, self.results.add_result) assert_equal(str(self.results), "[1, 2, 3, 5, 8]") self.results.clear_results() bst.pre_order_traversal(bst.root, self.results.add_result) assert_equal(str(self.results), "[5, 2, 1, 3, 8]") self.results.clear_results() bst.post_order_traversal(bst.root, self.results.add_result) assert_equal(str(self.results), "[1, 3, 2, 8, 5]") self.results.clear_results() bst = BstDfs(Node(1)) bst.insert(2) bst.insert(3) bst.insert(4) bst.insert(5) bst.in_order_traversal(bst.root, self.results.add_result) assert_equal(str(self.results), "[1, 2, 3, 4, 5]") self.results.clear_results() bst.pre_order_traversal(bst.root, self.results.add_result) assert_equal(str(self.results), "[1, 2, 3, 4, 5]") self.results.clear_results() bst.post_order_traversal(bst.root, self.results.add_result) assert_equal(str(self.results), "[5, 4, 3, 2, 1]") print('Success: test_dfs')
def handle_BST(self): print "How many nodes would you like to include in the binary search tree?" num_nodes = raw_input(">") print "Please enter the root node" root_node_data = raw_input(">") root_node = Node(data=root_node_data, left=None, right=None, is_root=True) bst = BST(root_node) for i in range(int(num_nodes) - 1): print "Please enter node val" val = raw_input(">") bst.insert(bst.root, val) print "Your final binary search tree is, in the form [Current, Left Node, Right Node]\n", bst.printAllNodes( )