title: Build a graph recursively - breadth first
body: <p>i want to build a "graph" in the following style:</p>
  
  <pre><code>{
  "name":cersei
  "children": [
      {
          "name": "baratheon", 
          "children": [
              {
                  "name": "cersei",
                  "children": []
              }, 
              {
                  "name": "baratheon",
                  "children": [],
              }
          ], 
      }, 
      {
          "name": "joffrey", 
          "children": [
              {
                  "name": "robert", 
                  "children": []   
              }, 
              {
                  "name": "cersei", 
                  "children": []
              }
          ]            
      }
  ]
  }
  </code></pre>
  
  <p>But i build this via depth-first. That means the first element of "children" is fully build, after that the second element of "children" is build. This is the recurse-function:</p>
  
  <pre><code>def recurse(dicts, depth):
      if depth &gt;=0:
          dicts["children"] = []
      child_elements = [] //do something to get your child-elements
      for child in child_elements:
          if depth &gt;=0:
              child_dict = dict(name=word[0])
              dicts["children"].append(child_dict)
              recurse(child_dict, depth-1)
  </code></pre>
  
  <p>How can i change the code that it builds the whole "level" first and appends the childrens-children later?  I've got the problem, that i don't know how to call the level 1 dictionary, because its kind of a dictionary in the dictionary...</p>
  
  <p>Kind regards and thanks for your help, FFoDWindow.</p>
  
  <p>--------------------****UPDATE****---------------------------
  I solved the issue myself. Actually it was pretty simple. I only had to save the temporarly build "children"-elements in an extra list. Now the tree is build breadth-first. Here is my recurse function:</p>
  
  <pre><code>def recurse( input_dict, level_list, depth):
       next_level_list = []
       for dictionary in input_dict:
             child_elements = [...] //get the data for your children
             for child_element in child_elements:
                 dictionary["children"].append = dict(name = child_element)
                 next_level_list.append(dictionary["children"][-1])
        if depth &gt;=0:
             recurse(input_dict, next_level_list, depth-1)
  </code></pre>