So I was writing up a simple binary tree in Python and came across [...]
I don\'t believe this to be related to the Ellipsis object, more it seems to have something
Ok, so in points:
You're creating infinite data structure:
def Keys(x,y=[])will use the same 'y' in each call. This just isn't correct.
The print statement, however, is clever enough not to print an infinite data, but to mark self-reference with a [...] (known as Ellipsis)
a.keys()[1][1][1]and so on. Why shouldn't you?
y = y[:] statement simply copies the list y. Can be done more soundly with y = list(y)Try using the following code:
def Keys(x,y=None):
if y is None:
y = []
if len(x):
y += [x[2], Keys(x[0],y), Keys(x[1],y)]
return y
But still I guess that it can bite you. You're still using the same variable y (I mean the same object) in three places in one expression:
y += [x[2], Keys(x[0], y), Keys(x[1], y)]
Is that what you really want to achieve? Or maybe you should try:
def mKeys(x,y=None):
if y is None:
y = []
if len(x):
z = [x[2], mKeys(x[0], y), mKeys(x[1],y)]
return z
return []