caesar0301 / treelib

An efficient implementation of tree data structure in python 2/3.
http://treelib.readthedocs.io/en/latest/
Other
801 stars 185 forks source link

add a tree to a node of another tree as a subtree #167

Closed Freakwill closed 3 years ago

Freakwill commented 3 years ago

How do I add a tree to a node of another tree as a subtree? I need a method add_tree(tree, parent) to do it like add_node. Sometimes, I also want to merge the root of subtree to the parent node.

The definition would be

def add_tree(self, tree, parent=None):
    if not isinstance(node, Tree):
            raise OSError("First parameter must be object of Tree")
    # tree is a subtree of self and the parent of its root is `parent`

I think, a node is a special tree, regarded as a Tree object (or define a subclass of Tree with only one node). In the sense, add_node is a special case of add_tree.

PS: the issue is from when I create a decision tree for machine learning.