B _t+@s0ddlmZGdddeZGdddeZdS))dequec@sXeZdZddZddZddZddZd d Zd d Zd dZ e Z ddZ ddZ dS) SimpleVisitorcCsdS)a Visit a node in a tree and perform some operation on it. This method should be over-written by a user that is creating a sub-class. Args: node: a node in a tree Returns: nothing N)selfnoderr9/tmp/pip-unpacked-wheel-c5cl1q8n/pyutilib/misc/visitor.pyvisitszSimpleVisitor.visitcCs|jS)a Return the children for a node in a tree. This method has a default implementation, but this implementation will likely be redefined to reflect the tree data structures used for a specific application. Args: node: a node in a tree Returns: A list of children of the specified node. )children)rrrrrr szSimpleVisitor.childrencCst|jdkS)z Return :const:`True` if the node has no children. Args: node: a node in a tree Returns: :const:`True` if this node has no children, and :const:`False` otherwise r)lenr )rrrrris_leaf's zSimpleVisitor.is_leafcCsdS)a Return the "final value" of the search. The default implementation returns :const:`None`, because the traditional visitor pattern does not return a value. Returns: The final value after the search. Default is :const:`None`. Nr)rrrrfinalize4s zSimpleVisitor.finalizecCsHt|g}x4|r>|}||||s |||q W|S)z Perform breadth-first search starting at a node. Args: node: a node in a tree Returns: The value of the :func:`finalize` method. )rpopleftrr extendr r )rrdqcurrentrrrbfs@s   zSimpleVisitor.bfscCsbt|g}xN|rX|}||x2||D]$}||rH||q.||q.Wq W|S)a Perform breadth-first search starting at a node, except that leaf nodes are immediately visited. Immediately visiting leaf nodes eliminates storing and retrieving leaf nodes, which is an expense that may not be necessary if the search order can be changed from the standard BFS. Args: node: a node in a tree Returns: The value of the :func:`finalize` method. )rr rr r appendr )rrrrcrrrxbfsRs    zSimpleVisitor.xbfscCsZt|g}xF|rP|}||||s x t||D]}||q|||qx t||D]}||qNW|t|qW| S)z Perform depth-first search starting at a node, where nodes are visited after their children. Args: node: a node in a tree Returns: The value of the :func:`finalize` method. ) setridrrrr raddr )rrexpandedrrrrrr dfs_postorders    zSimpleVisitor.dfs_postordercCst}t|g}xz|r|}t||ks4||r@||qd}x4t||D]"}|rbd}n ||||qTW| t|qW| S)a% Perform depth-first search starting at a root node for a binary tree, where a node is visited after the left child and before the right child. Args: node: a node in a tree Returns: The value of the :func:`finalize` method. TF) rrrrr rrr rrr )rrrrrfirstrrrr dfs_inorders    zSimpleVisitor.dfs_inorderN) __name__ __module__ __qualname__rr r r rrrdfsrrrrrrrs  rc@sDeZdZddZddZddZddZd d Zd d Zd dZ dS) ValueVisitorcCsdS)a Visit a node in a tree and compute its value using the values of its children. This method should be over-written by a user that is creating a sub-class. Args: node: a node in a tree values: a list of values of this node's children Returns: The *value* for this node, which is computed using :attr:`values` Nr)rrvaluesrrrrszValueVisitor.visitcCs|jS)a Return the children for a node in a tree. This method has a default implementation, but this implementation will likely be redefined to reflect the tree data structures used for a specific application. Args: node: a node in a tree Returns: A list of children of the specified node. )r )rrrrrr szValueVisitor.childrencCst|jdkS)z Return :const:`True` if the node has no children. Args: node: a node in a tree Returns: :const:`True` if this node has no children, and :const:`False` otherwise r)r r )rrrrrr s zValueVisitor.is_leafcCs|S)a Return the "final value" of the search. The default implementation returns the value of the initial node (aka the root node), because this visitor pattern computes and returns value for each node to enable the computation of this value. Args: ans: The final value computed by the search method. Returns: The final value after the search. Defaults to simply returning :attr:`ans`. r)ransrrrr szValueVisitor.finalizecCs||sdSd||dfS)ar Visit a node and return its value if it is a leaf. Args: node: a node in a tree Returns: A tuple: ``(flag, value)``. If ``flag`` is False, then the node is not a leaf and ``value`` is :const:`None`. Otherwise, ``value`` is used to compute the value of the parent node. )FNTN)r r)rrrrrvisiting_potential_leafs z$ValueVisitor.visiting_potential_leafc Cs||\}}|r|Sgg}t}t|g}x|r|d}t||krn||}|d|||q.||\}}|r|d||q.x t||D]} || qW| t||gq.W| |ddS)aO Perform depth-first search starting at a node, where nodes are visited after their children. This method uses a deque to manage the set of nodes that need to be explored. Args: node: a node in a tree Returns: The value of the :func:`finalize` method. rr) r'rrrrrrrr rr ) rrflagvalueZ_valuesrrrr%rrrrdfs_postorder_deques,   z ValueVisitor.dfs_postorder_dequec Cs||\}}|r|S|||dt||gfg}x|\}}}}} xl||kr||} |d7}|| \}}|r| |qL|||||| f| }|| }d}t|}g} qLW||| } |r|dd| q8|| Sq8WdS)aO Perform depth-first search starting at a node, where nodes are visited after their children. This method uses a stack to manage the set of nodes that need to be explored. Args: node: a node in a tree Returns: The value of the :func:`finalize` method. rrN)r'r r rrrr ) rrr(r)_stack_objZ_argListZ_idx_lenZ_resultZ_subr&rrrdfs_postorder_stack:s,     z ValueVisitor.dfs_postorder_stackN) r r!r"rr r r r'r*r/rrrrr$s &r$N) collectionsrobjectrr$rrrrs 6