【发布时间】:2020-09-23 07:44:00
【问题描述】:
我没有找到任何快速算法来获取以下格式的四叉树差异。 假设我们有两个任意的 4 级树:
var tree1 = [
{ id: "1.1", children: [
{ id: "1.1.1", children: null },
{ id: "1.1.2", children: null },
{ id: "1.1.3", children: [
{ id: "1.1.3.1", children: null },
{ id: "1.1.3.2", children: null },
{ id: "1.1.3.3", children: null },
{ id: "1.1.3.4", children: null }
] },
{ id: "1.1.4", children: null }
] },
{ id: "1.2", children: null },
{ id: "1.3", children: [
{ id: "1.3.1", children: [
{ id: "1.3.1.1", children: null },
{ id: "1.3.1.2", children: null },
{ id: "1.3.1.3", children: null },
{ id: "1.3.1.4", children: null }
] },
{ id: "1.3.2", children: null },
{ id: "1.3.3", children: null },
{ id: "1.3.4", children: null }
] },
{ id: "1.4", children: null }
];
var tree2 = [
{ id: "1.1", children: [
{ id: "1.1.1", children: null },
{ id: "1.1.2", children: null },
{ id: "1.1.3", children: null },
{ id: "1.1.4", children: [
{ id: "1.1.4.1", children: null },
{ id: "1.1.4.2", children: null },
{ id: "1.1.4.3", children: null },
{ id: "1.1.4.4", children: null }
] }
] },
{ id: "1.2", children: [
{ id: "1.2.1", children: null },
{ id: "1.2.2", children: null },
{ id: "1.2.3", children: [
{ id: "1.2.3.1", children: null },
{ id: "1.2.3.2", children: null },
{ id: "1.2.3.3", children: null },
{ id: "1.2.3.4", children: null }
] },
{ id: "1.2.1", children: null }
]},
{ id: "1.3", children: null },
{ id: "1.4", children: [
{ id: "1.4.1", children: [
{ id: "1.4.1.1", children: null },
{ id: "1.4.1.2", children: null },
{ id: "1.4.1.3", children: null },
{ id: "1.4.1.4", children: null }
] },
{ id: "1.4.2", children: null },
{ id: "1.4.3", children: null },
{ id: "1.4.1", children: null }
] }
];
所以,我需要找到差异并将它们返回为:
var result = {
"1.1.3.1" : "1.1.3",
"1.1.3.2" : "1.1.3",
"1.1.3.3" : "1.1.3",
"1.1.3.4" : "1.1.3",
"1.1.4" : ["1.1.4.1", "1.1.4.2", "1.1.4.3", "1.1.4.4"],
"1.2" : ["1.2.1", "1.2.2". ["1.2.3.1", "1.2.3.2", "1.2.3.3", "1.2.3.4"], "1.2.4"],
"1.3.1.1" : "1.3",
"1.3.1.2" : "1.3",
"1.3.1.3" : "1.3",
"1.3.1.4" : "1.3",
"1.3.2" : "1.3",
"1.3.3" : "1.3",
"1.3.4" : "1.3",
"1.4" : [["1.4.1.1", "1.4.1.2", "1.4.1.3", "1.4.1.4"], "1.4.2", "1.4.3", "1.4.4"]
};
如果可以看到,结果必须是与更新对应的地图或字典。 不知道如何将 ["1.1.3.1", "1.1.3.2", "1.1.3.3", "1.1.3.4"] 引用到单个索引,所以我将它们拆分,但是如果有更优雅的不客气。
此数据是手动创建的,如有错误请见谅。
【问题讨论】:
标签: javascript search tree difference quadtree