From 5f79cf3d17c272b32b613169e340aef43e0beca0 Mon Sep 17 00:00:00 2001 From: Adriana Gutierrez Date: Tue, 27 Dec 2022 17:36:45 -0500 Subject: [PATCH 1/2] three tests passing --- binary_search_trees/array_to_bst.py | 60 ++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/binary_search_trees/array_to_bst.py b/binary_search_trees/array_to_bst.py index f69cc42..6a0e60e 100644 --- a/binary_search_trees/array_to_bst.py +++ b/binary_search_trees/array_to_bst.py @@ -10,4 +10,62 @@ 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 + def returnOddVals(arr): + mid_index = len(arr)//2 + return arr[mid_index], arr[:mid_index], arr[mid_index+1:] + + def returnEvenVals(arr): + mid_index = len(arr)//2-1 + return arr[mid_index], arr[:mid_index], arr[mid_index+1:] + + def recursive_helper(prev, left_side, right_side): + #left side + if not left_side: + new_node_val = None + elif len(left_side) == 1: + new_node_val = left_side[0] + new_left_node = TreeNode(new_node_val) + prev.left = new_left_node + else: + if len(left_side)%2 == 0: + new_node_val, new_left_for_left_side, new_right_for_left_side = returnEvenVals(left_side) + else: + new_node_val, new_left_for_left_side, new_right_for_left_side = returnOddVals(left_side) + if new_node_val: + new_left_node = TreeNode(new_node_val) + else: + new_left_node = None + prev.left = new_left_node + recursive_helper(new_left_node, new_left_for_left_side, new_right_for_left_side) + + #right side + if not right_side: + new_node_val = None + elif len(right_side) == 1: + new_node_val = right_side[0] + new_right_node = TreeNode(new_node_val) + prev.right = new_right_node + else: + if len(right_side)%2 == 0: + new_node_val, new_left_for_right_side, new_right_for_right_side = returnEvenVals(right_side) + else: + new_node_val, new_left_for_right_side, new_right_for_right_side = returnOddVals(right_side) + if new_node_val: + new_right_node = TreeNode(new_node_val) + else: + new_right_node = None + prev.right = new_right_node + recursive_helper(new_right_node, new_left_for_right_side, new_right_for_right_side) + + if not arr: + return None + if len(arr) == 1: + root = TreeNode(arr[0]) + else: + if len(arr)%2 == 0: + root_val, left_side, right_side = returnEvenVals(arr) + else: + root_val, left_side, right_side = returnOddVals(arr) + root = TreeNode(root_val) + recursive_helper(root, left_side, right_side) + return root \ No newline at end of file From e1a2e2136981d9f3934cf7912d18b4800be97872 Mon Sep 17 00:00:00 2001 From: Adriana Gutierrez Date: Tue, 27 Dec 2022 18:19:14 -0500 Subject: [PATCH 2/2] all tests passing --- binary_search_trees/array_to_bst.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/binary_search_trees/array_to_bst.py b/binary_search_trees/array_to_bst.py index 6a0e60e..367e341 100644 --- a/binary_search_trees/array_to_bst.py +++ b/binary_search_trees/array_to_bst.py @@ -19,6 +19,8 @@ def returnEvenVals(arr): return arr[mid_index], arr[:mid_index], arr[mid_index+1:] def recursive_helper(prev, left_side, right_side): + if not prev: + return None #left side if not left_side: new_node_val = None @@ -31,7 +33,7 @@ def recursive_helper(prev, left_side, right_side): new_node_val, new_left_for_left_side, new_right_for_left_side = returnEvenVals(left_side) else: new_node_val, new_left_for_left_side, new_right_for_left_side = returnOddVals(left_side) - if new_node_val: + if new_node_val != None: new_left_node = TreeNode(new_node_val) else: new_left_node = None @@ -50,7 +52,7 @@ def recursive_helper(prev, left_side, right_side): new_node_val, new_left_for_right_side, new_right_for_right_side = returnEvenVals(right_side) else: new_node_val, new_left_for_right_side, new_right_for_right_side = returnOddVals(right_side) - if new_node_val: + if new_node_val != None: new_right_node = TreeNode(new_node_val) else: new_right_node = None