code: private function findOrCreateChild($tree, $id)
      {
          /* @var NodeInterface $node */
          foreach ($tree->getChildren() as $node) {
              if ($id && $node->getId() == $id) {
                  return $node;
              }
          }
  
          $nodeClass = get_class($tree);
          $node = new $nodeClass();
          $tree->addChild($node);
  
          return $node;
      }
docs: Finds an item in a tree structure or create a new item.
  
  @param NodeInterface $tree
  @param $id
  
  @return NodeInterface