public
Created

  • Download Gist
gistfile1.erl
Erlang
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
-module(t).
-compile(export_all).
 
merge([], NewTree)->
NewTree;
 
merge([{Node, Ch}|Tail], NewTree) ->
case lists:member(Node, NewTree) of
true -> merge(Tail, NewTree);
false -> merge(Tail, [{Node, Ch}|NewTree])
end.
 
add_to_tree_iter(Tree, _, [], NewTree) ->
merge(Tree, NewTree);
 
add_to_tree_iter(Tree, Parent, [Child| Childs], NewTree) ->
case proplists:get_value(Parent, Tree) of
undefined -> add_to_tree_iter(Tree, Child, Childs, [{Parent, [Child]}|NewTree]);
HChilds when is_list(HChilds) ->
NChilds = case lists:member(Child, HChilds) of
false -> [Child|HChilds];
true -> HChilds
end,
add_to_tree_iter(Tree, Child, Childs, [{Parent, NChilds}|NewTree])
end.
 
add_to_tree(Tree, [From| Childs]) ->
add_to_tree_iter(Tree, From, Childs, []).

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.