I've got a tree structure that I need to rearrange (drag and drop) and then submit the changes.
What's going to be the best way to capture the changes? As I see it there are two ways:
- Store every change command, submit the list of changes then execute each one
- Serialize the tree and then diff the new tree with the old tree to work out what's changed, then execute the changes
1 seems easiest to implement, although it could be very wasteful if many repetitive actions have occurred (i.e. dragging nodes around many times, but putting them back where they started)
2 avoids the above problem, but how can I "diff" the trees to work out what parental change commands to execute? Presumably there are algorithms for this?
Edit To clarify, every node has an "id" and a "parentId". I need to allow users to rearrange the tree (thereby changing the parentId of some nodes).
For option 2, should it be straightforward enough to simply serialize the changed tree, then to work out the differences iterate over the the original tree in preorder, find the same node in the new tree and record a change if the parents are different? is that a robust approach that won't get stuck in a cycle?
Edit actually no, that won't work. I need to iterate over the NEW tree in preorder and find the corresponding node in the old tree, then diff parent ids etc..