JSFiddle - React, Tailwind, and code Playground
by Daedalus
JavaScript
class Node {
constructor(id) {
this.id = id;
this.children=[];
this.parents=[];
this.requirements=[];
}
getID() {
return this.id;
}
getChildren() {
return this.children;
}
getParents() {
return this.parents;
}
addChild(node) {
this.children.push(node);
}
addParent(node) {
this.parents.push(node);
}
removeChild(node) {
this.children.pop(node);
}
removeParent(node) {
this.parent.pop(node);
}
hasChildren(node) {
return this.children.length > 0;
}
addRequirement(req){
this.requirements.push(req);
}
getRequirements(){
return this.requirements
}
}
class AbstractNodeTree {
constructor() {
this.nodes=[];
}
isValid(node) {
var requirements = node.getRequirements();
for(var i=0, len = requirements.length; i<len; i++) {
if(!this.contains(requirements[i])) return false;
}
return true;
}
contains(id) {
for(var i=0, len = this.nodes.length; i<len; i++) {
if(this.nodes[i].getID() == id) return true;
}
return false;
}
addNode(node){
if(this.isValid(node)){
this.nodes.push(node);
}
}
deleteNode(node){
var nodechildren = node.getChildren();
var nodeparents = node.getParents();
console.log("Deleting node " + node.getID());
for(var i=0, len = nodechildren.length; i<len; i++) {
var child = nodechildren[i];
console.log("Deleting child " + child.getID());
this.deleteNode(child);
}
for(var i=0, len = nodeparents.length; i<len; i++) {
var parent = nodeparents[i];
parent.removeChild(node);
}
this.nodes.pop(node)
}
print() {
console.log(this.nodes.toString());
}
}
console.log("Cascade deletion test");
let list = new AbstractNodeTree();
let node_a = new Node("One");
let node_b = new Node("Two");
let node_c = new Node("Three");
//Setting up our...