0

I am trying to create a Node class that implements an n-ary tree but I also want to keep track of the parent of each node to trace back to the root.

class Node(object):
    def __init__(self, state, children=None, parent=None):
        self.state = state
        self.children = children or []
        self.parent = parent
    def add(self, obj):
        self.children.append(Node(obj))
        Node(obj).parent = self.state

This is what I have been working on. But when I check to see a node's parent, it prints None. In a binary tree, checking if the child was to the left or right makes it easy but for an n-ary tree, I don't really understand how to go about it with explicitly making it the parent.

I am pretty new to Python and coding so I would really appreciate it if anyone could help out! Thank you!

EDIT:

I ran this on the IDE:

>>> n = Node(4)
>>> l = [1,2,3]
>>> for i in l:
    n.add_child(i)


>>> n.children.state
Traceback (most recent call last):
  File "<pyshell#63>", line 1, in <module>
    n.children.state
AttributeError: 'list' object has no attribute 'state'
>>> for child in n.children:
    print(child.state)


1
2
3
>>> for child in n.children:
    print(child.parent)


None
None
None
>>> 
Raunak Anand
  • 47
  • 1
  • 2
  • 8

2 Answers2

1

instead of self.parent = parent

you want to do

self.parent = None # for root node only

for child in children:
  child.parent = self
ashish singh
  • 6,526
  • 2
  • 15
  • 35
  • would this change in the code be done in the initialization or the add() function? And if I am doing this, would I need to keep "parent" as an argument for __init__? – Raunak Anand May 24 '20 at 02:37
  • this is for constructor, `add` function should also change.. please note that in `add` you are creating two different nodes. ie .. you are adding one node to the children array but you are setting parent on a completely different node – ashish singh May 24 '20 at 02:38
  • just for clarification, how am I setting the parent on a completely different node when using Node(obj).parent = self.state? – Raunak Anand May 24 '20 at 02:40
  • Node(obj) creates a new node every time you call it – ashish singh May 24 '20 at 02:41
  • So instead I should just do obj.parent = self? – Raunak Anand May 24 '20 at 02:44
  • please try to experiment a bit – ashish singh May 24 '20 at 02:45
  • I am trying but I am kinda clueless on how to go about this. I would really appreciate more guidance since you've been helping me out so much. – Raunak Anand May 24 '20 at 02:48
  • read this https://stackoverflow.com/questions/25437376/implement-a-tree-where-children-and-parents-can-refer-to-each-other – ashish singh May 24 '20 at 02:50
  • Thank you so much. I think you made me understand the constructor bit properly though. But when I still try adding the children and then print the children's parent, it prints out none. How do I fix that? – Raunak Anand May 24 '20 at 03:26
  • I just got it. Thank you so much for helping me! – Raunak Anand May 24 '20 at 03:28
1
self.children.append(Node(obj))

Here you are creating a Node instance on the fly and appending it to the children list

Node(obj).parent = self.state

That is an extra instance of Node on the fly which you are assigning its parent to be self.state

I think you are trying to do something like this:

node = Node(obj)
node.parent = self
self.children.append(node)

With this you are creating an instance of Node and storing its reference in the variable node, then assigning its parent to be the current Node instance (with the self reference) and saving the reference to its children list.

Also, you could do:

self.children.append(Node(obj, None, self))

since you are assigning the parent in the constructor.

cmt
  • 46
  • 1
  • 4