tree.js 769 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const {
  2. Node,
  3. Link,
  4. Graph
  5. } = require("./graph");
  6. console.log(Graph);
  7. class Tree extends Graph {
  8. constructor(NodeType) {
  9. super(NodeType);
  10. }
  11. addLink(link) {
  12. if (this.nodes[link.to.id].in.length > 0) {
  13. throw new Error("Cannot have 2 parents... Its a tree dude!");
  14. }
  15. super.addLink(link);
  16. }
  17. }
  18. let tree = new Tree(Node);
  19. var A = new Node("A");
  20. var B = new Node('B');
  21. var C = new Node('C');
  22. var D = new Node('D');
  23. var E = new Node('E');
  24. var F = new Node("F");
  25. var AB = new Link(A, B);
  26. var AC = new Link(A, C);
  27. var CD = new Link(C, D);
  28. var DE = new Link(D, E);
  29. tree.addNode(A);
  30. tree.addNode(B);
  31. tree.addNode(C);
  32. tree.addNode(D);
  33. tree.addNode(E);
  34. tree.addNode(F);
  35. tree.addLink(AB);
  36. tree.addLink(AC);
  37. tree.addLink(CD);
  38. tree.addLink(DE);