Home > Software engineering >  Traversing a javascript tree array of objects to compare it with another tree array of objects?
Traversing a javascript tree array of objects to compare it with another tree array of objects?

Time:08-10

I have two trees of object type stated as follows:

export class asmTreeNode {
    uuid: uuidv4;
    name: string;
    instanceName: string;
    guid: string;
    componentId: number;
    type: string;
    transform: any = [];
    data: any;
    children: asmTreeNode[];
}

So, what I am supposed to do is compare if tree A (of object type asmTreeNode[]) with tree B (of object type asmTreeNode[]). And if a node is identical in tree B (the node is identical if the uuid matches) to tree A then it's properties are copied to the node in tree A. I have been told to start from the root node to do this and then move to the children (if parent nodes don't match then not to check the children). This is what I did so far but it's not giving the correct results I feel like there's something widely off with my logic.

 public compareAndCopyNodeProperties() {
        A: asmTreeNode [] = this.getExistingTree()
        B: asmTreeNode[] = this.getNewTree()

        var q1 = [];//queue to store the nodes of A that are to be checked
        var q2 = [];//queue to store the nodes of B that are to be checked
        var l = [];// List of identical nodes of A after it's property data has been upgraded from B

        A.forEach(e => {
            for (let i = 0; i < B.length; i  ) {
                q1.push(e);
                q2.push(B[i])
                if (q1[i].uuid != q2[i].uuid) {
                    q1.pop();
                    q2.pop();
                    return;
                }

                else {
                    q1[i].data = q2[i].data;
                    l.push(q1);
                    q1.pop();
                    q2.pop();
                }
                e.children;
                B[i].children;
            }
        })

    }

Any input on how to correctly do this and procced ahead would be appreciated.:)

CodePudding user response:

We will use a recursion for sure. The question is whether to compare children of same level, or of the entire tree. Anyway this is pretty basic example but maybe it will help.

var A = {
  uuid: 1,
  name: 'one',
  children: [{
      uuid: 2,
      name: 'two'
    },
    {
      uuid: 3,
      name: 'three',
      children: [{
        uuid: 4,
        name: 'four'
      }]
    }
  ]
}

var B = {
  uuid: 1,
  name: 'five',
  children: [{
      uuid: 2,
      name: 'same'
    },
    {
      uuid: 6,
      name: 'differ',
      children: [{
        uuid: 4,
        name: 'same again'
      }]
    }
  ]
}

function copy_matched_uuid_of_obj(A, B) {

  // if parent nodes don't match no need to check the children
  if (A.uuid != B.uuid) {
    return;
  }

  // copy properties, here only name
  A.name = B.name
    

  // compare for children (on same level!!!)
  if (A.children && B.children) {
    for (var i = 0; i < A.children.length; i  ) {
      for (var j = 0; j < B.children.length; j  ) {
        copy_matched_uuid_of_obj(A.children[i], B.children[j]);
      }
    }
  }
  
}

copy_matched_uuid_of_obj(A, B)
console.log(A)
.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}

  • Related