diff --git a/binary_search_trees/array_to_bst.py b/binary_search_trees/array_to_bst.py index f69cc42..479dc53 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 + + class BinarySearch_tree: + def __init__(self) : + self.root = None + + def insert(self, value): +# check for duplicates + if value == self.root.value: + return + # If root is empty + mid_point = len(arr)//2 + if self.root == None: + self.root = TreeNode(arr[mid_point]) + else: + self.insertNode(value, self.root) + +# recursive function + def insertNode(self, value, current_node): + if value < current_node.value: + if current_node.left == None: + current_node.left == TreeNode(value) +# else set left to current_node + else: + self.insertNode(value, current_node.left) +# else check the right side + else: + if current_node.right == None: + current_node.right = TreeNode(value) + else: + self.insertNode(value, current_node.right) + + + + + + + + + + + + + # arr = [5, 10, 15, 20, 25, 30, 35, 40, 45] + # Finding the midpoint +# Step one -find mid-point in sorted arrray +# assign mid-point to root +# find length and integer diviide by 2 +# if the % of length + # mid-point == len(arr)//2 + + # if len(arr)%2 == 0: + # x = len(arr) //2 + # else + # y = len(arr) + + # Duplicate values check + # compare value being passed in to value in current node. If equals the value in current node, return + + # if tree is empty assign value to root \ No newline at end of file