List of directories into a nested list

I have a list of directories like this

a
a/b
a/c
a/d
a/d/e
a/d/f
a/d/g
a/d/g/h
a/i

I would like to convert this list into another list, nested like this

a{b{} c{} d{e{} f{} g{h{}}} i{}}

Here is a pseudo algorithm for this

Add a:
a{}
Add b:
a{ b{}}
Add c:
a{b{} c{}}
Add d:
a{b{} c{} d{}}
Add e:
a{b{} c{} d{ e{}}}
Add f:
a{b{} c{} d{e{} f{}}}
Add g:
a{b{} c{} d{e{} f{} g{}}}
Add h:
 a{b{} c{} d{e{} f{} g{ h{}}}}
Add i:
  a{b{} c{} d{e{} f{} g{h{}}} i{}}

How can I achieve this?

Thank you!

made a good puzzler for our guys :slight_smile: :

#!/usr/bin/perl -w
my $tree = {};
for (`find . -type d`) {            # change this to e.g. cat infile etc
      chomp;
      my $p = $tree; map { $p->{$_} ||= {}; $p = $p->{$_} } split '/';
}

sub d {
    local $_;
    return join ' ', map { $_.'{'.d($_[0]->{$_}).'}' } sort keys %{$_[0]}
}

$out = d($tree); $out =~ s/(^\.\{|\}$)//g;
print "$out\n";