def recurse(data, low, high): if low > high: return else: mid = low + (high - low) // 2 node = BSTNode(data[mid]) node.left = recurse(data, low, mid - 1) node.right = recurse(data, mid + 1, high) return node
def make_bst(array): if not array: return None mid = len(array) // 2 root = BSTNode(array[mid]) if mid == 0: return root root.left = make_bst(array[:mid]) root.right = make_bst(array[mid + 1:]) return root
def get_node_recursive(leftp, rightp): if (rightp - leftp) == 1: return BSTNode(sorted_list[leftp]) if (rightp - leftp) == 0: return None middle = int((leftp + rightp) / 2) node = BSTNode(sorted_list[middle]) node.left = get_node_recursive(leftp, middle) node.right = get_node_recursive(middle + 1, rightp) return node
def recursive(nodes): middle_node = math.floor(((len(nodes) - 1) / 2)) new_root = BSTNode(nodes[middle_node]) if middle_node == 0: return new_root new_root.right = recursive(nodes[middle_node:]) new_root.left = recursive(nodes[:middle_node]) return new_root
def create_balanced_BST(arr): if len(arr) == 0: return None middle = len(arr) // 2 new_root = BSTNode(data=arr[middle]) new_root.left = create_balanced_BST(arr[:middle]) new_root.right = create_balanced_BST(arr[middle + 1:]) return new_root
def making_tree(self): node = BSTNode(self._field) field1 = copy(self._field) field2 = copy(self._field) if self.status() == NOTHING: cell = choice(Board.possible_cells(self._field)) cell2 = choice(Board.possible_cells(self._field)) field1.turn(cell) field2.turn(cell2) node.left = self.making_tree(field1) node.right = self..making_tree(field2) return node
def remove(self, item): """Precondition: item is in self. Raises: KeyError if item is not in self. postcondition: item is removed from self.""" if not item in self: raise KeyError("Item not in tree.""") # Helper function to adjust placement of an item def lift_max_in_left(top): # Replace top's datum with the maximum datum in the left subtree # Pre: top has a left child # Post: the maximum node in top's left subtree # has been removed # Post: top.data = maximum value in top's left subtree parent = top current_node = top.left while current_node.right is not None: parent = current_node current_node = current_node.right top.data = current_node.data if parent == top: top.left = current_node.left else: parent.right = current_node.left # Begin main part of the method if self.isEmpty(): return None # Attempt to locate the node containing the item item_removed = None pre_root = BSTNode(None) pre_root.left = self._root parent = pre_root direction = 'L' current_node = self._root while current_node is not None: if current_node.data == item: item_removed = current_node.data break parent = current_node if current_node.data > item: direction = 'L' current_node = current_node.left else: direction = 'R' current_node = current_node.right # Return None if the item is absent if item_removed is None: return None # The item is present, so remove its node # Case 1: The node has a left and a right child # Replace the node's value with the maximum value in the # left subtree # Delete the maximium node in the left subtree if current_node.left is not None \ and current_node.right is not None: lift_max_in_left(current_node) else: # Case 2: The node has no left child if current_node.left is None: new_child = current_node.right # Case 3: The node has no right child else: new_child = current_node.left # Case 2 & 3: Tie the parent to the new child if direction == 'L': parent.left = new_child else: parent.right = new_child # All cases: Reset the root (if it hasn't changed no harm done) # Decrement the collection's size counter # Return the item self._size -= 1 if self.isEmpty(): self._root = None else: self._root = pre_root.left return item_removed
def remove(self, item): """Precondition: item is in self. Raises: KeyError if item is not in self. postcondition: item is removed from self.""" if not item in self: raise KeyError("Item not in tree.""") # Helper function to adjust placement of an item def liftMaxInLeftSubtreeToTop(top): # Replace top's datum with the maximum datum in the left subtree # Pre: top has a left child # Post: the maximum node in top's left subtree # has been removed # Post: top.data = maximum value in top's left subtree parent = top currentNode = top.left while not currentNode.right == None: parent = currentNode currentNode = currentNode.right top.data = currentNode.data if parent == top: top.left = currentNode.left else: parent.right = currentNode.left # Begin main part of the method if self.isEmpty(): return None # Attempt to locate the node containing the item itemRemoved = None preRoot = BSTNode(None) preRoot.left = self._root parent = preRoot direction = 'L' currentNode = self._root while not currentNode == None: if currentNode.data == item: itemRemoved = currentNode.data break parent = currentNode if currentNode.data > item: direction = 'L' currentNode = currentNode.left else: direction = 'R' currentNode = currentNode.right # Return None if the item is absent if itemRemoved == None: return None # The item is present, so remove its node # Case 1: The node has a left and a right child # Replace the node's value with the maximum value in the # left subtree # Delete the maximium node in the left subtree if not currentNode.left == None \ and not currentNode.right == None: liftMaxInLeftSubtreeToTop(currentNode) else: # Case 2: The node has no left child if currentNode.left == None: newChild = currentNode.right # Case 3: The node has no right child else: newChild = currentNode.left # Case 2 & 3: Tie the parent to the new child if direction == 'L': parent.left = newChild else: parent.right = newChild # All cases: Reset the root (if it hasn't changed no harm done) # Decrement the collection's size counter # Return the item self._size -= 1 if self.isEmpty(): self._root = None else: self._root = preRoot.left return itemRemoved
def remove(self, item): """Precondition: item is in self. Raises: KeyError if item is not in self. postcondition: item is removed from self.""" if not item in self: raise KeyError("Item not in tree." "") def liftMaxInLeftSubtreeToTop(top): """ Helper function to adjust placement of an item. """ parent = top current_node = top.left while not current_node.right == None: parent = current_node current_node = current_node.right top.data = current_node.data if parent == top: top.left = current_node.left else: parent.right = current_node.left if self.isEmpty(): return None item_removed = None pre_root = BSTNode(None) pre_root.left = self._root parent = pre_root direction = 'L' current_node = self._root while not current_node == None: if current_node.data == item: item_removed = current_node.data break parent = current_node if current_node.data > item: direction = 'L' current_node = current_node.left else: direction = 'R' current_node = current_node.right if item_removed == None: return None if not current_node.left == None \ and not current_node.right == None: liftMaxInLeftSubtreeToTop(current_node) else: if current_node.left == None: new_child = current_node.right else: new_child = current_node.left if direction == 'L': parent.left = new_child else: parent.right = new_child self._size -= 1 if self.isEmpty(): self._root = None else: self._root = pre_root.left return item_removed
def remove(self, item): """Precondition: item is in self. Raises: KeyError if item is not in self. postcondition: item is removed from self.""" if not item in self: raise KeyError("Item not in tree." "") def liftMaxInLeftSubtreeToTop(top): parent = top currentNode = top.left while not currentNode.right == None: parent = currentNode currentNode = currentNode.right top.data = currentNode.data if parent == top: top.left = currentNode.left else: parent.right = currentNode.left # Begin main part of the method if self.isEmpty(): return None # Attempt to locate the node containing the item itemRemoved = None preRoot = BSTNode(None) preRoot.left = self._root parent = preRoot direction = 'L' currentNode = self._root while not currentNode == None: if currentNode.data == item: itemRemoved = currentNode.data break parent = currentNode if currentNode.data > item: direction = 'L' currentNode = currentNode.left else: direction = 'R' currentNode = currentNode.right # Return None if the item is absent if itemRemoved == None: return None if not currentNode.left == None \ and not currentNode.right == None: liftMaxInLeftSubtreeToTop(currentNode) else: if currentNode.left == None: newChild = currentNode.right else: newChild = currentNode.left if direction == 'L': parent.left = newChild else: parent.right = newChild self._size -= 1 if self.isEmpty(): self._root = None else: self._root = preRoot.left return itemRemoved