def from_dtree_to_vtree(dtree_root):
    if (len(dtree_root.get_tree()[0]) == 1):
        vtree_root = VTreeNode(var_id=dtree_root.get_tree()[0][0])
        vtree_root.set_scopes([dtree_root.get_tree()[0]])
        return vtree_root
    scopes = []
    scopes_t = []
    last_node_visited = None
    dtree_stack = [dtree_root]
    buffer = []
    while dtree_stack:
        peek_dnode = dtree_stack[(- 1)]
        if ((not peek_dnode.children) or (last_node_visited in peek_dnode.children)):
            if (not peek_dnode.is_leaf()):
                leaves = []
                n_parents = 0
                for child in peek_dnode.children:
                    if child.is_leaf():
                        leaves.append(VTreeNode(child.var_id))
                    else:
                        n_parents += 1
                if n_parents:
                    temp_buffer = buffer[(- n_parents):]
                    del buffer[(- n_parents):]
                else:
                    temp_buffer = []
                vtree_root = VTreeNode(children=((leaves + temp_buffer) + [VTreeNode(peek_dnode.var_id)]))
                scope = []
                n_inner_children = 0
                for n in vtree_root.children:
                    if (not n.is_inner()):
                        scope.append(n.var_id)
                    else:
                        n_inner_children += 1
                if n_inner_children:
                    prev_scopes = scopes_t[(- n_inner_children):]
                    del scopes_t[(- n_inner_children):]
                    scope.extend([v for s in prev_scopes for v in s])
                scopes_t.append(scope)
                scopes.append(scope)
                buffer.append(vtree_root)
            dtree_stack.pop()
            last_node_visited = peek_dnode
        else:
            dtree_stack.extend(peek_dnode.children)
    vtree_root.set_scopes(scopes)
    return vtree_root
def from_dtree_to_vtree(dtree_root):
    if (len(dtree_root.get_tree()[0]) == 1):
        vtree_root = VTreeNode(var_id=dtree_root.get_tree()[0][0])
        vtree_root.set_scopes([dtree_root.get_tree()[0]])
        return vtree_root
    scopes = []
    scopes_t = []
    last_node_visited = None
    dtree_stack = [dtree_root]
    buffer = []
    while dtree_stack:
        peek_dnode = dtree_stack[(- 1)]
        if ((not peek_dnode.children) or (last_node_visited in peek_dnode.children)):
            if (not peek_dnode.is_leaf()):
                leaves = []
                n_parents = 0
                for child in peek_dnode.children:
                    if child.is_leaf():
                        leaves.append(VTreeNode(child.var_id))
                    else:
                        n_parents += 1
                if n_parents:
                    temp_buffer = buffer[(- n_parents):]
                    del buffer[(- n_parents):]
                else:
                    temp_buffer = []
                vtree_root = VTreeNode(children=((leaves + temp_buffer) + [VTreeNode(peek_dnode.var_id)]))
                scope = []
                n_inner_children = 0
                for n in vtree_root.children:
                    if (not n.is_inner()):
                        scope.append(n.var_id)
                    else:
                        n_inner_children += 1
                if n_inner_children:
                    prev_scopes = scopes_t[(- n_inner_children):]
                    del scopes_t[(- n_inner_children):]
                    scope.extend([v for s in prev_scopes for v in s])
                scopes_t.append(scope)
                scopes.append(scope)
                buffer.append(vtree_root)
            dtree_stack.pop()
            last_node_visited = peek_dnode
        else:
            dtree_stack.extend(peek_dnode.children)
    vtree_root.set_scopes(scopes)
    return vtree_root