I have a treeview in which a user can move, delete subtrees, nodes and create nodes.
I do not want to persist every single action at once but the user can say “apply my changes”. Therefore I need to compare the new tree with the old tree and – most important – determine the smallest set of operations to build the new tree. What’s a approach to this problem?
I am using ASP.NET/C# but the question is not really tight to that technology.
Thank you
There are two approaches to your problem. You could just cache all the user’s editing actions and then apply them in a batch update. This addresses the “not at once” part of your goals.
If you want to replace the user actions with an optimal set of actions that produces the same result (for some definition of “optimal”), this is, in general, a very difficult problem to solve. The best known algorithm, I believe, is based on something called the “editing distance between trees” and is described in a technical report by Zhang and Shasha. Be warned that this is very dense reading. There’s a much more readable article here for finding an optimal editing script to change one XML document into another. It’s based on first converting the XML into trees and working on them, so the core algorithms may be directly applicable to your problem. It even comes with pseudocode.