3
0

tree.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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(){
  10. super();
  11. this.root = null;
  12. this.levels = [];
  13. }
  14. /*addLink(link) {
  15. console.log("You cannot add Link because its deprecated ...Use Insert");
  16. return;
  17. }
  18. addNode(node){
  19. console.log("You cannot add Node because its deprecated ...Use Insert");
  20. return;
  21. }*/
  22. changeParent(node, parent) {
  23. let link = this.links[node.id].in.values().next().value;
  24. let oldparent = link.from;
  25. this.links[oldparent.id].out.delete(link);
  26. link.from = parent;
  27. this.links[parent.id].out.add(link);
  28. this.updateLevels(parent);
  29. }
  30. updateLevels(node) {
  31. let outSet = this.links[node.id].out;
  32. for(let link of outSet) {
  33. let child = this.getNode(link.to);
  34. if(child.depth !== node.depth + 1) {
  35. if(child.depth){
  36. this.levels[child.depth].delete(child.id);
  37. }
  38. child.depth = node.depth + 1;
  39. if(!this.levels[child.depth]){
  40. this.levels[child.depth] = new Set();
  41. }
  42. this.levels[child.depth].add(child.id);
  43. this.updateLevels(child);
  44. }
  45. }
  46. }
  47. insert(node,parentNode){
  48. if(this.nodes[node.id]){
  49. throw new Error("Node already Exists")
  50. }
  51. if(parentNode === null || parentNode === undefined) {
  52. super.addNode(node);
  53. this.root = node;
  54. this.root.depth = 0;
  55. this.levels = [new Set([this.root.id])];
  56. return;
  57. }
  58. if(!this.nodes[parentNode.id]){
  59. throw new Error("Parent Node does not Exist....Insert First ")
  60. }
  61. //console.log(node, parentNode);
  62. let link = new Link(parentNode.id,node.id);
  63. super.addNode(node);
  64. super.addLink(link);
  65. this.updateLevels(parentNode);
  66. }
  67. print() {
  68. console.log("§§§§§§§ Tree Structure §§§§§§§§§§");
  69. this.levels.forEach((level, index) => {
  70. console.log("Level ", index);
  71. level.forEach( id => console.log(` ${id} `));
  72. });
  73. console.log("§§§§§§§ Tree Structure End §§§§§§§§§§");
  74. }
  75. remove(node,propagation = true){
  76. propagation ? this.cleanup(node) : null;
  77. this.levels[node.depth].delete(node.id);
  78. if(this.levels[node.depth].size === 0) {
  79. this.levels.length = node.depth;
  80. }
  81. super.removeNode(node);
  82. }
  83. cleanup(parent) {
  84. let children = this.getChildren(parent);
  85. children.forEach(child => {
  86. this.remove(child);
  87. });
  88. }
  89. getParent(node) {
  90. return [...this.links[node.id].in].map(link => link.from)[0];
  91. }
  92. getChildren(node) {
  93. return [...this.links[node.id].out].map(link => this.getNode(link.to));
  94. }
  95. getLevel(depth) {
  96. return this.levels[depth];
  97. }
  98. replaceNode(oldNode,newNode){
  99. if(!this.nodes[oldNode.id]){
  100. throw new Error("The Node you are trying to replace doesnt exist in the current Tree")
  101. }
  102. newNode.depth = oldNode.depth
  103. let OldLinks = this.getLinks(oldNode.id);
  104. let OldFromNode;
  105. let OldToNodes = [];
  106. let OldDepth = oldNode.depth;
  107. let levels = this.levels;
  108. for(let m of OldLinks.in){
  109. OldFromNode = m.from
  110. }
  111. for(let k of OldLinks.out){
  112. OldToNodes.push(k.to)
  113. }
  114. this.remove(oldNode,false);
  115. //If the tree contains only the root this code will run
  116. if(this.levels.length === 0){
  117. this.levels = [new Set([newNode.id])]
  118. super.addNode(newNode);
  119. this.root = newNode;
  120. return true;;
  121. }
  122. //THIS PROCESS
  123. this.levels[OldDepth].add(newNode.id);
  124. super.addNode(newNode);
  125. if(OldFromNode){
  126. super.linkNodes(OldFromNode,newNode.id);
  127. }
  128. OldToNodes.map((node) => {
  129. super.linkNodes(newNode.id,node)
  130. })
  131. if(oldNode === this.root){
  132. console.log("never")
  133. this.root = newNode
  134. }
  135. }
  136. export() {
  137. let data = super.export();
  138. let levels = this.levels.map(level => Array.from(level));
  139. return {
  140. ...data,
  141. levels,
  142. rootId: this.root.id
  143. };
  144. }
  145. import(data, nodeType = TreeNode) {
  146. let { levels, rootId, ...rest } = data;
  147. super.import(data, nodeType);
  148. this.root = super.getNode(rootId);
  149. this.levels = levels.map(setArray => new Set(setArray));
  150. }
  151. }
  152. //console.log("SSSSSSSSSSSSSSSS")
  153. //console.log(A)
  154. //var B = new TreeNode('B');
  155. //var C = new TreeNode('C');
  156. //var D = new TreeNode('D');
  157. //var E = new TreeNode('E');
  158. //var F = new TreeNode("F");
  159. //console.log(tree.root)
  160. //console.log(tree)
  161. // tree.insert(B,A)
  162. // tree.insert(D,B)
  163. // tree.insert(E,D)
  164. // tree.insert(F,D)
  165. // tree.print();
  166. // tree.remove(D)
  167. // tree.print();
  168. module.exports = { Tree, TreeNode };