Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

N-ary tree in python

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
    # ....
like image 602
pankaj udaas Avatar asked Nov 25 '25 15:11

pankaj udaas


1 Answers

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.

like image 168
stackPusher Avatar answered Nov 27 '25 04:11

stackPusher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!