class_docstr: A tree node class for representing a cluster.
  
  Leaf nodes correspond to original observations, while non-leaf nodes
  correspond to non-singleton clusters.
  
  The `to_tree` function converts a matrix returned by the linkage
  function into an easy-to-use tree representation.
  
  All parameter names are also attributes.
  
  Parameters
  ----------
  id : int
      The node id.
  left : ClusterNode instance, optional
      The left child tree node.
  right : ClusterNode instance, optional
      The right child tree node.
  dist : float, optional
      Distance for this cluster in the linkage matrix.
  count : int, optional
      The number of samples in this cluster.
  
  See Also
  --------
  to_tree : for converting a linkage matrix ``Z`` into a tree object.
class_docstr_tok: ["A","tree","node","class","for","representing","a","cluster",".","Leaf","nodes","correspond","to","original","observations",",","while","non-leaf","nodes","correspond","to","non-singleton","clusters",".","The","`","to_tree","`","function","converts","a","matrix","returned","by","the","linkage","function","into","an","easy-to-use","tree","representation",".","All","parameter","names","are","also","attributes",".","Parameters","--","--","--","--","--","id",":","int","The","node","id",".","left",":","ClusterNode","instance",",","optional","The","left","child","tree","node",".","right",":","ClusterNode","instance",",","optional","The","right","child","tree","node",".","dist",":","float",",","optional","Distance","for","this","cluster","in","the","linkage","matrix",".","count",":","int",",","optional","The","number","of","samples","in","this","cluster",".","See","Also","--","--","--","--","to_tree",":","for","converting","a","linkage","matrix","``","Z","``","into","a","tree","object","."]
docstr: Perform 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.
docstr_tok: ["Perform","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","."]
code: def pre_order(self, func=(lambda x: x.id)):
      """
      Perform 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.
  
      """
      # Do a preorder traversal, caching the result. To avoid having to do
      # recursion, we'll store the previous index we've visited in a vector.
      n = self.count
  
      curNode = [None] * (2 * n)
      lvisited = set()
      rvisited = set()
      curNode[0] = self
      k = 0
      preorder = []
      while k >= 0:
          nd = curNode[k]
          ndid = nd.id
          if nd.is_leaf():
              preorder.append(func(nd))
              k = k - 1
          else:
              if ndid not in lvisited:
                  curNode[k + 1] = nd.left
                  lvisited.add(ndid)
                  k = k + 1
              elif ndid not in rvisited:
                  curNode[k + 1] = nd.right
                  rvisited.add(ndid)
                  k = k + 1
              # If we've visited the left and right of this non-leaf
              # node already, go up in the tree.
              else:
                  k = k - 1
  
      return preorder
  
imports: import warnings
  import bisect
  from collections import deque
  import numpy
  from .None import _hierarchy, _optimal_leaf_ordering
  import scipy.spatial.distance
  from scipy._lib._disjoint_set import DisjointSet
label_desc: Use image node_id 10 for calling the ClusterNode obj's underlying member method code with example usage: obj.pre_order(func) and returns: preorder