The main challenge/consideration when doing this is determining what you consider a difference to be. This can be more tricky than you might think. The experience I had that lead me to that conclusion was around XML which has thankfully fallen into disuse, at least for new development. Finding a good XML diffing tool was tough. It might be a place to start your research, though.
Probably the main thing is to determine whether ordering of child nodes matters. For example, are the following trees the same or different?
A A
|\ |\
B C C B
Aside from that and what properties you are considering, the approach to determining differences is pretty straightforward: recursion. A node is the same if it has all the same children and they are the same.
Where things get tricky again is how you interpret those differences. For example:
A A
|\ |
B C D
|\
B C
I can interpret that at least 2 different ways. The first being that a new node D
was inserted. The other being that B
and C
were deleted, D
was added, and B
and C
were added to D
. The latter interpretation is easier, in my estimation, to code. The former will require you to come up with more rules. One last example:
A A
|\ |
B C D
|
B
Now if you want to see that as D
being inserted and C
being deleted (again, does order matter?), you need to make a lot of assumptions. Ultimately, it comes down to the fact that there are many different transformations that result in the same tree structure. To a large degree, what you should infer from a given before and after image will depend on the way they will be synchronized.