<jupyter_start><jupyter_text>TreeStruct TreeStruct is a Python module which simplifies the task of creating, traversing, manipulating and visualizing tree structures. Let's start by importing key objects from `treestruct`:<jupyter_code>from treestruct import Node, visualize, FORWARD, BACKWARD<jupyter_output><empty_output><jupyter_text>Basics Let's create our first tree struct using the **Node** object:<jupyter_code>a = Node('a', children=[
    Node('b', children=[
        Node('d', children=[
            Node('h')
        ]),
        Node('e', children=[
            Node('i'),
            Node('j')
        ])
    ]),
    Node('c', children=[
        Node('f', children=[
            Node('k')
        ]),
        Node('g')
    ])
])<jupyter_output><empty_output><jupyter_text>**Node** accepts a `data` attribute. This can be any arbitrary object.<jupyter_code>a.data<jupyter_output><empty_output><jupyter_text>We can traverse the tree structure via `depth_first_traversal` and `breadth_first_traversal`:<jupyter_code>def callback(node, obj):
    if node.data in ('e', 'f'):
        print 'Found node of interest', node
        obj.append(node)
    else:
        print 'Ignoring node', node

result = a.depth_first_traversal(callback=callback, direction=FORWARD, obj=[])
result<jupyter_output>Ignoring node <Node a>
Ignoring node <Node c>
Found node of interest <Node f>
Ignoring node <Node k>
Ignoring node <Node g>
Ignoring node <Node b>
Found node of interest <Node e>
Ignoring node <Node i>
Ignoring node <Node j>
Ignoring node <Node d>
Ignoring node <Node h><jupyter_text>We can search for nodes via `.find` and `.find_all`:<jupyter_code>e = a.find(condition=lambda n: n.data == 'e')
e<jupyter_output><empty_output><jupyter_text>We can access the children of the node via the `children` attribute, and the parents via the `parents` attribute:<jupyter_code>e.children
e.parents<jupyter_output><empty_output><jupyter_text>If we expect the parent to always have a single parent, we can call `e.parent.one()` to return it directly:<jupyter_code>e.parents.one()<jupyter_output><empty_output><jupyter_text>Visualization We can visualize graphs via the `visualize` module. This module is a basic wrapper around the `graphviz` module. The `Graph` class gives more control on how to construct the graph outputs, but for now we will the `view_inline` function, which returns a Jupyter Notebook compatible graph:<jupyter_code>visualize.view_inline(a)<jupyter_output><empty_output><jupyter_text>Only the subtree of the given node is visualized:<jupyter_code>visualize.view_inline(e)<jupyter_output><empty_output><jupyter_text>Paths We can extract a path from our tree structure via flattening and re-building:<jupyter_code>flattened_lists = e.flatten()
flattened_lists
paths = [Node.from_nodes(l) for l in flattened_lists]
graphs = [visualize.view_inline(p) for p in paths]
graphs
graphs[0]
graphs[1]<jupyter_output><empty_output><jupyter_text>Manipulation Nodes can be deleted from trees by via `delete`. By default the node and all its children are deleted from the graph, though by providing `FORWARD` as a direction, we only delete the children from the tree:<jupyter_code>e.delete(direction=FORWARD)
visualize.view_inline(a)<jupyter_output><empty_output><jupyter_text>Nodes can also be added as follows:<jupyter_code>e.children.add(Node('z'))
visualize.view_inline(a)<jupyter_output><empty_output><jupyter_text>Exporting and importing We can convert a node structure to a Python dictionary via `.to_dict`. If you're required to transform the node's data object, you can pass in a function via the `data_converter` parameter which will be called for each data object.<jupyter_code>a_dict = a.to_dict(data_converter=str)[0]
a_dict<jupyter_output><empty_output><jupyter_text>You can also convert a Python dictionary back into a node structure using `.from_dict`:<jupyter_code>new_a = Node.from_dict(a_dict, data_converter=str)
visualize.view_inline(new_a)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10233258088788563, 'fraction_numerical': 0.0007524454477050414, 'mean_word_length': 5.725126475548061, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '355274', 'license': 'MIT', 'n_tokens_mistral': 1311, 'n_tokens_neox': 1263, 'n_words': 388, 'path': 'examples/treestruct.ipynb', 'repo_name': 'srafehi/treestruct', 'size': '3987'}