def test_1(self): l = BinaryTreeVertex(2) r = BinaryTreeVertex(3) root = BinaryTreeVertex(1, l, r) result = paths_with_sum(3, root) self.assertEqual(result, 2)
def test_1(self): l = BinaryTreeVertex(1) r = BinaryTreeVertex(3) root = BinaryTreeVertex(2, l, r) result = possible_arrays(root) expected = {(2, 1, 3), (2, 3, 1)} self.assertEqual(result, expected)
def lowest_bst(sorted_array): l = len(sorted_array) if l == 0: return None elif l == 1: return BinaryTreeVertex(sorted_array[0], None, None) else: mid = l/2 return BinaryTreeVertex(sorted_array[mid], lowest_bst(sorted_array[:mid]), lowest_bst(sorted_array[mid+1:]))
def test_3(self): ll = BinaryTreeVertex(4) l = BinaryTreeVertex(5, ll) rl = BinaryTreeVertex(11) r = BinaryTreeVertex(15, rl) root = BinaryTreeVertex(10, l, r) result = possible_arrays(root) expected = {(10, 5, 4, 15, 11), (10, 5, 15, 4, 11), (10, 15, 5, 4, 11), (10, 5, 15, 11, 4), (10, 15, 5, 11, 4), (10, 15, 11, 5, 4)} self.assertEqual(result, expected)
def test_1(self): l = BinaryTreeVertex('l') r = BinaryTreeVertex('r') balanced_root = BinaryTreeVertex('root', l, r) self.assertTrue(is_balanced(balanced_root)) left_leaning_root = BinaryTreeVertex('root', left=l, right=None) self.assertTrue(is_balanced(left_leaning_root)) right_leaning_root = BinaryTreeVertex('root', left=None, right=r) self.assertTrue(is_balanced(right_leaning_root))
def test_3(self): lll = BinaryTreeVertex('lll') llr = BinaryTreeVertex('llr') ll = BinaryTreeVertex('ll', lll, llr) lr = BinaryTreeVertex('lr') l = BinaryTreeVertex('l', ll, lr) rrr = BinaryTreeVertex('rrr') rr = BinaryTreeVertex('rr', right=rrr) r = BinaryTreeVertex('r', right=rr) root = BinaryTreeVertex('root', l, r) self.assertFalse(is_balanced(root))
def test_2(self): ll = BinaryTreeVertex('ll') lr = BinaryTreeVertex('lr') rl = BinaryTreeVertex('rl') rr = BinaryTreeVertex('rr') l = BinaryTreeVertex('l', ll, lr) r = BinaryTreeVertex('r', rl, rr) balanced_root = BinaryTreeVertex('root', l, r) self.assertTrue(is_balanced(balanced_root)) left_leaning_root = BinaryTreeVertex('root', left=l, right=None) self.assertFalse(is_balanced(left_leaning_root)) right_leaning_root = BinaryTreeVertex('root', left=None, right=r) self.assertFalse(is_balanced(right_leaning_root))
def test_2(self): ll = BinaryTreeVertex(3) lr = BinaryTreeVertex(4) l = BinaryTreeVertex(2, ll, lr) rl = BinaryTreeVertex(6) rr = BinaryTreeVertex(7) r = BinaryTreeVertex(5, rl, rr) root = BinaryTreeVertex(1, l, r) result = paths_with_sum(6, root) expected_result = 4 self.assertEqual(result, expected_result)
def test_1(self): ll = BinaryTreeVertex('ll') lr = BinaryTreeVertex('lr') rl = BinaryTreeVertex('rl') rr = BinaryTreeVertex('rr') l = BinaryTreeVertex('l', ll, lr) r = BinaryTreeVertex('r', rl, rr) root = BinaryTreeVertex('root', l, r) depth_list = list_of_depths(root) self.assertEqual(len(depth_list), 3) self.assertEqual(depth_list[1], [root]) self.assertEqual(depth_list[2], [l, r]) self.assertEqual(depth_list[3], [ll, lr, rl, rr])
def test_2(self): root = BinaryTreeVertex(1) result = possible_arrays(root) expected = {(1, )} self.assertEqual(result, expected)