o
    ˷eb                     @   s   d Z dgZdddZdS )z-
Text-based visual representations of graphs

forest_strTNFc                     sh  ddl }g }|du r|j}n|}|r'd}d}	d}
d}d}d}d}d	}d
}d}nd}d}	d}
d}d}d}d}d}d}d}t jdkrG|| n| sQ|d  }|rZ jn j}|du rz|rn fdd jD }n fdd|	 D }t|d fddt
|D ddd }t |r(| \}}}|v rq| |s|r||	 }|| }n2||
 }|| }n)|r|r|| }|| }n|| }|| }n|r|| }|| }n|| }|| }|r j| d|}n|}||t|  fdd|| D }t
|ddd ddD ]\}}|dk}|||f}|| q|s|du r2d|S dS ) u  
    Creates a nice utf8 representation of a directed forest

    Parameters
    ----------
    graph : nx.DiGraph | nx.Graph
        Graph to represent (must be a tree, forest, or the empty graph)

    with_labels : bool
        If True will use the "label" attribute of a node to display if it
        exists otherwise it will use the node value itself. Defaults to True.

    sources : List
        Mainly relevant for undirected forests, specifies which nodes to list
        first. If unspecified the root nodes of each tree will be used for
        directed forests; for undirected forests this defaults to the nodes
        with the smallest degree.

    write : callable
        Function to use to write to, if None new lines are appended to
        a list and returned. If set to the `print` function, lines will
        be written to stdout as they are generated. If specified,
        this function will return None. Defaults to None.

    ascii_only : Boolean
        If True only ASCII characters are used to construct the visualization

    Returns
    -------
    str | None :
        utf8 representation of the tree / forest

    Example
    -------
    >>> graph = nx.balanced_tree(r=2, h=3, create_using=nx.DiGraph)
    >>> print(nx.forest_str(graph))
    ╙── 0
        ├─╼ 1
        │   ├─╼ 3
        │   │   ├─╼ 7
        │   │   └─╼ 8
        │   └─╼ 4
        │       ├─╼ 9
        │       └─╼ 10
        └─╼ 2
            ├─╼ 5
            │   ├─╼ 11
            │   └─╼ 12
            └─╼ 6
                ├─╼ 13
                └─╼ 14


    >>> graph = nx.balanced_tree(r=1, h=2, create_using=nx.Graph)
    >>> print(nx.forest_str(graph))
    ╙── 0
        └── 1
            └── 2

    >>> print(nx.forest_str(graph, ascii_only=True))
    +-- 0
        L-- 1
            L-- 2
        N+z+-- z    u   :   u   |   zL-> z|-> zL-- z|-- u   ╙u
   ╙── u
   ╟── u   ╎   u   │   u
   └─╼ u
   ├─╼ u
   └── u
   ├── z)input must be a forest or the empty graphc                    s   g | ]} j | d kr|qS )r   )	in_degree).0ngraph N/var/www/ideatree/venv/lib/python3.10/site-packages/networkx/readwrite/text.py
<listcomp>|       zforest_str.<locals>.<listcomp>c                    s    g | ]}t | fd ddqS )c                    s
    j |  S )N)degree)r   r   r	   r
   <lambda>   s   
 z'forest_str.<locals>.<listcomp>.<lambda>)key)min)r   ccr   r	   r
   r      s       c                    s   g | ]\}}|d | kfqS ) r	   )r   idxnode)last_idxr	   r
   r      r   labelc                    s   g | ]}| vr|qS r	   r	   )r   child)seenr	   r
   r      s    )start
)networkxappendlennodes	is_forestNetworkXNotImplementedis_directedsuccadjconnected_components	enumeratesetpopaddgetstrjoin) r   with_labelssourceswrite
ascii_onlynxprintbuf_writeglyph_emptyglyph_newtree_lastglyph_newtree_midglyph_endof_forestglyph_within_forestglyph_within_treeglyph_directed_lastglyph_directed_midglyph_undirected_lastglyph_undirected_midr#   r$   stackr   indentislastthis_prefixnext_prefixr   childrenr   r   islast_next	try_framer	   )r   r   r   r
   r      s   A











1
)TNNF)__doc____all__r   r	   r	   r	   r
   <module>   s    