Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
merge([], NewTree)->
merge([{Node, Ch}|Tail], NewTree) ->
case lists:member(Node, NewTree) of
true -> merge(Tail, NewTree);
false -> merge(Tail, [{Node, Ch}|NewTree])
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
add_to_tree_iter(Tree, Child, Childs, [{Parent, NChilds}|NewTree])
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