简体   繁体   中英

MLM binary tree update

i have nested object in javascript where id of object are increasing depth wise. 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

(
[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:

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

This is the id-incrementing 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 -

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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