0

I want to traverse across a tree data, mimicked the scenario as self explanatory python script. I am not able to find why the horizontal nodes are not traversed in recursion.

Explanation: The first string is representation of tree in script. The get_childrens method is how I am getting response from database.

"""
                          1
    11               |              12                        |            13
111     112    113   |   121         122              123     |   
                     |             1221 1222                  |

"""

# Do not edit this function in the same format getting data
def get_childrens(i):
    if i==1: return [11,12,13]
    if i==11: return [111,112,113]
    if i==12: return [121,122,123]
    if i==122: return [1221,1222]

def create_tree(nodeid):
    child_users=get_childrens(nodeid)
    if child_users:
        child_users=[user for user in child_users]
    else: 
        return 

    return {
        "data":{
            "type":nodeid
        },
        "children": [create_tree(e) for e in child_users if create_tree(e)] 
    }



if __name__=="__main__":
    import json
    data= create_tree(1) 

    print json.dumps(data , indent=4)
    print "####################### GETTING THIS #####################"
    {
        "data": {
            "type": 1
        },
        "children": [
            {
                "data": {
                    "type": 11
                },
                "children": []
            },
            {
                "data": {
                    "type": 12
                },
                "children": [
                    {
                        "data": {
                            "type": 122
                        },
                        "children": []
                    }
                ]
            }
        ]
    }
    print "###################### EXPECTED THIS #####################"
    print json.dumps({"data":{"value":1},
        "children":[
                    {"data":{"value":11},
                     "children":[
                        {"data":{"value":111},"children":[]},
                        {"data":{"value":112},"children":[]},
                        {"data":{"value":113},"children":[]},
                      ],  
                     },
                    {"data":{"value":12},
                     "children":[
                        {"data":{"value":121},"children":[
                                   {"data":{"value":1221},"children":[]},
                                   {"data":{"value":1222},"children":[]},
                                                          ]},
                        {"data":{"value":122},"children":[]},
                        {"data":{"value":123},"children":[]},
                      ],  
                     },
                    {"data":{"value":13},
                     "children":[

                      ],  
                     }                    
                    ]

     },indent=4)

2 Answers 2

2

With

[create_tree(e) for e in child_users if create_tree(e)]

you're deliberating eliminating any child who does not in turn have children. So for example "data":{"value":1} only ends up with children 11 and 12, which do have some children themselves, but not 13, which doesn't.

I suspect this is faulty logic, and instead you mean something rather more like

[create_tree(e) or e for e in child_users]
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks Alex, returning the data instead of empty solved the problem.

def create_tree(nodeid):
    child_users=get_childrens(nodeid)
    if child_users:
        child_users=[user for user in child_users]
    else: 
        return {
              "data":{

                 "type":nodeid
              },
              "children":  []
            } 

    return {
              "data":{

                 "type":nodeid
              },
              "children":  [create_tree(e) or e for e in child_users]
            }

1 Comment

I recommend you move this to an edit of your Q -- it's in appropriate as an A. Moreover, if my A helped, it's good practice to upvote and accept it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.