這篇文章主要講解了“javascript怎么創(chuàng)建一個(gè)多叉樹”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“javascript怎么創(chuàng)建一個(gè)多叉樹”吧!
創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括牙克石網(wǎng)站建設(shè)、牙克石網(wǎng)站制作、牙克石網(wǎng)頁制作以及牙克石網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,牙克石網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到牙克石省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
1、創(chuàng)造一個(gè)節(jié)點(diǎn)
數(shù)據(jù)是以節(jié)點(diǎn)的形式存儲的:
class Node { constructor(data) { this.data = data; this.parent = null; this.children = []; } }
2、創(chuàng)造樹
樹用來連接節(jié)點(diǎn),就像真實(shí)世界樹的主干一樣,延伸著很多分支
class MultiwayTree { constructor() { this._root = null; } }
3、添加一個(gè)節(jié)點(diǎn)
function add(data, toData, traversal) { let node = new Node(data) // 第一次添加到根節(jié)點(diǎn) // 返回值為this,便于鏈?zhǔn)教砑庸?jié)點(diǎn) if (this._root === null) { this._root = node; return this; } let parent = null, callback = function(node) { if (node.data === toData) { parent = node; return true; } }; // 根據(jù)遍歷方法查找父節(jié)點(diǎn)(遍歷方法后面會講到),然后把節(jié)點(diǎn)添加到父節(jié)點(diǎn) // 的children數(shù)組里 // 查找方法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、深度優(yōu)先遍歷
深度優(yōu)先會盡量先從子節(jié)點(diǎn)查找,子節(jié)點(diǎn)查找完再從兄弟節(jié)點(diǎn)查找,適合數(shù)據(jù)深度比較大的情況,如文件目錄
function traverseDF(callback) { let stack = [], found = false; stack.unshift(this._root); let currentNode = stack.shift(); while(!found && currentNode) { // 根據(jù)回調(diào)函數(shù)返回值決定是否在找到第一個(gè)后繼續(xù)查找 found = callback(currentNode) === true ? true : false; if (!found) { // 每次把子節(jié)點(diǎn)置于堆棧最前頭,下次查找就會先查找子節(jié)點(diǎn) stack.unshift(...currentNode.children); currentNode = stack.shift(); } } }
5、廣度優(yōu)先遍歷
廣度優(yōu)先遍歷會優(yōu)先查找兄弟節(jié)點(diǎn),一層層往下找,適合子項(xiàng)較多情況,如公司崗位級別
function traverseBF(callback) { let queue = [], found = false; queue.push(this._root); let currentNode = queue.shift(); while(!found && currentNode) { // 根據(jù)回調(diào)函數(shù)返回值決定是否在找到第一個(gè)后繼續(xù)查找 found = callback(currentNode) === true ? true : false; if (!found) { // 每次把子節(jié)點(diǎn)置于隊(duì)列最后,下次查找就會先查找兄弟節(jié)點(diǎn) queue.push(...currentNode.children) currentNode = queue.shift(); } } }
6、包含節(jié)點(diǎn)
function contains(callback, traversal) { traversal.call(this, callback); }
回調(diào)函數(shù)算法可自己根據(jù)情況實(shí)現(xiàn),靈活度較高
7、移除節(jié)點(diǎn)
// 返回被移除的節(jié)點(diǎn) 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實(shí)現(xiàn):
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; } //深度優(yōu)先遍歷 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(); } } } //廣度優(yōu)先遍歷 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'); // 深度優(yōu)先查找 console.group('contains1'); tree.contains(function(node) { console.log(node.data); if (node.data === 'f') { return true; } }, tree.traverseDF); console.groupEnd('contains1') // 廣度優(yōu)先查找 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);
運(yùn)行效果如下:
感謝各位的閱讀,以上就是“javascript怎么創(chuàng)建一個(gè)多叉樹”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對javascript怎么創(chuàng)建一個(gè)多叉樹這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!