score:0

Try modifying your click function to this:

function click(d) {
    root.children.forEach(collapse);
    if (d.children) {
        d._children = d.children;
        d.children = null;
    } else {
        d.children = d._children;
        d._children = null;
    }
    update(d);
}

Basically it would collapse all the "limbs" before "expanding" the new one. There might be some minor bugs but I can help you fix, if you can host your working code somewhere it would be much easier.


Related Query