1

I'm working on a binary tree in Python3 and so far almost everything has been working like expected; however, I have a function that is supposed to return a list of all children for any given node and for whatever reason I'm only getting a list of the object addresses, and not calling my overridden __str__(self) method.

from collections import deque # http://docs.python.org/3.1/tutorial/datastructures.html
class BinaryNode: # binary tree functionality via iterative means
 def __init__(self, name, data):
 self.Left = None
 self.Right = None
 self.Parent = None
 self.Name = name
 self.Data = data
 return
 def AddNew(self, name, data):
 q = []
 q.append(self)
 while q:
 i = q.pop()
 if i.Name == name:
 i.Data = data
 return i
 elif name < i.Name:
 if i.Left:
 q.append(i.Left)
 else:
 i.Left = BinaryNode(name, data)
 i.Left.Parent = i
 return i.Left
 else:
 if i.Right:
 q.append(i.Right)
 else:
 i.Right = BinaryNode(name, data)
 i.Right.Parent = i
 return i.Right
 def Find(self, name):
 q = deque()
 q.append(self)
 '''if self.Left: q.append(self.Left)
 if self.Right: q.append(self.Right)'''
 while q:
 i = q.pop()
 print(i)
 if i.Name == name:
 return i
 elif name < i.Name:
 if i.Left: q.append(i.Left)
 else: return None
 else:
 if i.Right: q.append(i.Left)
 else: return None
 def Children(self):
 children = []
 q = deque()
 if self.Left: q.append(self.Left)
 if self.Right: q.append(self.Right)
 while q:
 i = q.popleft()
 if i.Left: q.append(i.Left)
 if i.Right: q.append(i.Right)
 children.append(i)
 return children
 def Parents(self):
 lst = []
 i = self.Parent
 while i is not None:
 lst.append(i)
 i = i.Parent
 return lst
 def __str__(self): return "{} : {}".format(self.Name, self.Data)

and I'm testing it by calling

test = BinaryNode("Jesse", 21)
print(test)
print(test.AddNew("David", 22))
print(test.AddNew("Marli", 23))
print(str(test.Children()))
print(test.Find("David"))
print(test.Find("David").Children())
print(test.Find("Gary")) #Will return None

with the resulting console output

Jesse : 21
David : 22
Marli : 23
[<__main__.BinaryNode object at 0x000000000333E160>, <__main__.BinaryNode object at 0x000000000333E1D0>, <__main__.BinaryNode object at 0x000000000333E198>]
David : 22
[<__main__.BinaryNode object at 0x000000000333E1D0>]
None

UPDATE: Here is the answer I implemented:

def __repr__ (self): return str(self)
asked Oct 15, 2013 at 9:44

1 Answer 1

3

Python containers always use the representation of contained objects.

Implement a __repr__ method too and that'll be used when printing the list; you can make it an alias for __str__ if you wish:

 __repr__ = __str__

or explicitly print each element in your list:

print(', '.join(map(str, test.Children())))
answered Oct 15, 2013 at 9:47
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, thanks for that. Definitely didn't know about that. Here's what I ended up doing def __repr__ (self): return str(self)
On an unrelated note people should be wary, I think I need to redo my AddNew method.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.