普通树的广度搜索和深度搜索java实现
2017-04-20 18:57
405 查看
数据结构中的树搜索方式可以分为广搜和深搜,顾名思义,广搜就是一层一层的搜索,深搜就是先搜索左子树从左节点搜索一直到叶子节点,然后搜索右子树。
下面用java实现广搜和深搜:
广搜采用队列的结构,java API提供了双端队列的类ArrayDeque
public static Tree find(int value, Tree tree){
Deque<Tree> deque = new ArrayDeque<Tree>();
deque.add(tree);
Tree result = null;
while(!deque.isEmpty()){
Tree temp = deque.pop();
if(temp.element == value){
result = temp;
break;
}
List<Tree> children = temp.getChildren();
if(children != null && children.size() > 0){
for(Tree child : children){
deque.add(child);
}
}
}
return result;
}
class Tree{
private int element;
private List<Tree> children = new ArrayList<Tree>();
public int getElement() {
return element;
}
public void setElement(int element) {
this.element = element;
}
public List<Tree> getChildren() {
return children;
}
public void setChildren(List<Tree> children) {
this.children = children;
}
}
深搜采用栈的数据结构:
public static Tree find2(int value, Tree tree){
Stack<Tree> stack = new Stack<Tree>();
stack.add(tree);
Tree result = null;
while(!stack.isEmpty()){
Tree temp = stack.pop();
if(temp.element == value){
result = temp;
break;
}
List<Tree> children = temp.getChildren();
if(children != null && !children.isEmpty()){
for(Tree child : children){
stack.add(child);
}
}
}
return result;
}
下面用java实现广搜和深搜:
广搜采用队列的结构,java API提供了双端队列的类ArrayDeque
public static Tree find(int value, Tree tree){
Deque<Tree> deque = new ArrayDeque<Tree>();
deque.add(tree);
Tree result = null;
while(!deque.isEmpty()){
Tree temp = deque.pop();
if(temp.element == value){
result = temp;
break;
}
List<Tree> children = temp.getChildren();
if(children != null && children.size() > 0){
for(Tree child : children){
deque.add(child);
}
}
}
return result;
}
class Tree{
private int element;
private List<Tree> children = new ArrayList<Tree>();
public int getElement() {
return element;
}
public void setElement(int element) {
this.element = element;
}
public List<Tree> getChildren() {
return children;
}
public void setChildren(List<Tree> children) {
this.children = children;
}
}
深搜采用栈的数据结构:
public static Tree find2(int value, Tree tree){
Stack<Tree> stack = new Stack<Tree>();
stack.add(tree);
Tree result = null;
while(!stack.isEmpty()){
Tree temp = stack.pop();
if(temp.element == value){
result = temp;
break;
}
List<Tree> children = temp.getChildren();
if(children != null && !children.isEmpty()){
for(Tree child : children){
stack.add(child);
}
}
}
return result;
}
相关文章推荐
- 广度优先搜索与深度优先搜索的 java 实现
- 无权图无向图【邻接矩阵存储】深度搜索和广度搜索--Java实现
- Java实现数据结构之深度优先搜索DFS和广度优先搜索BFS
- JAVA实现广度搜索&深度搜素
- java-二叉树广度优先实现、深度优先之前序实现(非递归)
- (算法入门)基本图论-广度优先搜索之JAVA实现
- 基于图的深度优先搜索和广度优先搜索java实现
- java实现图的遍历(深度优先遍历和广度优先遍历)
- 从Cheney算法->广度优先搜索->倒酒问题(JAVA实现)
- Java实现图:邻接矩阵表示、深度优先搜索、广度优先搜索、无向图的最小生成树
- 图及其算法复习(Java实现) 一:存储结构,深度优先周游,广度优先周游
- 数据结构-图-Java实现:有向图 图存储(邻接矩阵),最小生成树,广度深度遍历,图的连通性,最短路径
- 数据结构-图-Java实现:有向图 图存储(邻接矩阵),最小生成树,广度深度遍历,图的连通性,最短路径
- C++ 图的深度搜索广度搜索和最小生成树代码实现
- 数据结构--图的理解:深度优先和广度优先遍历及其 Java 实现
- (算法入门)基本图论-深度优先搜索之JAVA实现
- c++实现树的广度搜索和深度搜索完…
- 图的深度优先,完全搜索...有点脑残....java实现
- c++实现树的广度搜索和深度搜索完整代码
- 迷宫问题解决方法:分别基于深度搜索和广度搜索的思想来实现