tree.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. const { Node, Link, Graph } = require("./graph");
  2. class TreeNode extends Node {
  3. constructor(id){
  4. super(id);
  5. this.depth = null;
  6. }
  7. }
  8. class Tree extends Graph {
  9. constructor(rootNode){
  10. super();
  11. this.root = rootNode;
  12. this.root.depth = 0;
  13. this.levels = [new Set([this.root.id])];
  14. super.addNode(this.root);
  15. }
  16. addLink(link) {
  17. console.log("You cannot add Link because its deprecated ...Use Insert");
  18. return;
  19. }
  20. addNode(node){
  21. console.log("You cannot add Node because its deprecated ...Use Insert");
  22. return;
  23. }
  24. changeParent(node, parent) {
  25. let link = this.links[node.id].in.values().next().value;
  26. let oldparent = link.from;
  27. this.links[oldparent.id].out.delete(link);
  28. link.from = parent;
  29. this.links[parent.id].out.add(link);
  30. this.updateLevels(parent);
  31. }
  32. updateLevels(node) {
  33. let outSet = this.links[node.id].out;
  34. for(let link of outSet) {
  35. let child = link.to;
  36. if(child.depth !== node.depth + 1) {
  37. this.levels[child.depth].delete(child.id);
  38. child.depth = node.depth + 1;
  39. this.levels[child.depth].add(child.id);
  40. this.updateLevels(child);
  41. }
  42. }
  43. }
  44. insert(node,parentNode){
  45. if(this.nodes[node.id]){
  46. throw new Error("Node already Exists")
  47. }
  48. if(!this.nodes[parentNode.id]){
  49. throw new Error("Parent Node does not Exist....Insert First ")
  50. }
  51. let link = new Link(parentNode,node);
  52. super.addNode(node);
  53. super.addLink(link);
  54. this.updateLevels(parentNode);
  55. }
  56. print() {
  57. console.log("§§§§§§§ Tree Structure §§§§§§§§§§");
  58. this.levels.forEach((level, index) => {
  59. console.log("Level ", index);
  60. level.forEach( id => console.log(` ${id} `));
  61. });
  62. console.log("§§§§§§§ Tree Structure End §§§§§§§§§§");
  63. }
  64. remove(node){
  65. this.cleanup(node)
  66. this.levels[node.depth].delete(node.id);
  67. if(this.levels[node.depth].size === 0)
  68. this.levels.length = node.depth;
  69. super.removeNode(node);
  70. }
  71. cleanup(parent){
  72. let children = this.getChildren(parent);
  73. // console.log(children);
  74. children.forEach(child => {
  75. // console.log(child);
  76. this.remove(child)
  77. });
  78. }
  79. getChildren(node){
  80. // console.log(node)
  81. return [...this.links[node.id].out].map(link => link.to);
  82. }
  83. getLevel(depth) {
  84. return this.levels[depth];
  85. }
  86. }
  87. // var A = new TreeNode("A");
  88. // let tree = new Tree(TreeNode,A);
  89. // var B = new TreeNode('B');
  90. // var C = new TreeNode('C');
  91. // var D = new TreeNode('D');
  92. // var E = new TreeNode('E');
  93. // var F = new TreeNode("F");
  94. // tree.insert(B,A)
  95. // tree.insert(C,A)
  96. // tree.insert(D,B)
  97. // tree.insert(E,D)
  98. // tree.insert(F,D)
  99. // tree.print();
  100. // tree.remove(D)
  101. // tree.print();
  102. module.exports = { Tree, TreeNode };