简体   繁体   English

传销二叉树更新

[英]MLM binary tree update

i have nested object in javascript where id of object are increasing depth wise.我在 javascript 中嵌套了 object ,其中 object 的 id 正在增加深度。 using javascript support i add and object inside id - 2 children array will be 3, What i need is to update(increment) id all siblings in tree, this is just a sample code, it should work for every case eg id: 4 will become 5 and all its childrens will also change changing id to 6, 7, 8 etc also we also need to change parent id as id is changing check eg below使用 javascript 支持我在 id 中添加和 object - 2 个子数组将为 3,我需要更新(递增)id 树中的所有兄弟姐妹,这只是一个示例代码,它应该适用于每种情况,例如 id: 4 将变成 5 并且它的所有孩子也将更改 id 更改为 6、7、8 等,我们还需要更改父 id,因为 id 正在更改检查,例如下面

(
[id] => 1
[parent_id] => 
[children] => Array
    (
        [0] => Array
            (
                [id] => 2
                [parent_id] => 1
                [children] => Array
                    (
                        [0] => Array
                            (
                                [id] => 3
                                [parent_id] => 2
                                [children] => Array
                                    (
                                    )

                            )

                    )

            )

        [1] => Array
            (
                [id] => 4
                [parent_id] => 1
                [children] => Array
                    (
                        [0] => Array
                            (
                                [id] => 5
                                [parent_id] => 3
                                [children] => Array
                                    (
                                    )

                            )

                        [1] => Array
                            (
                                [id] => 6
                                [parent_id] => 3
                                [children] => Array
                                    (
                                    )

                            )

                        [2] => Array
                            (
                                [id] => 7
                                [parent_id] => 4
                                [children] => Array
                                    (
                                    )

                            )

                    )

            )

        [2] => Array
            (
                [id] => 8
                [parent_id] => 1
                [children] => Array
                    (
                    )

            )

    )

) )

This simple recursive function (assuming that nodes without parents have a parentId of undefined or null ) should mutate the tree and increment the id and parent id of a node and all its children:这个简单的递归 function (假设没有父节点的节点的 parentId 为undefinednull )应该改变树并增加节点及其所有子节点的 id 和父 id:

const updateIds = (tree) => {
  tree.id++;
  if (Number.isInteger(tree.parentId)) {
    tree.parentId++;
  }
  tree.children.forEach(child => {
    updateIds(child);
  })
}

This is the id-incrementing function:这是 id 递增的 function:

const updateIds = (tree, insertedId) => {
  if (tree.id > insertedId) {
     tree.id++;
  }
  if (Number.isInteger(tree.parentId) && tree.parentId > insertedId) {
    tree.parentId++;
  }
  tree.children.forEach(child => {
    updateIds(child, insertedId);
  })
}

This is my function blow -这是我的 function 吹 -

var insId = '';
function updateIds( tree, itemName, newTeam ) {
if ( tree.name === itemName ) {
    const childrenLength = tree.children.length;
    newTeam.id = tree.id + childrenLength + 1;
    insId = tree.id + childrenLength + 1;
    tree.children = [ ...tree.children, newTeam ];
}
if ( insId !== '' && tree.id >= insId && tree.name !== newTeam.name ) {
    tree.id++;
}
if ( insId !== '' && Number.isInteger(tree.parent_id) && tree.parent_id >= insId && tree.name !== newTeam.name) {
    tree.parent_id++;
}
tree.children.forEach(child => { 
    updateIds(child, itemName, newTeam );
});
return tree;

} }

everything works really fine until just one case senario, like adding sibling element at root so it doesnot update the id of this sibling element一切都很好,直到只有一个案例 senario,比如在根处添加兄弟元素,所以它不会更新这个兄弟元素的 id

if somebody is looking for this kind of problem that worked for me, for add item part below is solution如果有人正在寻找对我有用的此类问题,请在下面添加项目部分是解决方案

function depthItem( element ) {
let children = '';
if ( element.children.length > 0 ) {
    children = element.children[ element.children.length - 1 ];
    return depthItem( children );
}
return element;}

function addNestedItem( array, itemName, newTeam ) {
array.forEach( ( element ) => {
    if ( element.name === itemName ) {
        const lastItem = depthItem( element );
        newTeam.id = lastItem.id + 1;
        element.children = [ ...element.children, newTeam ];
    } else {
        return addNestedItem( element.children, itemName, newTeam );
    }
} );
return array;}
function updgradeNestedItemIndex( array, newTeam ) {
array.forEach( ( element ) => {
    if ( element.id >= newTeam.id && element.name !== newTeam.name ) {
        element.id++;
    }
    return updgradeNestedItemIndex( element.children, newTeam );
} );
return array;}

const tempTree = addNestedItem( [ tree ], team.name, newTeam );
const newTree = updgradeNestedItemIndex( tempTree, newTeam );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM