protected static function get_tree($userid, $capability) {
        // Extract raw capability data for this user and capability.
        $pathroleperms = self::get_capability_info_at_each_context($userid, $capability);

        // Convert the raw data into a permission tree based on context.
        $root = self::calculate_permission_tree($pathroleperms);
        unset($pathroleperms);

        // Simplify the permission tree by removing unnecessary nodes.
        $root = self::remove_negative_subtrees($root);
        self::remove_duplicate_nodes($root);

        // Return the tree.
        return $root;
    }