tree data structure

Padraig Brady Padraig at
Fri Sep 6 09:52:46 EDT 2002

Duncan Booth wrote:
> Padraig Brady <Padraig at> wrote in
> news:Us1e9.2865$cP3.6131 at 
>>>assert result==('L0', [('S2', [('S0', [('Q1', [])]), ('S1', [('Q2',
>>>[])]), ('S3', [])])])
>>It certainly does!
>>Just one thing...Why use lists instead of tuples?
> Specifically because you can't append to tuples, but you can append to a 
> list.
> However, a general rule of thumb is to use tuples where you want a record 
> (but don't want the overhead of a full-blown class). So each node here is a 
> record with the fields name,children. Use a list where you want to store a 
> collection of unknown length: so a list is appropriate for the children.


> Note that neither your original list, nor the code I used actually force 
> this to be a tree, you could equally well have a graph with or without 
> cycles. You will find it impossible to represent a graph containing cycles 
> if you use tuples throughout.

True, but I am assured that the parts of the graph I'm interested
in will not have cycles.

thanks again,


More information about the Python-list mailing list