js--数据结构--树

来源:互联网 发布:黑客帝国 矩阵 编辑:程序博客网 时间:2024/05/14 02:42

1.树的构造函数

1.1 Node

属性:

data存储的值
parent指向节点的父节点
children指向许多孩子节点

Node构造函数:

function Node(data) {    this.data = data;    this.parent = null;    this.children = [];}
1.2Tree

属性和方法:

root:指向一棵树的根节点
deepTraversal(callback,node):遍历与DFS树的节点
widthTraversal(callback,node):遍历与BFS树的节点
contasins(data,traversal)搜索树中的节点
add(data,toData,traverse)将节点添加到一棵树
remove(child,parent)在一棵树中删除一个节点

Tree构造函数:

先创建一个新实例的Node,随后指派node作为树的根

function Tree (data) {    var node = new Node(data);    this._root = node;}
1.3创建tree
var tree = new Tree('CEO');tree._root;//{data:'CEO',parent:null,children:[]}

2.一个树的方法

2.1 traverseDF(callback)遍历与DFS树的节点
//深度遍历Tree.prototype.deepTraversal =  function(callback,node){    var nodes = [];    if (node != null) {        var stack = [];        stack.push(node);        while (stack.length != 0) {            var item = stack.pop();            callback(item);            nodes.push(item);            var children = item.children;            for (var i = children.length - 1; i >= 0; i--)                stack.push(children[i]);        }    }    return nodes;};

举例

var tree = new Tree('one');tree._root.children.push(new Node('two'));tree._root.children[0].parent = tree;tree._root.children.push(new Node('three'));tree._root.children[1].parent = tree;tree._root.children.push(new Node('four'));tree._root.children[2].parent = tree;tree._root.children[0].children.push(new Node('five'));tree._root.children[0].children[0].parent = tree._root.children[0];tree._root.children[0].children.push(new Node('six'));tree._root.children[0].children[1].parent = tree._root.children[0];tree._root.children[2].children.push(newNode('seven'));tree._root.children[2].children[0].parent = tree._root.children[2];/*creates this tree one ├── two │   ├── five │   └── six ├── three └── four     └── seven*/tree.deepTraversal(function(node) {    console.log(node.data)},tree._root);/*logs the following strings to the console'five''six''two''three''seven''four''one'*/
2.2 traverseBF(callback)遍历与BFS树的节点
//广度搜索Tree.prototype.wideTraversal = function(callback,node) {    var nodes = [];    if (node != null) {        var queue = [];        queue.unshift(node);        while (queue.length != 0) {            var item = queue.shift();            callback(item);            nodes.push(item);            var children = item.children;            for (var i = 0; i < children.length; i++)                queue.push(children[i]);        }    }    return nodes;}
2.3 contains(data,traversal)搜索树中的节点
//接受两个参数 ︰ 搜索和遍历类型的数据。Tree.prototype.contains = function(callback, traversal) {    traversal.call(this, callback);};

想象一下我们想要登录到控制台包含的数据具有奇数和遍历我们与 BFS 树中的每个节点的任何节点。这是我们要编写的代码 ︰

// tree is an example of a root nodetree.contains(function(node){    if(node.data ===  'two') {        console.log(node);    }}, tree.traverseBF);
2.4 add(child,parent)将节点添加到一棵树

第一个参数,data,用于创建新实例的Node。
第二个参数,toData,用来比较每一棵树中的节点。
第三个参数,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 {        thrownewError('Cannot add node to a non-existent parent.');    }};

实例:

vartree = newTree('CEO');tree.add( 'CEO''VP of Happiness', , tree.traverseBF);/*our tree'CEO'└── 'VP of Happiness'*/

实例:

vartree = newTree('CEO');tree.add( 'CEO''VP of Happiness', , tree.traverseBF);tree.add( 'CEO''VP of Finance', , tree.traverseBF);tree.add('VP of Sadness',  'CEO', tree.traverseBF);tree.add( 'VP of Finance''Director of Puppies', , tree.traverseBF);tree.add( 'Director of Puppies''Manager of Puppies', , tree.traverseBF);/* tree 'CEO' ├── 'VP of Happiness' ├── 'VP of Finance' │   ├── 'Director of Puppies' │   └── 'Manager of Puppies' └── 'VP of Sadness' */
2.5 remove(node,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;        }    };    this.contains(callback, traversal);    if(parent) {        index = findIndex(parent.children, data);        if(index === undefined) {            thrownewError('Node to remove does not exist.');        } else {            childToRemove = parent.children.splice(index, 1);        }    } else {        thrownewError('Parent does not exist.');    }    return childToRemove;};

3.一棵树深度和广度的遍历

function Node(data) {    this.data = data;    this.parent = null;    this.children = [];}function Tree(data) {    var node = new Node(data);    this._root = node;}//深度搜索Tree.prototype.deepTraversal =  function(callback,node){    var nodes = [];    if (node != null) {        var stack = [];        stack.push(node);        while (stack.length != 0) {            var item = stack.pop();            callback(item);            nodes.push(item);            var children = item.children;            for (var i = children.length - 1; i >= 0; i--)                stack.push(children[i]);        }    }    return nodes;};//广度搜索Tree.prototype.wideTraversal = function(callback,node) {    var nodes = [];    if (node != null) {        var queue = [];        queue.unshift(node);        while (queue.length != 0) {            var item = queue.shift();            callback(item);            nodes.push(item);            var children = item.children;            for (var i = 0; i < children.length; i++)                queue.push(children[i]);        }    }    return nodes;}var tree = new Tree('one');tree._root.children.push(new Node('two'));tree._root.children[0].parent = tree;tree._root.children.push(new Node('three'));tree._root.children[1].parent = tree;tree._root.children.push(new Node('four'));tree._root.children[2].parent = tree;tree._root.children[0].children.push(new Node('five'));tree._root.children[0].children[0].parent = tree._root.children[0];tree._root.children[0].children.push(new Node('six'));tree._root.children[0].children[1].parent = tree._root.children[0];tree._root.children[2].children.push(new Node('seven'));tree._root.children[2].children[0].parent = tree._root.chilvar arr = tree.deepTraversal(function(node) {    console.log(node.data)},tree._root);console.log(arr);var arr2 = tree.wideTraversal(function(node) {    console.log(node.data)},tree._root);console.log(arr2);
0 0
原创粉丝点击