Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript:四叉树比较_Javascript_Search_Tree_Difference_Quadtree - Fatal编程技术网

JavaScript:四叉树比较

JavaScript:四叉树比较,javascript,search,tree,difference,quadtree,Javascript,Search,Tree,Difference,Quadtree,我没有找到任何快速算法来获得以下格式的四叉树差异。 假设我们有两个任意的4级树: 因此,我需要找出差异并将其返回为: 如果可以看到,结果必须是与更新相对应的地图或字典。 我不知道如何将[“1.1.3.1”、“1.1.3.2”、“1.1.3.3”、“1.1.3.4”]引用到单个索引,因此我将它们拆分,但是如果有更优雅的方式,欢迎使用 此数据是手动创建的,如果有错误,请道歉。您可以在树中添加标记,用于添加/删除树的一部分,并从中获得差异 函数getDifference(t1、t2){ 函数ge

我没有找到任何快速算法来获得以下格式的四叉树差异。 假设我们有两个任意的4级树:

因此,我需要找出差异并将其返回为:

如果可以看到,结果必须是与更新相对应的地图或字典。 我不知道如何将[“1.1.3.1”、“1.1.3.2”、“1.1.3.3”、“1.1.3.4”]引用到单个索引,因此我将它们拆分,但是如果有更优雅的方式,欢迎使用


此数据是手动创建的,如果有错误,请道歉。

您可以在树中添加标记
,用于添加/删除树的一部分,并从中获得差异

函数getDifference(t1、t2){ 函数getD(对象,父对象){ 函数getKeys({where,…object}){ 返回Object.keys(Object.flatMap(k=>k,…(Object[k]?getKeys(Object[k]):[])); } var类型; 对象 .条目(对象) .forEach(([k,{其中,…o}])=>{ 如果(在哪里){ 设keys=getKeys(o); 如果(!types)type={}; 如果(!types[where])types[where]=[]; 类型[where].push(keys.length?[k,keys]:k); }否则{ getD(o,k); } }); 如果(类型){ if(-1在类型中)result.push([types[-1],parent]); if(类型中的1个)result.push([parent,types[1]]); } } var temp={}, add=(inc,tree)=>({id,children})=>{ 树[id]=树[id]|{其中:0}; 树[id]。其中+=inc; if(children)children.forEach(add(inc,tree[id]); }, 结果=[]; t1.forEach(添加(-1,temp)); t2.forEach(添加(1,临时)); getD(温度); 返回结果; } var tree1=[{id:“1.1”,子项:[{id:“1.1.1”,子项:null},{id:“1.1.2”,子项:null},{id:“1.1.3.3”,子项:[{id:“1.1.3.1”,子项:null},{id:“1.1.3.3”,子项:null},{id:“1.1.3.4”,子项:null},{id:“1.1.4”,子项:null},子项:null},{id:“1.3.1,{,children:[{id:“1.3.1”,children:[{id:“1.3.1.1”,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 id:“1.3.4”,children:null},{id:“1.4”,children, tree2=[{id:“1.1”,子项:[{id:“1.1.1”,子项:空},{id:“1.1.2”,子项:空},{id:“1.1.3”,子项:空},{id:“1.1.4.1”,子项:空},{id:“1.1.4.2”,子项:空},{id:“1.1.4.3”,子项:空},{id:“1.1.4.4.4”,子项:空},{id:“1.2,{id:},{1.1:{,children:null},{id:“1.2.2”,children:null},{id:“1.2.3”,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.4”,children id:“1.3”,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.4”,children, 结果=获取差异(tree1,tree2); console.log(结果);

.as控制台包装{max height:100%!important;top:0;}
暂时不知道,因为我有1D节点数组,正在尝试找出[如何不费吹灰之力地将它们转换为2D树。我不知道这个新问题。但是这个答案是什么?我已经将1D数组转换为我这里的数组,但是,我非常确信你的代码会完美地工作。嗯,你能调整这个函数来获得对象,而不仅仅是它们的索引吗?“1.1.3”到{id:“1.1.3”,children:null}是否仅获取具有更改的节点?嵌套零件包括在内。。。
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"]

 };