in place-ness of list.append

skip at pobox.com skip at pobox.com
Mon Feb 5 08:31:21 EST 2007


>>>>> "Bart" == Bart Van Loon <bbbart at inGen.be> writes:

    >> Such an operation will be O(N**2), 

    Bart> why is that?

The a[:] operation makes a copy of a (as will the x = a + [n] idiom).

    Bart> I am building a binary tree where each node is a list. the two
    Bart> children are the parent list + 1 and the parent list + 2.

You might consider creating each node as

    left = [parent, 1]
    right = [parent, 2]

You thus wind up with a nested set of two-element nodes, e.g.:

                                        []
                     [[], 1]                           [[], 2]
         [[[], 1], 1]       [[[], 1], 2]   [[[], 2], 1]       [[[], 2], 2] 

where the left-hand side of each node is just a reference to the parent
node.  Once the tree is built if you can flatten the resulting node lists to
generate lists which are more efficient for direct element access.

Again, this all depends on how big your trees are, what you do with them
once they are built, if you need to search the tree while building the tree,
etc.

Skip



More information about the Python-list mailing list