js实现深度优先
Posted 偏灬爱
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js实现深度优先相关的知识,希望对你有一定的参考价值。
; (function() { var def = function() { var me = this; me.data = { color_1: ‘#333333‘, color_2: ‘#1890ff‘, one: 1, arr: [{ click: false, txt: "parent-1", show: true, arr: [{ click: false, txt: "child-1", show: false, arr: [{ click: false, txt: "child-1-1", show: false }, { click: false, txt: "child-1-2", show: false }] }, { click: false, txt: "child-2", show: false }, { click: false, txt: "child-3", show: false }, { click: false, txt: "child-4", show: false }] }, { click: false, txt: "parent-2", show: true }, { click: false, txt: "parent-3", show: true }, { click: false, txt: "parent-4", show: true }] } me.key = "child-1-1" me.r = { arr: [], count:0 }; me.start(me.r, me.data.arr); console.log(me) }; def.prototype = { start: function(p, arr) { var me = this; var r; for (var i = 0; i < arr.length; i += 1) { me.r.arr.push(p); me.r.count+=1; if (arr[i].arr) { r = me.start(arr[i], arr[i].arr); } if (arr[i].txt === me.key || r) { return 1; } me.r.count-=1; me.r.arr.pop(); }; } }; new def() })()
js实现深度优先遍历 和树的先序基本相识的思想
以上是关于js实现深度优先的主要内容,如果未能解决你的问题,请参考以下文章