How to navigate tree data ?

How to navigate a tree data ?

when you know the data structure are repeating in the same fashion, what's there to worry ?
just go ahead and hit it by recursion function.

so here's a example of tree data structure (family tree)

val  = {'mom': 'xx',
        'dad': 'yy',
        'kids_family': [{'mom': 'zz',
                  'dad': 'rr',
                  'kids_family': [{'mom': 'gg',
                            'dad': 'jj',
                            'kids_family': [{'mom':'oo',
                                      'dad': 'pp',
                                      'kids_family': []
                           {'mom': 'vv',
                            'dad': 'bb',
                            'kids_family': []},
                           {'mom': 'kk',
                            'dad': 'll',
                            'kids_family': []}
                 {'mom': 'mm',
                  'dad': 'nn',
                  'kids_family': []}

We know there will be three keys available mom, dad and kids_family. So only thing grows is 'kids_family’. Put for loop for 'kids_family’ and don’t worry about mom and dad.

Recursive function goes like this

def rec(data, gen):
    print "\t"*gen,"mom :",data['mom']
    print "\t"*gen,"dad :",data['dad']
    print "\t"*gen,"we are ",gen," gen"
    print "\n"
    for i in data['kids_family']:
        gen += 1
        rec(i, gen)
        gen -= 1

tats all the output goes like this

        mom : xx
        dad : yy
        we are  1  gen

                mom : zz
                dad : rr
                we are  2  gen

                        mom : gg
                        dad : jj
                        we are  3  gen

                                mom : oo
                                dad : pp
                                we are  4  gen

                        mom : vv
                        dad : bb
                        we are  3  gen

                        mom : kk
                        dad : ll
                        we are  3  gen

                mom : mm
                dad : nn
                we are  2  gen