我怎么能差异比较两棵树,以确定父母的变化?差异、父母、两棵树、我怎么能

2023-09-11 05:09:10 作者:源来是你、

我有,我需要重新安排一个树状结构(拖放),然后提交更改。

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,

店铺的每一个变化命令,提交更改的列表,然后执行每一个 序列化树,然后差异与老树新树摸出什么变化,然后执行修改

1,似乎最容易实现的,但如果许多重复的动作都发生(即拖上许多次节点,但把它们放回原处,他们开始),它可能是很浪费的。

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避免了上述问题,但我怎么能差异的树木,以制定出什么父母更改命令来执行? presumably有算法呢?

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?

修改澄清,每个节点都有一个身份证和parentId的。我需要允许用户重新排列树(由此改变一些节点的parentId)。

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).

有关选项2,它应该是足够直截了当的系列化改变的树,然后制定出不同遍历在preorder原树,发现新树的同一个节点,并记录变化,如果家长们有什么不同?是一个强大的方法,不会停留在一个周期?

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 wont get stuck in a cycle?

修改其实也没什么,这不会工作。我需要遍历在preorder新的树,并找到在老树中相应的节点,然后差异父母IDS等。

Edit actually no, that wont 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..

感谢

推荐答案

除非树是巨大的,可能是最容易简单地删除旧的树,然后添加新的树。

Unless the trees are huge it might be easiest simply to delete the old tree and then add the new tree.

差分两棵树时,所允许的操作包括移动,删除,添加将是更加复杂,所以,除非有需要获得它的可能是最好的避免这种情况的复杂性和使用简单的删除,然后,添加选项显著的好处

Differencing two trees when the allowed operations include moves, deletes, adds is going to be much more complex so unless there is a significant benefit to be gained it's probably best to avoid that complexity and use a simple remove-then-add option.