tree.js 8.6 KB

1
  1. "use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");var _objectWithoutProperties2=_interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));var _toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));var _createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass"));var _get2=_interopRequireDefault(require("@babel/runtime/helpers/get"));var _classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));var _possibleConstructorReturn2=_interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));var _getPrototypeOf2=_interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));var _inherits2=_interopRequireDefault(require("@babel/runtime/helpers/inherits"));function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);if(enumerableOnly)symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable;});keys.push.apply(keys,symbols);}return keys;}function _objectSpread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};if(i%2){ownKeys(source,true).forEach(function(key){(0,_defineProperty2["default"])(target,key,source[key]);});}else if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source));}else{ownKeys(source).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key));});}}return target;}var _require=require("./graph"),Node=_require.Node,Link=_require.Link,Graph=_require.Graph;var TreeNode=function(_Node){(0,_inherits2["default"])(TreeNode,_Node);function TreeNode(id){var _this;(0,_classCallCheck2["default"])(this,TreeNode);_this=(0,_possibleConstructorReturn2["default"])(this,(0,_getPrototypeOf2["default"])(TreeNode).call(this,id));_this.depth=null;return _this;}return TreeNode;}(Node);var Tree=function(_Graph){(0,_inherits2["default"])(Tree,_Graph);function Tree(){var _this2;(0,_classCallCheck2["default"])(this,Tree);_this2=(0,_possibleConstructorReturn2["default"])(this,(0,_getPrototypeOf2["default"])(Tree).call(this));_this2.root=null;_this2.levels=[];return _this2;}(0,_createClass2["default"])(Tree,[{key:"changeParent",value:function changeParent(node,parent){var link=this.links[node.id]["in"].values().next().value;var oldparent=link.from;this.links[oldparent.id].out["delete"](link);link.from=parent;this.links[parent.id].out.add(link);this.updateLevels(parent);}},{key:"updateLevels",value:function updateLevels(node){var outSet=this.links[node.id].out;var _iteratorNormalCompletion=true;var _didIteratorError=false;var _iteratorError=undefined;try{for(var _iterator=outSet[typeof Symbol==="function"?Symbol.iterator:"@@iterator"](),_step;!(_iteratorNormalCompletion=(_step=_iterator.next()).done);_iteratorNormalCompletion=true){var link=_step.value;var child=this.getNode(link.to);if(child.depth!==node.depth+1){if(child.depth){this.levels[child.depth]["delete"](child.id);}child.depth=node.depth+1;if(!this.levels[child.depth]){this.levels[child.depth]=new Set();}this.levels[child.depth].add(child.id);this.updateLevels(child);}}}catch(err){_didIteratorError=true;_iteratorError=err;}finally{try{if(!_iteratorNormalCompletion&&_iterator["return"]!=null){_iterator["return"]();}}finally{if(_didIteratorError){throw _iteratorError;}}}}},{key:"insert",value:function insert(node,parentNode){if(this.nodes[node.id]){throw new Error("Node already Exists");}if(parentNode===null||parentNode===undefined){(0,_get2["default"])((0,_getPrototypeOf2["default"])(Tree.prototype),"addNode",this).call(this,node);this.root=node;this.root.depth=0;this.levels=[new Set([this.root.id])];return;}if(!this.nodes[parentNode.id]){throw new Error("Parent Node does not Exist....Insert First ");}var link=new Link(parentNode.id,node.id);(0,_get2["default"])((0,_getPrototypeOf2["default"])(Tree.prototype),"addNode",this).call(this,node);(0,_get2["default"])((0,_getPrototypeOf2["default"])(Tree.prototype),"addLink",this).call(this,link);this.updateLevels(parentNode);}},{key:"print",value:function print(){console.log("§§§§§§§ Tree Structure §§§§§§§§§§");this.levels.forEach(function(level,index){console.log("Level ",index);level.forEach(function(id){return console.log(" ".concat(id," "));});});console.log("§§§§§§§ Tree Structure End §§§§§§§§§§");}},{key:"remove",value:function remove(node){var propagation=arguments.length>1&&arguments[1]!==undefined?arguments[1]:true;propagation?this.cleanup(node):null;this.levels[node.depth]["delete"](node.id);if(this.levels[node.depth].size===0){this.levels.length=node.depth;}(0,_get2["default"])((0,_getPrototypeOf2["default"])(Tree.prototype),"removeNode",this).call(this,node);}},{key:"cleanup",value:function cleanup(parent){var _this3=this;var children=this.getChildren(parent);children.forEach(function(child){_this3.remove(child);});}},{key:"getParent",value:function getParent(node){return(0,_toConsumableArray2["default"])(this.links[node.id]["in"]).map(function(link){return link.from;})[0];}},{key:"getChildren",value:function getChildren(node){var _this4=this;return(0,_toConsumableArray2["default"])(this.links[node.id].out).map(function(link){return _this4.getNode(link.to);});}},{key:"getLevel",value:function getLevel(depth){return this.levels[depth];}},{key:"replaceNode",value:function replaceNode(oldNode,newNode){var _this5=this;if(!this.nodes[oldNode.id]){throw new Error("The Node you are trying to replace doesnt exist in the current Tree");}newNode.depth=oldNode.depth;var OldLinks=this.getLinks(oldNode.id);var OldFromNode;var OldToNodes=[];var OldDepth=oldNode.depth;var levels=this.levels;var _iteratorNormalCompletion2=true;var _didIteratorError2=false;var _iteratorError2=undefined;try{for(var _iterator2=OldLinks["in"][typeof Symbol==="function"?Symbol.iterator:"@@iterator"](),_step2;!(_iteratorNormalCompletion2=(_step2=_iterator2.next()).done);_iteratorNormalCompletion2=true){var m=_step2.value;OldFromNode=m.from;}}catch(err){_didIteratorError2=true;_iteratorError2=err;}finally{try{if(!_iteratorNormalCompletion2&&_iterator2["return"]!=null){_iterator2["return"]();}}finally{if(_didIteratorError2){throw _iteratorError2;}}}var _iteratorNormalCompletion3=true;var _didIteratorError3=false;var _iteratorError3=undefined;try{for(var _iterator3=OldLinks.out[typeof Symbol==="function"?Symbol.iterator:"@@iterator"](),_step3;!(_iteratorNormalCompletion3=(_step3=_iterator3.next()).done);_iteratorNormalCompletion3=true){var k=_step3.value;OldToNodes.push(k.to);}}catch(err){_didIteratorError3=true;_iteratorError3=err;}finally{try{if(!_iteratorNormalCompletion3&&_iterator3["return"]!=null){_iterator3["return"]();}}finally{if(_didIteratorError3){throw _iteratorError3;}}}this.remove(oldNode,false);if(this.levels.length===0){this.levels=[new Set([newNode.id])];(0,_get2["default"])((0,_getPrototypeOf2["default"])(Tree.prototype),"addNode",this).call(this,newNode);this.root=newNode;return true;;}this.levels[OldDepth].add(newNode.id);(0,_get2["default"])((0,_getPrototypeOf2["default"])(Tree.prototype),"addNode",this).call(this,newNode);if(OldFromNode){(0,_get2["default"])((0,_getPrototypeOf2["default"])(Tree.prototype),"linkNodes",this).call(this,OldFromNode,newNode.id);}OldToNodes.map(function(node){(0,_get2["default"])((0,_getPrototypeOf2["default"])(Tree.prototype),"linkNodes",_this5).call(_this5,newNode.id,node);});if(oldNode===this.root){console.log("never");this.root=newNode;}}},{key:"export",value:function _export(){var data=(0,_get2["default"])((0,_getPrototypeOf2["default"])(Tree.prototype),"export",this).call(this);var levels=this.levels.map(function(level){return Array.from(level);});return _objectSpread({},data,{levels:levels,rootId:this.root.id});}},{key:"import",value:function _import(data){var nodeType=arguments.length>1&&arguments[1]!==undefined?arguments[1]:TreeNode;var parentNode=arguments.length>2?arguments[2]:undefined;var levels=data.levels,rootId=data.rootId,rest=(0,_objectWithoutProperties2["default"])(data,["levels","rootId"]);(0,_get2["default"])((0,_getPrototypeOf2["default"])(Tree.prototype),"import",this).call(this,data,nodeType);if(!parentNode){this.root=(0,_get2["default"])((0,_getPrototypeOf2["default"])(Tree.prototype),"getNode",this).call(this,rootId);this.levels=levels.map(function(setArray){return new Set(setArray);});}else{this.linkNodes(parentNode,rootId);this.updateLevels(this.getNode(parentNode));}}}]);return Tree;}(Graph);module.exports={Tree:Tree,TreeNode:TreeNode};