def preorder_visit_no_recruse(node): current = node s = Stack() while current or not s.is_empty(): if current: current.echo() if current.right: s.push(current.right) if current.left: current = current.left else: current = None else: current = s.pop()
def _compute_block_id(self, block): """ For every block build a Cartesian tree using stack-based approach. During the build process encode stack pushes as *1* and stack pops as *0*. The generated 2b-bit number is the id of the block. @param block (List[int]): An array of integer numbers. @return code (int): A 2b-bit integer giving the id of the block, where b is the size of the block. """ binary_code = [0] * (2 * len(block)) idx = 0 S = Stack() for i in range(len(block)): while (not S.is_empty()) and (S.top() > block[i]): S.pop() idx += 1 S.push(block[i]) binary_code[idx] = 1 idx += 1 code = "".join(str(bit) for bit in binary_code) return int(code, 2)