def pre_order(self, func=(lambda x: x.id)):
<EOL>n = self.count<EOL>curNode = [None] * (<NUM_LIT:2> * n)<EOL>lvisited = set()<EOL>rvisited = set()<EOL>curNode[<NUM_LIT:0>] = self<EOL>k = <NUM_LIT:0><EOL>preorder = []<EOL>while k >= <NUM_LIT:0>:<EOL><INDENT>nd = curNode[k]<EOL>ndid = nd.id<EOL>if nd.is_leaf():<EOL><INDENT>preorder.append(func(nd))<EOL>k = k - <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>if ndid not in lvisited:<EOL><INDENT>curNode[k + <NUM_LIT:1>] = nd.left<EOL>lvisited.add(ndid)<EOL>k = k + <NUM_LIT:1><EOL><DEDENT>elif ndid not in rvisited:<EOL><INDENT>curNode[k + <NUM_LIT:1>] = nd.right<EOL>rvisited.add(ndid)<EOL>k = k + <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>k = k - <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>return preorder<EOL>
Performs pre-order traversal without recursive function calls.

When a leaf node is first encountered, ``func`` is called with
the leaf node as its argument, and its result is appended to
the list.

For example, the statement::

   ids = root.pre_order(lambda x: x.id)

returns a list of the node ids corresponding to the leaf nodes
of the tree as they appear from left to right.

Parameters
----------
func : function
    Applied to each leaf ClusterNode object in the pre-order traversal.
    Given the i'th leaf node in the pre-ordeR traversal ``n[i]``, the
    result of func(n[i]) is stored in L[i]. If not provided, the index
    of the original observation to which the node corresponds is used.

Returns
-------
L : list
    The pre-order traversal.