I am playing with a binary tree implementation in Erlang. Here is a fragment of the code to give you an idea:
-record(node, {key, value, left, right}).
% ...
insert(Tree, {Key, Value}) when Key == Tree#node.key ->
#node{key=Key,
value=Value,
left=Tree#node.left,
right=Tree#node.right};
insert(Tree, {Key, Value}) when Key > Tree#node.key ->
Tree#node{right=insert(
Tree#node.right, {Key, Value})};
% ...
Here when I insert a new key and value to the tree, I return a new tree with the inserted (or modified) node.
Question: will VM actually copy the tree and GC the old one (which would be inefficient), or copy the references to the old branches and alter only nodes/branches affected by the new key?
Related: