您的位置:首页 > Web前端 > JavaScript

js树

2017-01-31 00:28 316 查看
/**
* @Author    Toney
* @Explain   [description]
* @DateTime  2017-01-22
* @copyright [datacvg]
* @param     {[type]}      data [description]
*/
function Node(data) {
this.data = data;
this.children = [];
}
/**
* @Author    Toney
* @Explain   [description]
* @DateTime  2017-01-22
* @copyright [datacvg]
* @param     {[type]}      data [description]
*/
function Tree(data){
var node = new Node(data);
this._root = node;
}
/**
* @Author    Toney
* @Explain   [深度优先遍历]
* @DateTime  2017-01-22
* @copyright [datacvg]
* @param     {Function}    callback [description]
* @return    {[type]}               [description]
*/
Tree.prototype.traverseDF = function (callback) {
(function recurse (currentNode) {
// 迭代每一个子
for(var i = 0,len = currentNode.children.length;i < len;i++){
recurse(currentNode.children[i]);
}
callback(currentNode);
})(this._root);
}
/**
* @Author    Toney
* @Explain   [广度优先遍历]
* @DateTime  2017-01-22
* @copyright [datacvg]
* @param     {Function}    callback [description]
* @return    {[type]}               [description]
*/
Tree.prototype.traverseBF = function(callback){
var queue = [];

queue.push(this._root);

var currentTree = queue.shift();

while(currentTree){
for(var i = 0,len = currentTree.children.length; i < len; i++){
queue.push(currentTree.children[i]);
}
callback(currentTree);
currentTree = queue.shift();
}
}
/**
* @Author    Toney
* @Explain   [搜索包含节点]
* @DateTime  2017-01-22
* @copyright [datacvg]
* @param     {Function}    callback  [description]
* @param     {[type]}      traversal [description]
* @return    {[type]}                [description]
*/
Tree.prototype.contains = function(callback,traversal){
traversal.call(this,callback);
}
/**
* @Author    Toney
* @Explain   [添加一个节点到树]
* @DateTime  2017-01-22
* @copyright [datacvg]
* @param     {[string]}      data      [新添加节点]
* @param     {[string]}      toData    [目标节点]
* @param     {[function]}      traversal [遍历树所用的方法类型]
*/
Tree.prototype.add = function(data,toData,traversal){
var child = new Node(data),
parent = null,
callback = function(node){
if(node.data === toData){
parent = node;
}
};

this.contains(callback,traversal);

if(parent){
parent.children.push(child);
child.parent == parent;
}else{
console.log('Error:Cannot add node to a non-existent parent.');
}
}
Tree.prototype.remove = function(data,fromData,traversal){

var tree = this,
parent = null,
childToRemove = null,
index;
var callback = function(node){
if(node.data === fromData){
parent = node;
}
};

var findIndex = function(arr, data){

var index;

for(var i = 0; i < arr.length; i++) {
if(arr[i].data === data) {
index = i;
}
}

return index;
};

this.contains(callback,traversal);

if(parent){
index = findIndex(parent.children,data);
if(index === undefined){
console.log('Error:Node to remove does not exist');
}else{
childToRemove = parent.children.splice(index,1);
}
}else{
console.log('Error:parent does not exist.');
}
return childToRemove;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: