public static function buildTreeAndGetRoots(array $objects)
    {
        $nodeClass = get_called_class(); // Use late static binding so new nodes are created as the calling subclass.

        // Build a TreeNode for each object.
        $nodes = array();
        foreach ($objects as $object) {
            $nodes[] = new $nodeClass($object);
        }

        // Loop through each node and update set its parent and children.
        foreach ($nodes as $node) {
            $object = $node->getObject();

            foreach ($nodes as $potentialParentNode) {
                if ($potentialParentNode->getObject()->isParentOf($object)) {
                    $node->setParent($potentialParentNode);
                    $potentialParentNode->addChild($node);

                    break;
                }
            }
        }

        // Now find the root nodes and return them.
        $roots = array();
        foreach ($nodes as $potentialRoot) {
            if ($potentialRoot->isRootNode()) {
                $roots[] = $potentialRoot;
            }
        }

        return $roots;
    }