Skip to content

Instantly share code, notes, and snippets.

@arhibot
Created November 6, 2011 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arhibot/1343341 to your computer and use it in GitHub Desktop.
Save arhibot/1343341 to your computer and use it in GitHub Desktop.
-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, []).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment