Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > convert flat structure into hierarchical one

Reply
Thread Tools

convert flat structure into hierarchical one

 
 
Ksenia Marasanova
Guest
Posts: n/a
 
      09-26-2004
I get this kind of list from a database. (The tuple structure is: id,
name, parent_id)

[(1, 'grandparent', None), (2, 'parent', 1), (3, 'otherparent', 1), (4,
'child', 3)]

I would like to transfer it (in Python) into a tree structure. I don't
care much about format, as long as I'll be able to get all the
information, based on one id. For example, if I have id=3, I want to
get
- name ('otherparent')
- children (one child, with id=4, name='child')
- parent id

Any tips? Recipes?


Thanks,
Ksenia.

 
Reply With Quote
 
 
 
 
Dan Perl
Guest
Posts: n/a
 
      09-26-2004
I started writing a script that would implement a solution to your problem,
but it turned out that it is not so trivial. You have all kind of issues,
depending on whether your structure is sparse or not, and what especially
made me give up (I may still try it later, but right now I don't have the
time) is that you may retrieve a child before its parent, so either you
create a dummy that you update later, or you recursively retrieve all the
ancestors in the tree up to the root of the tree.

Here is my suggestion in brief, though. Implement a class for the tree and
a class for the tree nodes. The 'tree' class keeps a list of instances of
the 'tree node' class. Depending on the sparsity of the tree, you can have
a list indexed by the id's of the nodes (with None for empty spaces), or an
arbitrary list and you do the search for the node in the list that has the
particular id.

The 'tree node' class would have 4 attributes: id, name, parent_id, and
children. 'children' would be a list. BTW, this attribute is not
mandatory, but it is more efficient having it if it is rarely updated but
often accessed.

The 'tree' class would also have a method 'addNode'. In it, create a new
instance of the 'tree node class', add it to the list in the tree and update
the node matching the parent_id. Here is where the complication come in.
You may have to pad the list with 'None' and you may not yet have a node
matching the parent in the 'tree' list. Override the __getitem__ method in
the 'tree' class to retrieve the node based on its id.

Hope this helps.

Dan

"Ksenia Marasanova" <> wrote in message
news:mailman.3941.1096224597.5135.python-...
>I get this kind of list from a database. (The tuple structure is: id, name,
>parent_id)
>
> [(1, 'grandparent', None), (2, 'parent', 1), (3, 'otherparent', 1), (4,
> 'child', 3)]
>
> I would like to transfer it (in Python) into a tree structure. I don't
> care much about format, as long as I'll be able to get all the
> information, based on one id. For example, if I have id=3, I want to get
> - name ('otherparent')
> - children (one child, with id=4, name='child')
> - parent id
>
> Any tips? Recipes?
>
>
> Thanks,
> Ksenia.
>



 
Reply With Quote
 
 
 
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      09-27-2004
On Sun, 26 Sep 2004 19:49:41 +0200, Ksenia Marasanova <>
declaimed the following in comp.lang.python:

> I get this kind of list from a database. (The tuple structure is: id,
> name, parent_id)
>
> [(1, 'grandparent', None), (2, 'parent', 1), (3, 'otherparent', 1), (4,
> 'child', 3)]


Note that your data has "parent" and "otherparent" both linked
to the one "grandparent"... I hope this isn't supposed to represent a
genealogical structure... <G> (The latter should have two "parent-ID"
pointers per tuple: "father" and "mother".)

>
> I would like to transfer it (in Python) into a tree structure. I don't
> care much about format, as long as I'll be able to get all the
> information, based on one id. For example, if I have id=3, I want to
> get
> - name ('otherparent')
> - children (one child, with id=4, name='child')
> - parent id
>


If you truly want it as a tree in Python memory, you'll have to
traverse the tree in other to find any particular node -- especially if
the number of children per node is variable (meaning you can't reserve
ID#s for them -- Genealogical /ancestor/ reporting schemes using
ahnentafel numbering can be computed; for node-n, father is node-n*2,
mother is node-n*2 + 1).

For general usage I'd forget using a literal tree. Stuff the
data in a dictionary using the ID# as the key, and the related data
being the name, parent ID#, list of child ID#s

{3"otherparent", 1, [4])}

--
> ================================================== ============ <
> | Wulfraed Dennis Lee Bieber KD6MOG <
> | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <

 
Reply With Quote
 
Ksenia Marasanova
Guest
Posts: n/a
 
      09-27-2004
Thanks to all for helpfull suggestions!
I think I'll use the tree class as Dan suggested, but instead of
creating the list of node instances in advance, will do it on demand.
I'll use the function of Mike in the constructor of the tree, and will
use the resulting data for internal tree searching and node generation.
I think it's much faster then creating all the node instances from the
beginning, and I actually seldom need a whole three, only parts of it.

And no, it's not a genealogical structure .. I used words as 'parent'
and 'child' just to explain the tree structure better. It's for a
website, where all navigation parts are stored in one tree. Until now,
I had a recursive SQL function in PostgreSQL, but with the growing of
the tree (hey, it's alive! the function is getting slower, so I
want to move it to Python.

Ksenia.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
serializing an arbitrary data structure into a flat buffer (raw contiguousmemory block) Alfonso Morra C Programming 5 10-04-2005 01:55 PM
serializing an arbitrary data structure into a flat buffer (raw contiguousmemory block) Alfonso Morra C++ 0 10-03-2005 07:13 AM
Flat to hierarchical using attributes Steve Mac XML 3 12-31-2004 01:03 PM
WSDL for hierarchical structure? Michael Schuerig XML 0 11-09-2004 12:42 PM
Converting 'flat' gate level names to hierarchical names Paddy McCarthy VHDL 3 09-24-2004 05:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57