Legacy Code | class CPHierarchialTreeBuilder {
constructor() {
this.combTree = {
'super_root': null
};
}
getCombTree() {
return this.combTree['super_root'];
}
addRootNode(id, source) {
let level = 0;
let parent = null;
let rootNode = null;
let prev = null;
if (!this.combTree['super_root']) {
rootNode = {
id,
uniqueId: id,
parent,
parentUniqueId: parent,
level,
childs: [],
next: null,
source
};
this.combTree['super_root'] = rootNode;
} else {
let start = this.combTree['super_root'];
while (start) {
if (start.id === id) {
rootNode = start;
break;
} else {
prev = start;
start = start.next;
}
}
if (!rootNode) {
rootNode = {
id,
uniqueId: id,
parent,
parentUniqueId: parent,
level,
childs: [],
source,
next: null
};
prev.next = rootNode;
}
}
return rootNode;
}
addChildNode(parentNode, id, level, source) {
let childrens = parentNode.childs;
let childNode = null;
for (let currentNode of childrens) {
if (currentNode.id === id && currentNode.level === level) {
childNode = currentNode;
break;
}
}
if (!childNode) {
childNode = {
id,
uniqueId: `${id}->${parentNode.uniqueId}`,
parent: parentNode.id,
parentUniqueId: parentNode.uniqueId,
level,
childs: [],
source
};
childrens.push(childNode);
}
return childNode;
}
}
const combHierarchyBuilder = new CPHierarchialTreeBuilder();
const uniqueCombinationsList = ["G1##R1##S1", "G1##R2##S2"];
for (let combination of uniqueCombinationsList) {
let splittedCombination = combination.split('##');
let prevNode = null;
for (let [index, attrValue] of splittedCombination.entries()) {
if (index === 0) {
prevNode = combHierarchyBuilder.addRootNode(attrValue, combination);
} else {
prevNode = combHierarchyBuilder.addChildNode(prevNode, attrValue, index, combination);
}
}
}
console.log(combHierarchyBuilder.getCombTree());
| ready |
Modified Code | class CPHierarchialTreeBuilder {
constructor() {
this.nodeMap = new Map();
this.superRoot = { id: 'super_root', childs: new Map() };
this.nodeMap.set('super_root', this.superRoot);
}
getCombTree() {
return this.superRoot.childs;
}
addRootNode(id, source) {
let rootNode = this.nodeMap.get(id);
let parent = null;
if (!rootNode) {
rootNode = {
id,
uniqueId: id,
parent,
parentUniqueId: parent,
level: 0,
childs: new Map(),
source,
next: null
};
this.nodeMap.set(id, rootNode);
this.superRoot.childs.set(id, rootNode);
}
return rootNode;
}
addChildNode(parentNode, id, level, source) {
let childNode = parentNode.childs.get(id);
if (!childNode) {
childNode = {
id,
uniqueId: `${id}->${parentNode.uniqueId}`,
parent: parentNode.id,
parentUniqueId: parentNode.uniqueId,
level,
childs: new Map(),
source
};
parentNode.childs.set(id, childNode);
}
return childNode;
}
}
const combHierarchyBuilder = new CPHierarchialTreeBuilder();
const uniqueCombinationsList = ["G1##R1##S1", "G1##R2##S2"];
uniqueCombinationsList.forEach((combination) => {
const [first, ...rest] = combination.split('##');
let prevNode = combHierarchyBuilder.addRootNode(first, combination);
for (let index = 0; index < rest.length; index++) {
prevNode = combHierarchyBuilder.addChildNode(prevNode, rest[index], index + 1, combination);
}
});
console.log(combHierarchyBuilder.getCombTree());
| ready |