data structure

Discussion in 'Python' started by noro, Sep 3, 2006.

  1. noro

    noro Guest

    Hello again.

    I have a task i need to do and i can't seem to find an elegent
    solution.
    i need to make a tree like data structure (not necessry a binary tree).


    i would like each node to access his sons in a dicionary kind of why,
    for example: if ROOT node has the name 'A' and 'AA', 'AB' are his
    sons, and 'ABA' is 'AB son etc' (in this name scheme the letters from
    left to right shows the route from the root to the node) then
    ROOT['AB'] will point to 'AB' node and ROOT['AB'][ABA'] will point to
    'ABA' node.

    the tree does not have to be symmarical and every node link to
    different number of nodes. two nodes can have the same name if they are
    in a different location in the tree. so ROOT['factory1]['manager'] and
    ROOT['factory2']['manager'] can be in the same tree and point to
    different objects.

    all up to now i can manage.
    ---------------------
    my problem is this: i would like to find a way to easly construct the
    tree by giving some simple template, somthing similer to:
    "
    ROOT={'factory1':FACTORY,'facory2':FACTORY,'linux':OS,'windows':OS}
    FACTORY={'manager':EMPLOEY,'worker':EMPLOEY,'office':BUILDING,'......}
    OS={'ver':VERSION,'problems':LIST,....}
    "
    i started bulding the class NODE as an extention of "dict" with the
    keys are the childern names and the items are the next node reference.
    the proablem was that as you can see from the above example 'factory1'
    and 'factory2' point to the same object. i would like to have 2
    different objects of FACTORY. making FACTORY a template and not an
    object.

    i'll appreciate any comment
    amit
     
    noro, Sep 3, 2006
    #1
    1. Advertising

  2. noro

    Jason Guest

    noro wrote:
    > Hello again.
    >
    > I have a task i need to do and i can't seem to find an elegent
    > solution.
    > i need to make a tree like data structure (not necessry a binary tree).
    >
    >
    > i would like each node to access his sons in a dicionary kind of why,
    > for example: if ROOT node has the name 'A' and 'AA', 'AB' are his
    > sons, and 'ABA' is 'AB son etc' (in this name scheme the letters from
    > left to right shows the route from the root to the node) then
    > ROOT['AB'] will point to 'AB' node and ROOT['AB'][ABA'] will point to
    > 'ABA' node.
    >
    > the tree does not have to be symmarical and every node link to
    > different number of nodes. two nodes can have the same name if they are
    > in a different location in the tree. so ROOT['factory1]['manager'] and
    > ROOT['factory2']['manager'] can be in the same tree and point to
    > different objects.
    >
    > all up to now i can manage.
    > ---------------------
    > my problem is this: i would like to find a way to easly construct the
    > tree by giving some simple template, somthing similer to:
    > "
    > ROOT={'factory1':FACTORY,'facory2':FACTORY,'linux':OS,'windows':OS}
    > FACTORY={'manager':EMPLOEY,'worker':EMPLOEY,'office':BUILDING,'......}
    > OS={'ver':VERSION,'problems':LIST,....}
    > "
    > i started bulding the class NODE as an extention of "dict" with the
    > keys are the childern names and the items are the next node reference.
    > the proablem was that as you can see from the above example 'factory1'
    > and 'factory2' point to the same object. i would like to have 2
    > different objects of FACTORY. making FACTORY a template and not an
    > object.
    >
    > i'll appreciate any comment
    > amit


    You can make a shallow copy of a dictionary using the dictionary's copy
    method. To make a completely new copy, use the deepcopy function in
    the copy module. Please see the copy module's documentation on the
    limits and differences between deep copies and shallow copies.
    (http://docs.python.org/lib/module-copy.html)

    --Jason
     
    Jason, Sep 3, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    783
    Martin Ambuhl
    Oct 26, 2004
  2. Leo Nunez
    Replies:
    3
    Views:
    1,275
    Neil Kurzman
    Feb 9, 2005
  3. Replies:
    2
    Views:
    631
  4. Replies:
    9
    Views:
    25,489
    Lal Bahadur Singh
    Nov 11, 2011
  5. A
    Replies:
    27
    Views:
    1,658
    Jorgen Grahn
    Apr 17, 2011
Loading...

Share This Page