javascript多叉树经典操作的方法
发表于:2025-02-05 作者:千家信息网编辑
千家信息网最后更新 2025年02月05日,本篇内容主要讲解"javascript多叉树经典操作的方法",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"javascript多叉树经典操作的方法"吧!多叉
千家信息网最后更新 2025年02月05日javascript多叉树经典操作的方法
本篇内容主要讲解"javascript多叉树经典操作的方法",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"javascript多叉树经典操作的方法"吧!
多叉树可以实现复杂的数据结构的存储,通过遍历方法可以方便高效的查找数据,提高查找的效率,同时方便管理节点数据。javascript的DOM其实就是以多叉树的形式存储的。下面用javascript来实现多叉树的数据结构
1、创造一个节点
数据是以节点的形式存储的:
class Node { constructor(data) { this.data = data; this.parent = null; this.children = []; }}
2、创造树
树用来连接节点,就像真实世界树的主干一样,延伸着很多分支
class MultiwayTree { constructor() { this._root = null; }}
3、添加一个节点
function add(data, toData, traversal) { let node = new Node(data) // 第一次添加到根节点 // 返回值为this,便于链式添加节点 if (this._root === null) { this._root = node; return this; } let parent = null, callback = function(node) { if (node.data === toData) { parent = node; return true; } }; // 根据遍历方法查找父节点(遍历方法后面会讲到),然后把节点添加到父节点 // 的children数组里 // 查找方法contains后面会讲到 this.contains(callback, traversal); if (parent) { parent.children.push(node); node.parent = parent; return this; } else { throw new Error('Cannot add node to a non-existent parent.'); }}
4、深度优先遍历
深度优先会尽量先从子节点查找,子节点查找完再从兄弟节点查找,适合数据深度比较大的情况,如文件目录
function traverseDF(callback) { let stack = [], found = false; stack.unshift(this._root); let currentNode = stack.shift(); while(!found && currentNode) { // 根据回调函数返回值决定是否在找到第一个后继续查找 found = callback(currentNode) === true ? true : false; if (!found) { // 每次把子节点置于堆栈最前头,下次查找就会先查找子节点 stack.unshift(...currentNode.children); currentNode = stack.shift(); } }}
5、广度优先遍历
广度优先遍历会优先查找兄弟节点,一层层往下找,适合子项较多情况,如公司岗位级别
function traverseBF(callback) { let queue = [], found = false; queue.push(this._root); let currentNode = queue.shift(); while(!found && currentNode) { // 根据回调函数返回值决定是否在找到第一个后继续查找 found = callback(currentNode) === true ? true : false; if (!found) { // 每次把子节点置于队列最后,下次查找就会先查找兄弟节点 queue.push(...currentNode.children) currentNode = queue.shift(); } }}
6、包含节点
function contains(callback, traversal) { traversal.call(this, callback);}
回调函数算法可自己根据情况实现,灵活度较高
7、移除节点
// 返回被移除的节点function remove(data, fromData, traversal) { let parent = null, childToRemove = null, callback = function(node) { if (node.data === fromData) { parent = node; return true; } }; this.contains(callback, traversal); if (parent) { let index = this._findIndex(parent.children, data); if (index < 0) { throw new Error('Node to remove does not exist.'); } else { childToRemove = parent.children.splice(index, 1); } } else { throw new Error('Parent does not exist.'); } return childToRemove;}
_findIndex实现:
function _findIndex(arr, data) { let index = -1; for (let i = 0, len = arr.length; i < len; i++) { if (arr[i].data === data) { index = i; break; } } return index;}
完整算法
class Node { constructor(data) { this.data = data; this.parent = null; this.children = []; }}class MultiwayTree { constructor() { this._root = null; } //深度优先遍历 traverseDF(callback) { let stack = [], found = false; stack.unshift(this._root); let currentNode = stack.shift(); while(!found && currentNode) { found = callback(currentNode) === true ? true : false; if (!found) { stack.unshift(...currentNode.children); currentNode = stack.shift(); } } } //广度优先遍历 traverseBF(callback) { let queue = [], found = false; queue.push(this._root); let currentNode = queue.shift(); while(!found && currentNode) { found = callback(currentNode) === true ? true : false; if (!found) { queue.push(...currentNode.children) currentNode = queue.shift(); } } } contains(callback, traversal) { traversal.call(this, callback); } add(data, toData, traversal) { let node = new Node(data) if (this._root === null) { this._root = node; return this; } let parent = null, callback = function(node) { if (node.data === toData) { parent = node; return true; } }; this.contains(callback, traversal); if (parent) { parent.children.push(node); node.parent = parent; return this; } else { throw new Error('Cannot add node to a non-existent parent.'); } } remove(data, fromData, traversal) { let parent = null, childToRemove = null, callback = function(node) { if (node.data === fromData) { parent = node; return true; } }; this.contains(callback, traversal); if (parent) { let index = this._findIndex(parent.children, data); if (index < 0) { throw new Error('Node to remove does not exist.'); } else { childToRemove = parent.children.splice(index, 1); } } else { throw new Error('Parent does not exist.'); } return childToRemove; } _findIndex(arr, data) { let index = -1; for (let i = 0, len = arr.length; i < len; i++) { if (arr[i].data === data) { index = i; break; } } return index; }}
控制台测试代码
var tree = new MultiwayTree();tree.add('a') .add('b', 'a', tree.traverseBF) .add('c', 'a', tree.traverseBF) .add('d', 'a', tree.traverseBF) .add('e', 'b', tree.traverseBF) .add('f', 'b', tree.traverseBF) .add('g', 'c', tree.traverseBF) .add('h', 'c', tree.traverseBF) .add('i', 'd', tree.traverseBF);console.group('traverseDF');tree.traverseDF(function(node) { console.log(node.data);});console.groupEnd('traverseDF');console.group('traverseBF');tree.traverseBF(function(node) { console.log(node.data);});console.groupEnd('traverseBF');// 深度优先查找console.group('contains1');tree.contains(function(node) { console.log(node.data); if (node.data === 'f') { return true; }}, tree.traverseDF);console.groupEnd('contains1')// 广度优先查找console.group('contains2');tree.contains(function(node) { console.log(node.data); if (node.data === 'f') { return true; }}, tree.traverseBF);console.groupEnd('contains2');tree.remove('g', 'c', tree.traverseBF);
这里使用在线HTML/CSS/JavaScript代码运行工具:http://tools.jb51.net/code/HtmlJsRun测试运行效果如下:
到此,相信大家对"javascript多叉树经典操作的方法"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
节点
方法
数据
深度
广度
经典
兄弟
函数
情况
存储
代码
内容
形式
把子
数据结构
算法
结构
学习
测试
运行
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
服务器不能登录拒绝访问
深圳社交直播软件开发
网络安全知识主题黑板报
软件开发工作室赚钱思路
阿里云服务器是无形资产吗
知识竞赛-网络安全达人
服务器增加硬盘风险
信息基础设施网络安全总结
网络技术1.22ppt
数据库的数字化转型的方向
根据excel表建数据库表
网络技术工程师一个多少钱
数据库技术服务公司排行
廊坊塞思网络技术如何
崇明区智能网络技术配件
长丰网络技术开发优点
松江区推荐的网络技术管理模式
内蒙古志愿填报数据库
AAA网络安全管理
一次取多少条数据数据库会卡死
国家网络安全app排行榜
服务器风扇管理软件
怎么学习网络安全渗透
网络安全手抄报图片加文字
adodc控制数据库
深圳永图时代网络技术
地形图鉴数据库的技术总结
spss如何去除数据库
重庆专业软件开发价位
AAA网络安全管理