N-ary tree in python

data-structures, python, python-2.7, python-3.x, tree

Solution

class Node(object):
    def __init__(self, name, value):
        self.name = name
        self.value = value
        self.children = []
    def add_child(self, obj):
        self.children.append(obj)

You say youre looking for a "simpler approach without using class" but my claim here is that 9 times out of 10 using a class for this will be the simpler approach.

Problem

I want to create a N-ary tree in which each node will contain a key(name) and a value. 1 root then N children with two fields = name and associate value and again each children have N-children with 2 fields. looking for simpler approach without using class using only dictionary and lists (if possible??). ``` class Node(): #Do something # .... ```

Original source