def quicksort_helper(lyst, left, right): """ Helper method for quicksort(). """ RecursionCounter() if left < right: pivot_location = partition(lyst, left, right) quicksort_helper(lyst, left, pivot_location - 1) quicksort_helper(lyst, pivot_location + 1, right)
def _inorder_helper(self, cursor): """Handles recursion for the inorder() method.""" RecursionCounter() if cursor: self._inorder_helper(cursor.left_child) self.inordered_list.append(cursor.data) self._inorder_helper(cursor.right_child)
def recursive_binary_search_helper(low_index, high_index, lyst, target): """ Recursive function to search provided list. Process: If middle of list equals target value the middle value is returned. If the target is less than the middle value the method is recursively called with the range between the low and middle. If the target is greater than the middle, the method is recursively called with the range between the middle+1 and high values. Args: low_index(int): Lowest position in list search range high_index(int): Highest position in list search range lyst(list): The sorted list to be searched target(int): The value to be found Returns: bool: Returns True if the target is present and False if not found. """ RecursionCounter() mid = (low_index + high_index) // 2 if target > lyst[high_index]: return False if len(lyst) <= 0: return False if target == lyst[mid]: return True elif target < lyst[mid]: return recursive_binary_search_helper(low_index, mid, lyst, target) elif target > lyst[mid]: return recursive_binary_search_helper((mid + 1), high_index, lyst, target)
def remove_helper( precursor, cursor, data ): # recursive helper for remove function, for finding nodes past root to remove. RecursionCounter() if (cursor.data == data): if (cursor.right_child == None and cursor.left_child == None): if (precursor.right_child == cursor): precursor.right_child = cursor.right_child else: precursor.left_child = cursor.left_child elif (cursor.right_child == None): cursor.data = cursor.left_child.data cursor.right_child = cursor.left_child.right_child cursor.left_child = None elif (cursor.right_child.left_child == None): cursor.data = cursor.right_child.data cursor.right_child = None else: cursor.data = cursor.right_child.left_child.data cursor.right_child.left_child = None else: if (cursor.right_child == None and cursor.left_child == None): return False elif (cursor.data < data): BinarySearchTree.remove_helper(cursor, cursor.right_child, data) else: BinarySearchTree.remove_helper(cursor, cursor.left_child, data)
def add_helper(node, data): """ Recursively assists with adding nodes in their proper order. Process: Recursively calls add_helper method, locating position for new node, then inserting. Updates node height as it inserts. Args: node(Node): Subsequent nodes called for add data(int): The data to be added to an inserted node Returns: None(None): No return """ RecursionCounter() if node.data > data: if node.left_child is None: node.left_child = Node(data) else: add_helper(node.left_child, data) if node.height <= node.left_child.height: node.update_height() elif node.data < data: if node.right_child is None: node.right_child = Node(data) else: add_helper(node.right_child, data) if node.height <= node.right_child.height: node.update_height()
def add_helper( cursor, data ): # recursive helper for the add function. Finds location where a new node should be inserted. RecursionCounter() if (cursor.data == data): print("Duplicate Data Provided") return 0 if (data < cursor.data): if (cursor.left_child == None): cursor.left_child = Node(data) if (cursor.right_child == None): return 1 return 0 elif (data > cursor.left_child.data): newNode = Node(data) newNode.height = cursor.height - 1 cursor.left_child.right_child = newNode return 0 else: return BinarySearchTree.add_helper(cursor.left_child, data) else: if (cursor.right_child == None): cursor.right_child = Node(data) if (cursor.left_child == None): return 1 return 0 elif (data < cursor.right_child.data): newNode = Node(data) cursor.right_child.left_child = newNode return 0 else: return BinarySearchTree.add_helper(cursor.right_child, data)
def preorder_recursive( node): # recursive helper for the preorder traversal. RecursionCounter() if (node != None): print("{}, ".format(node.data), end="") BinarySearchTree.preorder_recursive(node.left_child) BinarySearchTree.preorder_recursive(node.right_child)
def preorder_helper(node): """Helper recursion method""" RecursionCounter() if node is not None: lyst.append(node.data) preorder_helper(node.left) preorder_helper(node.right)
def len_helper(node): """Recursive helper function for len""" RecursionCounter() if node is None: return 0 else: return len_helper(node.left) + 1 + len_helper(node.right)
def remove_helper(self, cursor, data): RecursionCounter() if cursor == None: return None elif cursor.data == data: if cursor.is_leaf(): return None if cursor.right_child is None and cursor.left_child is not None: cursor.height -= cursor.update_height() return cursor.left_child if cursor.left_child is None and cursor.right_child is not None: cursor.height -= cursor.update_height() return cursor.right_child if cursor.left_child is not None and cursor.right_child is not None: temp = cursor.right_child while temp.left_child is not None: temp = temp.left_child cursor.data = temp.data cursor.right_child.height -= cursor.right_child.update_height() cursor.right_child = self.remove_helper( cursor.right_child, temp.data) elif data > cursor.data: cursor.right_child = self.remove_helper(cursor.right_child, data) elif data < cursor.data: cursor.left_child = self.remove_helper(cursor.left_child, data) cursor.height = cursor.update_height() return cursor
def size_helper(self, node): """Handles recursion for size()""" _ = RecursionCounter() if not node: return 0 else: return 1 + self.size_helper(node.next)
def length_helper(cursor): RecursionCounter() if cursor is None: return 0 while not cursor.is_leaf(): return 1 + length_helper(cursor.right_child) + length_helper(cursor.left_child) return 1
def print_helper(cursor, offset): RecursionCounter() if cursor is None: return f'{offset}[Empty]' if cursor.is_leaf(): return f'{offset}{cursor}' return f"{offset}{cursor}\n{print_helper(cursor.left_child, offset+' ')}\n{print_helper(cursor.right_child, offset+' ')}"
def preorder_helper(cursor, lyst): RecursionCounter() if cursor is None: return lyst.append(cursor.data) preorder_helper(cursor.left_child, lyst) preorder_helper(cursor.right_child, lyst)
def quicksort_helper(self, lyst, low, high): """Recursive helper for the quicksort algorithm""" RecursionCounter() if low < high: pivot_point = self.quicksort_pivot(lyst, low, high) self.quicksort_helper(lyst, low, pivot_point - 1) self.quicksort_helper(lyst, pivot_point + 1, high)
def inorder(self, node: Node): _ = RecursionCounter() if node: self.inorder(node.left) print(node.key, end=" ") self.inorder(node.right)
def mergesort(array): '''merge sort algorithm''' if isinstance(array, list) == False: raise ValueError("Not a list!") RecursionCounter() if len(array) > 1: mid = len(array) // 2 left = array[:mid] right = array[mid:] mergesort(left) mergesort(right) i = j = k = 0 while i < len(left) and j < len(right): if left[i] < right[j]: array[k] = left[i] i += 1 else: array[k] = right[j] j += 1 k += 1 while i < len(left): array[k] = left[i] i += 1 k += 1 while j < len(right): array[k] = right[j] j += 1 k += 1 return array
def recurse(node, count): """ Recursive helper to determine CourseList size. """ _ = RecursionCounter() if node is not None: count += 1 return recurse(node.next, count) return count
def set_heights( node): # retrieves accurate heights for every node in the tree. RecursionCounter() if (node != None): BinarySearchTree.set_heights(node.left_child) node.height = BinarySearchTree.retrieve_height(node) BinarySearchTree.set_heights(node.right_child)
def recurse(node, grades, credit_hrs): """ Recursive helper to build cumulative gpa value. """ _ = RecursionCounter() if node is None: return grades / credit_hrs return recurse(node.next, grades + (node.grade() * node.credit_hr()),\ credit_hrs + node.credit_hr())
def print_helper(cursor, offset): # used to show a representation of the tree. RecursionCounter() offset += 1 if (cursor != None): cursor.height = BinarySearchTree.retrieve_height(cursor) for x in range(offset): print(" ", end="") print(cursor) if ((cursor != None) and (cursor.right_child != None and cursor.left_child == None)): for x in range(offset + 1): print(" ", end="") print("[Empty]") BinarySearchTree.print_helper(cursor.left_child, offset) if ((cursor != None) and (cursor.right_child == None and cursor.left_child != None)): for x in range(offset + 1): print(" ", end="") print("[Empty]") BinarySearchTree.print_helper(cursor.right_child, offset)
def recursive_binary_search_helper(lyst, target, low_index=0, high_index=None): """Function to recursively search through a lyst by comparing its value with the midpoint""" RecursionCounter() if high_index is None: high_index = len(lyst) - 1 midpoint = (low_index + high_index) // 2 if low_index > high_index: return False elif midpoint + 1 == high_index: return False elif midpoint - 1 == low_index: if lyst[low_index] == target: return True elif lyst[high_index] == target: return True else: return False elif lyst[midpoint] == target: return True elif lyst[midpoint] < target: return recursive_binary_search_helper(lyst, target, midpoint - 1, high_index) elif lyst[midpoint] > target: return recursive_binary_search_helper(lyst, target, low_index, midpoint + 1) else: return False
def __remove_helper(self, cursor, item): '''recursion helper for self.remove''' RecursionCounter() if cursor is None: return None elif item < cursor.data: cursor.left_child = self.__remove_helper(cursor.left_child, item) elif item > cursor.data: cursor.right_child = self.__remove_helper(cursor.right_child, item) else: if cursor.is_leaf(): cursor = None elif cursor.left_child is not None and cursor.right_child is not None: cursor.data = self.__min_value(cursor.right_child) cursor.right_child = self.__remove_helper( cursor.right_child, cursor.data) elif cursor.left_child is None and cursor.right_child is not None: cursor = cursor.right_child elif cursor.right_child is None and cursor.left_child is not None: cursor = cursor.left_child return cursor
def remove_helper(self, cursor, data): """ Recursively does some stuff Implementing after inOrder. """ _ = RecursionCounter() if cursor is None: return cursor if data < cursor.data: cursor.left_child = self.remove_helper(cursor.left_child, data) cursor.update_height() elif data > cursor.data: cursor.right_child = self.remove_helper(cursor.right_child, data) cursor.update_height() else: if cursor.left_child is None: temp_node = cursor.right_child cursor = temp_node return temp_node elif cursor.right_child is None: temp_node = cursor.left_child cursor = temp_node return temp_node temp = self.smallest_child(cursor.right_child) cursor.data = temp.data cursor.right_child = self.remove_helper(cursor.right_child, temp.data) cursor.update_height() return cursor
def _remove_helper(self, cursor, item): """Handles recursion for the remove() method.""" RecursionCounter() if not cursor: return cursor # finds the node if cursor.data > item: # traverse down the left of the tree if cursor.left_child is not None: cursor.left_child = self._remove_helper( cursor.left_child, item) elif cursor.data < item: # traverse down the right of the tree if cursor.right_child is not None: cursor.right_child = self._remove_helper( cursor.right_child, item) else: # found the node --> check if it is a leaf if cursor.is_leaf(): cursor = None return cursor elif cursor.left_child is not None and cursor.right_child is not None: cursor.data = self._min_value(cursor.right_child) cursor.right_child = self._remove_helper( cursor.right_child, cursor.data) elif cursor.right_child is not None: cursor = cursor.right_child return cursor elif cursor.left_child is not None: cursor = cursor.left_child return cursor return cursor
def print_helper(self, cursor, offset): """ Recursive print helper for tree. Iterates retursively through tree and prints with spacing per offset. """ _ = RecursionCounter() if cursor is None: return '' output = '' offset_counter = offset - self.root.height for i in range(offset_counter): output += ' ' output += str(cursor) print(output) if cursor.left_child is None: if cursor.height > 0: output = '' offset_counter += 1 for i in range(offset_counter): output += ' ' output += '[Empty]' print(output) elif self.left_is_leaf is True: output = '' for i in range(offset_counter): output += ' ' output += '[Empty]' print(output) self.left_is_leaf = False if cursor.left_child is not None: if cursor.left_child.is_leaf() and cursor.right_child is None: self.left_is_leaf = True self.print_helper(cursor.left_child, offset + 1) self.print_helper(cursor.right_child, offset + 1)
def __str__helper(self, cursor, level): """Handles recursion for the __str__() method.""" RecursionCounter() if cursor: if cursor.left_child is not None: if cursor.left_child.is_leaf(): self.output += (str(cursor.left_child) + ' (' + str(level - 1) + ') [leaf]\n') self.output += '[Empty]\n' self.__str__helper(cursor.left_child, level - 1) else: self.output += (str(cursor.left_child) + ' (' + str(level - 1) + ')\n') self.__str__helper(cursor.left_child, level - 1) elif cursor.left_child is None: if not cursor.is_leaf(): self.output += '[Empty]\n' self.__str__helper(cursor.left_child, level - 1) if cursor.right_child is not None: if cursor.right_child.is_leaf(): self.output += (str(cursor.right_child) + ' (' + str(level - 1) + ') [leaf]\n') self.__str__helper(cursor.right_child, level - 1) else: self.output += (str(cursor.right_child) + ' (' + str(level - 1) + ')\n') self.__str__helper(cursor.right_child, level - 1) elif cursor.right_child is None: self.__str__helper(cursor.right_child, level - 1)
def __len__(self): """ Returns length of BST (How many Nodes) """ _ = RecursionCounter() out = self.preorder() return len(out)
def quicksort_helper(lyst, lower, higher): # recursive helper for the quick sort. RecursionCounter() # counts the number of recursions. if (lower < higher): # compares the endpoints provided, checks if they are in order. pivot = new_list(lyst, lower, higher) quicksort_helper(lyst, lower, pivot) # recursively calls self using lower endpoint and pivot index. quicksort_helper(lyst, pivot + 1, higher) # recursively calls self using pivot index + 1 and higher endpoint. return lyst # returns a sorted list if in order.
def mergesort_helper(lyst): """The recursive part of mergesort.""" RecursionCounter() if len(lyst) > 1: mid = len(lyst) // 2 left = lyst[:mid] right = lyst[mid:] mergesort_helper(left) mergesort_helper(right) i = 0 j = 0 k = 0 while i < len(left) and j < len(right): if left[i] < right[j]: lyst[k] = left[i] i += 1 else: lyst[k] = right[j] j += 1 k += 1 while i < len(left): lyst[k] = left[i] i += 1 k += 1 while j < len(right): lyst[k] = right[j] j += 1 k += 1 return lyst