树的搜索
树的深度优先搜索
function Node(value) {
this.value = value;
this.children = [];
}
var a = new Node("a");
var b = new Node("b");
var c = new Node("c");
var d = new Node("d");
var e = new Node("e");
var f = new Node("f");
a.children.push(c);
a.children.push(b);
a.children.push(f);
b.children.push(d);
b.children.push(e);
// 树的深度优先遍历
function deepLink(root) {
if (root == null) return;
console.log(root.value);
for (var i = 0; i < root.children.length; i++) {
deepLink(root.children[i]);
}
}
deepLink(a);
// 树的深度优先搜索
function deepSearch(root, target) {
if (root == null) return false;
if (root.value == target) return true;
var result = false;
for (var i = 0; i < root.children.length; i++) {
result |= deepSearch(root.children[i], target);
}
return result;
}
console.log(deepSearch(a, "c"));
树的广度优先搜索
function Node(value) {
this.value = value;
this.children = [];
}
var a = new Node("a");
var b = new Node("b");
var c = new Node("c");
var d = new Node("d");
var e = new Node("e");
var f = new Node("f");
a.children.push(c);
a.children.push(b);
a.children.push(f);
b.children.push(d);
b.children.push(e);
// 树的广度优先搜索
function bfs(roots, target) {
if (roots == null || roots.length == 0) return false;
var childs = [];
for (var i = 0; i < roots.length; i++) {
if (roots[i].value == target) return true;
else {
childs = childs.concat(roots[i].children);
}
}
return bfs(childs, target);
}
console.log(bfs([a], "m"));
图的搜索
图的深度优先搜索
图的深度优先搜索相比于树来说,就是需要多加判断哪些点是没有走过的
function Node(value) {
this.value = value;
this.neighbor = [];
}
var a = new Node("a");
var b = new Node("b");
var c = new Node("c");
var d = new Node("d");
var e = new Node("e");
a.neighbor.push(b);
a.neighbor.push(c);
b.neighbor.push(d);
b.neighbor.push(a);
b.neighbor.push(c);
c.neighbor.push(d);
c.neighbor.push(a);
c.neighbor.push(b);
d.neighbor.push(c);
d.neighbor.push(b);
d.neighbor.push(e);
e.neighbor.push(d);
// 图的深度优先搜索
function deepSearch(node, target, path) {
// 以node为起点
if (node == null) return false;
if (path.indexOf(node) > -1) return false; //找过了
if (node.value == target) return true;
path.push(node); //已经查看过的
var result = false;
for (var i = 0; i < node.neighbor.length; i++) {
result |= deepSearch(node.neighbor[i], target, path);
}
return result ? true : false;
}
console.log(deepSearch(b, "c", []));
图的广度优先搜索
function Node(value) {
this.value = value;
this.neighbor = [];
}
var a = new Node("a");
var b = new Node("b");
var c = new Node("c");
var d = new Node("d");
var e = new Node("e");
a.neighbor.push(b);
a.neighbor.push(c);
b.neighbor.push(d);
b.neighbor.push(a);
b.neighbor.push(c);
c.neighbor.push(d);
c.neighbor.push(a);
c.neighbor.push(b);
d.neighbor.push(c);
d.neighbor.push(b);
d.neighbor.push(e);
e.neighbor.push(d);
// 图的广度优先搜索
function bfs(nodes, target, path) {
if (nodes.length == 0 || nodes == null) return false;
var nextNode = [];
for (var i = 0; i < nodes.length; i++) {
if (path.indexOf(nodes[i]) > -1) continue; //存在就进行下一次
path.push(nodes[i]);
if (nodes[i].value == target) return true;
else nextNode = nextNode.concat(nodes[i].neighbor);
}
return bfs(nextNode, target, path);
}
console.log(bfs([c], "b", []));