From 50979df0e54c0fac4b6e81f9f87e83b1cb9d0bfd Mon Sep 17 00:00:00 2001 From: char Date: Fri, 30 Sep 2022 11:02:23 -0700 Subject: [PATCH 1/2] update tests --- tests/test_array_to_bst.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/tests/test_array_to_bst.py b/tests/test_array_to_bst.py index 8246fe8..9ab6ff3 100644 --- a/tests/test_array_to_bst.py +++ b/tests/test_array_to_bst.py @@ -10,7 +10,7 @@ def test_will_return_balanced_bst_for_odd_lengthed_list(): answer = arr_to_bst(arr) # Assert - assert answer.val is 25 and is_bst(answer) and is_balanced_tree(answer) + assert answer.val is 25 and is_bst(answer) and is_balanced_tree(answer) and inorder(answer) == arr def test_will_return_balanced_bst_for_even_lengthed_list(): # Arrange @@ -20,7 +20,7 @@ def test_will_return_balanced_bst_for_even_lengthed_list(): answer = arr_to_bst(arr) # Assert - assert is_bst(answer) and is_balanced_tree(answer) + assert is_bst(answer) and is_balanced_tree(answer) and inorder(answer) == arr def test_will_return_balanced_bst_for_long_list(): # Arrange @@ -34,7 +34,7 @@ def test_will_return_balanced_bst_for_long_list(): answer = arr_to_bst(arr) # Assert - assert is_bst(answer) and is_balanced_tree(answer) + assert is_bst(answer) and is_balanced_tree(answer) and inorder(answer) == arr def test_will_return_none_for_empty_list(): # Arrange @@ -93,3 +93,17 @@ def is_balanced_tree(root): right_check = is_balanced_tree(root.right) return left_check and right_check + +def inorder_helper(node, values): + if not node: + return values + + inorder_helper(node.left, values) + values.append(node.val) + inorder_helper(node.right, values) + + return values + +def inorder(node): + values = [] + return inorder_helper(node, values) From 7423c343d9b13291685aa3b21a7a3047cad7d7b5 Mon Sep 17 00:00:00 2001 From: Andrea Garcia Date: Tue, 17 Jan 2023 00:30:38 -0800 Subject: [PATCH 2/2] all tests passed --- binary_search_trees/array_to_bst.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/binary_search_trees/array_to_bst.py b/binary_search_trees/array_to_bst.py index f69cc42..bc592d5 100644 --- a/binary_search_trees/array_to_bst.py +++ b/binary_search_trees/array_to_bst.py @@ -10,4 +10,11 @@ def arr_to_bst(arr): Balanced Binary Search Tree using the elements in the array. Return the root of the Binary Search Tree. """ - pass \ No newline at end of file + while arr: + medi = (len(arr)) // 2 + root = TreeNode(arr[medi]) + root.left = arr_to_bst(arr[:medi]) + root.right = arr_to_bst(arr[medi+1:]) + return root + else: + return None \ No newline at end of file