我的图形结构的最后一步 - forEachNode回调

时间:2016-06-06 23:29:28

标签: javascript data-structures graph

我是图形结构的最后一站。所以我应该为每个节点添加回调。 两件事情, 1.当我调用a.forEachNode(hasEdge()) - > hasEdge不是一个功能。 你认为我的代码会起作用吗? 请解释我是做对还是错。感谢

 var Graph = function(){
       this.nodes = [];
       this.edges = {};
    };

    Graph.prototype.addNode = function(node){
      this.nodes.push(node);
      this.edges[node] = {};
    };

    Graph.prototype.contains = function(node){
      return this.nodes.indexOf(node) !== -1;
    };

    Graph.prototype.removeNode = function(node){
       for(var key in this.edges){
        if(key === node){
          delete this.edges[node];
        }
      }

        for(var i = 0; i < this.nodes.length; i++){
          if(this.nodes[i] === node){
            this.nodes.splice(i,1);
          }
        }
    };

    Graph.prototype.hasEdge = function(fromNode, toNode){
      for(var key in this.edges){
        if(this.edges[fromNode][toNode]){
          return true;
        } else {
          return false;
        }
      }
    };

    Graph.prototype.addEdge = function(fromNode, toNode){
      this.edges[fromNode][toNode] = true;
      this.edges[toNode][fromNode] =  true;
    };

    Graph.prototype.removeEdge = function(fromNode, toNode){
      delete this.edges[fromNode][toNode];
      delete this.edges[toNode][fromNode];
    };

    Graph.prototype.forEachNode = function(cb){
      //loop through edges object
      for(var key in this.edges){
      //if keys exist
        if(keys){
        //call back on each nodes
           return cb(this.edges[key]);
        }
      }
    };





    var a = new Graph();
    a.addNode("puppies");
    a.addNode("kittens");
    a.addNode("bears");
    a.addEdge("puppies", "kittens");
    a.addEdge("puppies", "bears");
    a.hasEdge("puppies", "bears");
    a.hasEdge("kittens", "bears");
    a.forEachNode(addEdge());

1 个答案:

答案 0 :(得分:0)

您的代码看起来很不错,除了最后一部分,您尝试传递addEdge()hasEndge()

hasEdge() - 未定义,因为它是Graph实例的方法。所以应该有像

这样的东西
var g = new Graph();
a.forEachNode(function(node){
  g.addNode(node)
});

它应该将图a中的每个节点添加到新图g

===

<强> UPD 恕我直言,最好使用更简单的结构:

Graph {
  nodes: {
    node1: [node2, node3...] //achievable nodes for node1
    node2: [node5..]
  }
}

所以,我认为在这种情况下代码会更简单

function Graph(){
   this.nodes = {};
};

Graph.prototype.addNode = function(nodeName){
  this.nodes[nodeName] = [];
  return this; //for using method chaining graph.addNode(a).addNode(b).addNode(c)...
};

Graph.prototype.contains = function(node){
  return this.nodes[node] !== undefined;
};

Graph.prototype.removeNode = function(node){
  delete this.nodes[node];
  return this;
};

Graph.prototype.hasEdge = function(fromNode, toNode){
  //return this.nodes[fromNode].indexOf(toNode) !== -1;
  return this.nodes[fromNode].includes(toNode); //better
};

Graph.prototype.addEdge = function(fromNode, toNode){
  //add node "toNode" if it does not exist
  if(this.nodes[toNode] === undefined){ 
    this.addNode(toNode);
  }
  this.nodes[fromNode].push(toNode);
  return this;
};

Graph.prototype.removeEdge = function(fromNode, toNode){
  var index = this.nodes[fromNode].indexOf(toNode);
  if(index !== -1){
    this.nodes[fromNode].splice(index,1);
  }
  return this;
};

Graph.prototype.forEachNode = function(cb){
  //loop through edges object
  for(var key in this.nodes){
    cb(key, this.nodes[key])
  }
};


var a = new Graph();
a.addNode("puppies");
a.addNode("kittens");
a.addNode("bears");
a.addEdge("puppies", "kittens");
a.addEdge("puppies", "bears");
a.hasEdge("puppies", "bears");
a.hasEdge("kittens", "bears");

var g = new Graph();

a.forEachNode(function(nodeName, edges){
  g.addNode(nodeName);

  edges.forEach(function(edge){
    g.addEdge(nodeName, edge);
  });

});