From 4d5f07db7fd07265bd41a5d628824f5ff2ab14e0 Mon Sep 17 00:00:00 2001 From: Theresa Davis Date: Sat, 14 Jan 2023 15:32:25 -0800 Subject: [PATCH 1/2] first commit --- binary_search_trees/array_to_bst.py | 68 ++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/binary_search_trees/array_to_bst.py b/binary_search_trees/array_to_bst.py index f69cc42..cdd63cf 100644 --- a/binary_search_trees/array_to_bst.py +++ b/binary_search_trees/array_to_bst.py @@ -10,4 +10,70 @@ 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) + + + + def printFunc(self, current_node): + if self.root !=None: + self.printTree(self.root) + + def printTree( self, current_node): + if current_node !=None: + self.printTree(current_node.left) + print ( str(current_node.value)) + self.printTree(current_node.rightt) + + + + + + + + + # 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 From f6da5187758dab0ab2bb699062d238bc4763ba2e Mon Sep 17 00:00:00 2001 From: Theresa Davis Date: Sat, 14 Jan 2023 15:43:15 -0800 Subject: [PATCH 2/2] removed print statement --- binary_search_trees/array_to_bst.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/binary_search_trees/array_to_bst.py b/binary_search_trees/array_to_bst.py index cdd63cf..479dc53 100644 --- a/binary_search_trees/array_to_bst.py +++ b/binary_search_trees/array_to_bst.py @@ -43,15 +43,7 @@ def insertNode(self, value, current_node): - def printFunc(self, current_node): - if self.root !=None: - self.printTree(self.root) - - def printTree( self, current_node): - if current_node !=None: - self.printTree(current_node.left) - print ( str(current_node.value)) - self.printTree(current_node.rightt) +