Niko
Niko

Reputation: 4448

Python Binary Tree

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)

Upvotes: 1

Views: 592

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121972

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())))

Upvotes: 3

Related Questions