Node with as many children as possible python
<p>I want to create a data structure in Python, but since I'm very C oriented. I need a little bit of help.</p>

<p>In general, I want to create a Node class which will contain the data, a pointer to a sibling, pointers to the children and a pointer to the parent.</p>

<p>this is a way to think of the Node class:</p>

<pre><code>                                 NODE
            /            /         ...  \                   \
       child_node1 - child_node2 - ... - child_node(N-1) - child_nodeN
</code></pre>

<p>What I'm struggling with so far is:
I want to overload the '+' operator for the Node class so I can do this:</p>

<pre><code>node1 = Node("data1")
node2 = Node("data2", 3)
node1 = node1 + node2
</code></pre>

<p>So basically make the 2 nodes, siblings. </p>

<p>Here's my code: </p>

<pre><code>class Node:
def __init__(self, data = None, numberOfChildren = 0):
    '''
    Creates a new Node with *numberOfChildren* children. 
    By default it will be set to 0, meaning it will only create the root of the tree.
    No children whatsoever.
    '''
    self.__sibling_count = 0
    self.__parent = Node()
    self.__sibling = Node()
    self.__data = data

    self.__children = []
    if numberOfChildren != 0:
        '''
        The Node has children and we need to initialize them
        '''
        for i in range(numberOfChildren):
            self.__children[i] = Node()

def getParent(self):
    return self.__parent


def getData(self):
    return self.__data

def getChild(self, i):
    '''
    Returns the ith child of the current *Node*.
    '''
    return self.__children[i]


def __add__(self, other):
    '''
    Overloads the *+* function so that *Node* objects can be added.
    The 2 merged *Node* elements will now be siblings.
    ex. node1  = Node()
        node2 = Node()
        node1 = node1 + node2
    '''
    if self.__sibling_count == 0:
        self.__sibling = other
        self.__sibling_count += 1
    return self
</code></pre>

<p>But when I try to add 2 nodes like this:</p>

<pre><code>node1 = Node()
node2 = Node()
node1 = node1 + node2
</code></pre>

<p>I get a <code>RuntimeError: maximum recursion depth exceeded</code>. Why is this happening?</p>

<p>Operator overriding in Python is allowed, but using the <code>+</code> operator for something that is not concatenation or summing is frowned upon. A more pythonic implementation would be something like this untested fragment:</p>

<pre><code>class Node(object):
    def __init__(self, parent=None):
        self.set_parent(parent)
        self.children = set()

    def set_parent(self, parent):
        if self.parent and self.parent is not parent:
            self.parent.children.remove(self)
        self.parent = parent

    def siblings(self):
        if self.parent is None:
            return []
        return [_ for _ in self.parent.children if _ is not self]

    def add_child(self, node):
        self.children.add(node)
        node.set_parent(self)

    def add_sibling(self, node):
        assert self.parent, "root node can't have siblings"
        self.parent.add_child(node)
</code></pre>

<p>... and so on. Off course you can override the <code>+</code> operator to perform <code>add_sibling</code>, but the gist of it is to rely heavily on the native collections.</p>

<p>If you want to create a note with 3 children, it would be:</p>

<pre><code>root = Node()
nodes = [Node(parent=root) for _ in range(3)]
</code></pre>

python tree