title: Algorithm of finding node and its parent in tree data
body: <p>Let's say I have Javascript object structured below :</p>
  <pre><code>   const data = [{
      id: 1,
      type: &quot;ORGANIZATION&quot;,
      children: [
          {
              id: 11,
              type: 'ORGANIZATION',
              children: [
                  {
                      id: 111,
                      type: 'DEPARTMENT',
                      children: [
                          {
                              id: 1111,
                              type: 'DEPARTMENT',
                              children: null // final child
                          }
                      ]
                  },
                  {
                      id: 112,
                      type: 'DEPARTMENT',
                      children: [
                          //more children
                      ]
                  }
              ]
          },
          {
              id: 12,
              type: 'DEPARTMENT',
              children: [
                  //more children
              ]
          }
      ]
  }]
  </code></pre>
  <p>In above structure, an <code>ORGANIZATION</code> node can have a list of smaller <code>ORGANIZATION</code> node and <code>DEPARTMENT</code> node, while <code>DEPARTMENT</code> node can have a list of smaller <code>DEPARTMENT</code>.
  I'm trying to write an algorithm to get the closet parent of any node and the information of itself in the tree, following these rules:</p>
  <ul>
  <li><p>Algorithm will return 2 object <code>orgInfo</code> and <code>deptInfo</code>.</p>
  </li>
  <li><p>If the node is <code>ORGANIZATION</code> type, return <code>orgInfo</code> with information itself and return <code>deptInfo</code> null.</p>
  </li>
  <li><p>If the node is <code>DEPARTMENT</code> type, return the closet <code>orgInfo</code> and return <code>deptInfo</code> with information itself.</p>
  </li>
  </ul>
  <p>For example if the algorithm search for <code>id:1111</code> node , it will return <code>deptInfo</code> which has id = 1111 and <code>orgInfo</code> which has id = 11 (closet parent)</p>
  <p>I already have my algorithm in Javascript but when I try to put it in my React Native app it rendered slowly. Here my code:</p>
  <pre><code>const itemSelector = createSelector( // using selector for Redux 
      [
          state =&gt; state.LookUpReducer.selectedItem,
          state =&gt; state.globalReducer.department_tree,
          
      ],
      (item, tree) =&gt; {
          let nodePath = [];
          let orgId, deptId;
          searchTree(tree, item.id, (item) =&gt; nodePath = [...item]);
          console.log(nodePath);
          nodePath.reverse().some(item =&gt; {//reverse the path to find the closet parent
              if (item.type == 'ORGANIZATION') {
                  orgId = item.id
                  return true
              }
              return false
          })
          nodePath.some(item =&gt; {
              if (item.type == 'DEPARTMENT') {
                  deptId = item.id
                  return true
              }
              return false
          })
          console.log(&quot;OrgID: &quot; + orgId + &quot; DeptID:&quot; + deptId);
          return {
            .....
           }
      }
  )
  
  const searchTree = (tree, nodeId, callback, path = []) =&gt; {
  //nodeId : the node to be search
      tree.forEach(item =&gt; {
          path.push({
              id: item.id,
              type: item.type
          });
          if (item.id == nodeId) {
              callback(path); // node founded
          }
          else {
              if (item.children) searchTree(item.children, nodeId, callback, path)
          }
          path.pop();
      })
  }
  
  </code></pre>
  <p>I know that my algorithm is running through all the node in tree and I still don't know how to stop it when it finds the node. Anyone can help me to find a better solution for my algorithm ?</p>