假设待搜索的多叉树结构如下:

1

/    |     \

3      5      7

/  |  \    |    /   \

9   2   4   6  8    10

对应的代码如下:

package com.test.Tree;import java.util.*;public class N_aryTree {Tree tree=null;public static void main(String[] args) {// TODO Auto-generated method stubN_aryTree n_aryTree=new N_aryTree();}public N_aryTree() {// TODO Auto-generated constructor stubTree tree=new Tree();tree.BFS(tree.rootNode, 10);tree.DFS(tree.rootNode, 11);}}class Node{public int val;public List<Node> children;public int getVal() {return val;}public void setVal(int val) {this.val = val;}public List<Node> getChildren() {return this.children;}public void setChildren(List<Node> children) {this.children = children;}public Node(int val) {this.val = val;this.children = null;}}class Tree{Node rootNode;Node[] nodes=new Node[10]; List<Node> tempChildren=new Vector<Node>();List<Node> Children1=new Vector<Node>();List<Node> Children2=new Vector<Node>();List<Node> Children3=new Vector<Node>();List<Node> Children4=new Vector<Node>();Queue<Node> childrenQue=new LinkedList<Node>();Stack<Node> childrenSta=new Stack<Node>();public Tree() {// TODO Auto-generated constructor stub//先给每个节点赋值for (int i = 0; i < nodes.length; i++) {nodes[i]=new Node(i+1);}//建立节点之间的父子关系Children1.add(nodes[8]);        Children1.add(nodes[1]);Children1.add(nodes[3]);        nodes[2].setChildren(Children1);Children2.add(nodes[5]);nodes[4].setChildren(Children2);Children3.add(nodes[7]);Children3.add(nodes[9]);nodes[6].setChildren(Children3);Children4.add(nodes[2]);Children4.add(nodes[4]);Children4.add(nodes[6]);nodes[0].setChildren(Children4);rootNode=nodes[0];//System.out.println(nodes[2].children.size());}// breadth-first-search,node为待搜索的树的根节点,val为待搜索的节点的值public boolean BFS(Node node,int val) {System.out.println("BFS值为"+val+"的节点");if (node==null) {System.out.println("该树为空");return false;}      System.out.print("搜索路径:");if (node.getVal()==val) {return true;}childrenQue.offer(node);
/*      for (int i = 0; i < tempChildren.size(); i++) {childrenQue.offer(tempChildren.get(i));}*/while (!childrenQue.isEmpty()) {///System.out.println("hello");Node headNode=childrenQue.poll();System.out.print(headNode.getVal()+" ");if (headNode.getVal()==val) {System.out.println("找到节点");return true;}tempChildren=headNode.getChildren();if (tempChildren!=null) {for (int i = 0; i < tempChildren.size(); i++) {childrenQue.offer(tempChildren.get(i));}}
/*          for (int i = 0; i < tempChildren.size(); i++) {System.out.println("该节点孩子节点的个数为:"+tempChildren.size()+" 第"+(i+1)+"个节点的值为:"+tempChildren.get(i).val);childrenQue.offer(tempChildren.get(i));}*/}System.out.println("已遍历,该树不包含待寻节点");return false;}// depth-first-search,node为待搜索的树的根节点,val为待搜索的节点的值public boolean DFS(Node node,int val) {System.out.println("DFS值为"+val+"的节点");if (node==null) {System.out.println("该树为空");return false;}System.out.print("搜索路径:");if (node.getVal()==val) {return true;}childrenSta.push(node);System.out.print(node.val+" ");while (!childrenSta.isEmpty()) {Node headNode=childrenSta.peek();if (headNode.getChildren()==null||headNode.getChildren().isEmpty()) {Node toRmNode=childrenSta.pop();if (!childrenSta.isEmpty()) {Node fatherNode=childrenSta.peek();fatherNode.getChildren().remove(toRmNode);}}else {Node toAddNode=headNode.getChildren().get(0);System.out.print(toAddNode.getVal()+" ");if (toAddNode.getVal()==val) {System.out.println("找到节点");return true;}childrenSta.push(toAddNode);}}System.out.println("已遍历,该树不包含待寻节点");return false;}}

运行结果:

BFS值为10的节点
搜索路径:1 3 5 7 9 2 4 6 8 10 找到节点
DFS值为11的节点
搜索路径:1 3 9 2 4 5 6 7 8 10 已遍历,该树不包含待寻节点

树的Breadth-First-Search和Depth-First-Search的java实现相关推荐

  1. 数据结构与算法(python):广度优先搜索(Breadth First Search,BFS)和深度优先算法(Depth First Search,DFS)

    参考自 MOOC数据结构与算法Python版 目录 一.广度优先搜索 1.1 BFS算法过程 1.2 广度优先搜索算法分析 二.深度优先搜索 2.1 骑士周游 2.1.1 问题定义 2.1.2 构建骑 ...

  2. DFS(Depth First Search,深度优先搜索)与BFS(Breadth First Search,广度优先搜索)总结与思考

    目录 Depth First Search(dfs) 代码(递归) 代码(非递归) Breadth First Search(bfs) 代码 比较 数据结构 时间复杂度 使用 例题:岛的个数 dfs实 ...

  3. 深度优先遍历(Depth First Search, 简称 DFS)

    正文开始: 深度优先遍历(Depth First Search, 简称 DFS) 与广度优先遍历(Breath First Search)是图论中两种非常重要的算法,生产上广泛用于拓扑排序,寻路(走迷 ...

  4. JavaScript实现depth First Search深度优先搜索算法(附完整源码)

    JavaScript实现depth First Search深度优先搜索算法(附完整源码) depthFirstSearch.js完整源代码 depthFirstSearch.js完整源代码 func ...

  5. 图的深度优先遍历(Depth First Search)

    图的深度优先遍历(Depth First Search) 基本思想 类似于二叉树的先序遍历 假设图中所有结点均未被访问,从初始结点访问,访问其第一个邻接结点,接着以被访问的邻接结点作为初始结点,访问它 ...

  6. pat04-树7. Search in a Binary Search Tree (25)

    04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...

  7. php字符串search,js获取location.search每个查询字符串的值

    形如https://www.debug.org/temp/test2.html?a=1&b=2#ddd这样的链接,虽可通过location.search属性获取到问号后的所有查询字符串值,但要 ...

  8. 04-树7. Search in a Binary Search Tree (25)

    04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...

  9. python search用法,Python-re中search()函数的用法详解(查找ip)

    1.首先来看一下search()和find()的区别 import re s1 = "2221155" #search 字符串第一次出现的位置 print(re.search(&q ...

  10. 深度优先搜索(Depth First Search)、广度优先搜索(Breadth First Search)

    DFS: /* 邻接表存储的图 - DFS */void Visit( Vertex V ) {printf("正在访问顶点%d\n", V); }/* Visited[]为全局变 ...

最新文章

  1. Mac OS 提高工作效率的几个快捷键
  2. 推荐65个以自然风光为背景的UI设计
  3. B1059 C语言竞赛
  4. 原来 Excel 只需三步就可以给证件照换底色!
  5. Request.UrlReferrer详解
  6. SCOI2010 股票交易
  7. elementui 响应式导航栏网站_什么是响应式网站?响应式网站的优势介绍
  8. Git 忽略一些文件的提交
  9. python创建socket对象_python---一个简单的socket
  10. github如何同步fork到自己仓库的代码
  11. day09、1 - 简单渗透测试流程
  12. LeetCode-121. 买卖股票的最佳时机(java)
  13. 用php编写一个函数_使给定的一个二维数组(3×3)转置,写一个函数 使给定的一个3x3的二维数组转置,即行列互换...
  14. oracle中vim设置行号,vim的常用操作
  15. 【C语言】从字符串中提取IP地址最简洁的方法
  16. 安装计算机的显卡出现问题,电脑显卡驱动安装失败如何解决
  17. App Store拒绝原因
  18. 用c语言写鸡兔同笼问题
  19. import和@import
  20. Hadoop第七天--MapReduceYarn详解(二)

热门文章

  1. AngularJS中的谷歌地图开发
  2. FFMPEG进阶系列02-ffmpeg命令详解3
  3. Mockito 框架用于单元测试
  4. 长训终于完成了,深圳驾考,一把鼻涕一把泪
  5. 基于特征的图像匹配算法,图片相似度匹配算法
  6. pta—紧急救援 (dijkstra)
  7. 什么专业可以免考计算机二级,计算机二级证书可以免考自考哪些科目?
  8. 数据库中删除重复数据并保留一条。
  9. Cynthia问题、任务、缺陷管理系统
  10. 短视频seo获客系统,短视频SEO,短视频seo推广