title: Recursive Java data structure - creating an array of a family tree
body: <p>How do I create an efficient recursive function - that will take an input feed - which could be 1000's of rows long. Into a compacted array. The recursive function will need potentially some kind of lookup to determine if there is a manager above the current employee id.</p>
  
  <p>I'm trying to build an application that may require an entire relationship management tree in an array.</p>
  
  <p>I need to figure a method to create a recursive function over an input data feed to map it to collated output array.</p>
  
  <pre><code>//the input data may look something like this
  [{
      "employeeId": "A1",
      "managerId": "B1",
      "employeeRole": "Sales0",
      "managerRole": "Sales1"
  }, {
      "employeeId": "B1",
      "managerId": "C1",
      "employeeRole": "Sales1",
      "managerRole": "Sales2"
  }, {
      "employeeId": "C1",
      "managerId": "D1",
      "employeeRole": "Sales2",
      "managerRole": "Sales3"
  }, {
      "employeeId": "D1",
      "managerId": "",
      "employeeRole": "Sales3",
      "managerRole": "Sales4"
  }]
  
  
  //create a final output array like this
  [
      "level1id": "A1",
      "level2id": "B1",
      "level3id": "C1",
      "level4id": "D1",
  ]
  </code></pre>
  
  <p>The bare bone pseduo code I have at this stage is something like this</p>
  
  <pre><code>void myMethod(String employeeId, String[] rawData, Map &lt; String, String &gt; dataMap) {
  
      //employeeId -- child id
  
      //rawData - from feed 
  
      //dataMap - the array being built to contain ALL relationships from grand child, parent, grandparent.
  
  
  
      getManager(employeeId, rawData) {
  
          //find the manager of the current employeeid
  
          //managerData[0] - manager id
          //managerData[1] - manager role
  
          return managerData;
      }
  
  
      managerData = getManager(employeeId, rawData);
  
      managerId = managerData[0];
      managerRole = managerData[1];
  
      dataMap.put(managerData[1], managerData[0]);
      // like [{"customerLevel1" : "SKDOL2"}]
  
  
  
      //if there is no manager id - the grandparent manager has been found
      if (managerId !== null)
          return;
      else {
          //this employeeid has a manager above them - loop through again until the grandparent is found (employee id with no manager) 
          myMethod(managerId, rawData, dataMap);
          return;
      }
  }
  </code></pre>