01-12-2015, 10:07 PM
Turns out that this "in-place" sorting (which is in essence reverse pre-order traversal) produces adequate results only on very simple objects. I now create a linked structure instead:
I still haven't found a suitable flattening principle though.
Code:
branch_out(Fs, We) ->
{_,A,We0} = distribute(Fs, We),
branch_out(orddict:new(), [A], We0).
branch_out(Lookup, [], We) ->
{flatten_tree(Lookup, []),We};
branch_out(Lookup, [{F,L,R}|T], We)->
{F0,A0,We0} = distribute(L, We),
{F1,A1,We1} = distribute(R, We0),
branch_out(
orddict:store(F, {F0,F1}, Lookup), [A0,A1|T], We1);
branch_out(Lookup, [_|T], We) ->
branch_out(Lookup, T, We).
I still haven't found a suitable flattening principle though.