def get_the_distance_of_the_closest_entries_in_k_sorted_arrays(arrays): """ The time complexity is O(n * h) where n is the total numbers of elements in arrays. h is the height of the bst we use in line 22. If the bst library you use is a self-balanced bst, then h = logk where k is the number of arrays in arrays. """ heads = [0] * len(arrays) root = None for i in xrange(len(arrays)): array = arrays[i] head = heads[i] if root: """ We use a tuple = (array_val, array_source_id) as tree node data and tulple would compare the 1st value first and then the 2nd value, so even array_val is the same, we still have array_source_id to make them distinct. """ bst.Bst.insert(root, bst.BinaryTreeNode((array[head], i))) else: root = bst.BinaryTreeNode((array[head], i)) res = float('inf') while True: res = min(res, bst.Bst.last(root).data[0] - bst.Bst.first(root).data[0]) first_node, root = bst.Bst.pop_first(root) (val, array_id) = first_node.data heads[array_id] += 1 if heads[array_id] >= len(arrays[array_id]): return res data = (arrays[array_id][heads[array_id]], array_id) bst.Bst.insert(root, bst.BinaryTreeNode(data))
def insert(self, idx, credit): self.remove(idx) data = Item([credit, idx]) if self.root: self.table[idx] = credit bst.Bst.insert(self.root, bst.BinaryTreeNode(Item([credit, idx])), append_when_same_key=True) self.root = bst.BinaryTreeNode(Item([credit, set([idx])])) return True
def recursive(array, start, end): if start >= end: return None m = start + (end - start) / 2 return bst.BinaryTreeNode( array[m], recursive(array, start, m), recursive(array, m + 1, end), )
def enum_a_plus_b_sqrt2(k): """ The time complexity is O(k * h) where h is the bst height of root. (If the bse library you use is a self-balanced tree, then h = logk) The additional space complexity is O(k) """ root = bst.BinaryTreeNode(Item(0, 0)) res = [] while len(res) < k: next_smallest = bst.Bst.first(root).data res.append(next_smallest) bst.Bst.insert( root, bst.BinaryTreeNode(Item(next_smallest.a + 1, next_smallest.b))) bst.Bst.insert( root, bst.BinaryTreeNode(Item(next_smallest.a, next_smallest.b + 1))) tmp, root = bst.Bst.pop_first(root) return res
def rebuild_from_range(array, lower, upper, root_idx): if ( (root_idx >= len(array)) or (array[root_idx] < lower) or (array[root_idx] > upper) ): return None, root_idx root = bst.BinaryTreeNode(array[root_idx]) root_idx += 1 left_tree, root_idx = rebuild_from_range(array, lower, root.data, root_idx) right_tree, root_idx = rebuild_from_range(array, root.data, upper, root_idx) root.left = left_tree root.right = right_tree return root, root_idx
def helper(array, start, end): if start == end: return None left_start = start+1 left_end = start+1 # left_end = left_start + length for i in xrange(start+1, end): if array[i] < array[start]: left_end += 1 else: break return bst.BinaryTreeNode( array[start], helper(array, left_start, left_end), helper(array, left_end, end) )
from __future__ import print_function import bst def get_input(case=0): root = bst.BinaryTreeNode(3) for i in xrange(7): if i != 3: bst.Bst.insert(root, bst.BinaryTreeNode(i)) key = 3 ans = root if case == 0: pass elif case == 1: key = 1 ans = root.left.right elif case == 2: key = 5 ans = root.right.right elif case == 3: key = 6 ans = root.right.right.right return root, key, ans def main(): root, key, ans = get_input() print('root =\n', root) for case in xrange(4): print('--- case {} ---'.format(case)) root, key, ans = get_input(case)