tree data structure

Duncan Booth duncan at NOSPAMrcp.co.uk
Fri Sep 6 09:12:31 EDT 2002


Padraig Brady <Padraig at Linux.ie> wrote in
news:Us1e9.2865$cP3.6131 at news.iol.ie: 

>> 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.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list