Skip to content

Instantly share code, notes, and snippets.

@CreeJee
Created October 10, 2017 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CreeJee/22bcf1567113c29b7b0c55db4f228903 to your computer and use it in GitHub Desktop.
Save CreeJee/22bcf1567113c29b7b0c55db4f228903 to your computer and use it in GitHub Desktop.
var a = (function(){
var Tree = function(obj){
if (obj instanceof Object) {
//public variable
this.root = obj.root || this;
this.name = obj.name || "";
this.childs = obj.childs || [];
this.left = obj.left || null;
this.right = obj.right || null;
this.parent = obj.parent || null;
this.siblings = obj.siblings || [];
}
else{
throw new TypeError("argument is not Object \n needle : [Object]")
}
},
fn = Tree.prototype;
fn.add = function(childName){
var childObj = {},
childLen = this.childs.length;
if (typeof childName === "string") {
childObj.root = this.root; //root
childObj.name = childName; //name
childObj.childs = []; //childs
childObj.left = (childLen > 0) ? this.childs[childLen - 1] : null; //left
childObj.right = null; //right
childObj.parent = this; //parent
childObj.siblings = this.childs; //siblings
childObj = new Tree(childObj);
this.childs.push(childObj); //parent Childs
if (childLen > 0) {
this.childs[childLen].right = childObj; //right(left's right)
}
}
return this;
};
/**
* @param {Object} arg
* @description {
* name : node name,
* area : findZone,
* find : onTree Find event
* }
*/
fn.call = function(arg){
var nextNode = {},area = {};
if (arg instanceof Object) {
if (arg.name instanceof String) {
arg.area = (!arg.area instanceof Tree) ? this : arg.area;
area = arg.area;
nextNode = area.next();
arg.always.apply(area, [area]);
if (arg.name != area.name) {
this.call(Object.assign(arg,{area : nextNode}));
}
else if(arg.name === area.name){
if(typeof arg.find === "function"){
arg.find.apply(area, []);
}
else{
return area;
}
}
}
else{
throw new TypeError("name only String");
}
}
else{
throw new TypeError("argument only Object");
}
};
fn.next = function() {
return (this.right != null) ? this.right : this.childs[0];
};
fn.pre = function() {
return (this.left != null) ? this.left : this.parent;
};
return new Tree({});
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment