当前位置:网站首页>BFS and DFS
BFS and DFS
2022-06-21 17:35:00 【lanleihhh】
BFS And DFS
BFS:(Breadth-First-Search ) Breadth first search , Using queue implementation , Look for the shortest path
DFS:(Depth-First-Search) Depth-first search , Use recursion to implement , Quick discovery of bottom nodes
BFS:
DFS:
In the tree BFS And DFS
BFS application : Sequence traversal of trees : queue
public class BinarySearchTree<T extends Comparable<T>> {
// The node of the tree
class Node {
// Content of node
private T val;
// Left child node
private Node left;
// Right child node
private Node right;
public Node(T val) {
this.val = val;
}
}
// Sequence traversal ( breadth-first )
public List<T> layerOrder() {
List<T> list = new ArrayList<>();
if (root != null) {
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
// Take the first element from the queue
Node node = queue.poll();
list.add(node.val);
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
}
return list;
}
}
DFS: Depth first , Middle order traversal of trees ( And preface , In the following order )
// In the sequence traversal
public List<T> middleOrder() {
// Store the values traversed from the tree
List<T> res = new ArrayList<>();
// Use recursive traversal
middleOrder(root, res);
// Returns a collection of elements in the storage tree
return res;
}
// In the sequence traversal recursive
private void middleOrder(Node root, List<T> res) {
// Recursion to the end
if (root == null) {
return;
}
// Recursive operation
// First traverse the left tree , Re traversal root, Then traverse the right tree
middleOrder(root.left, res);
res.add(root.val);
middleOrder(root.right, res);
}
In the picture BFS and DFS
To be added …
边栏推荐
- Compose 中的附带效应
- FragmentStatePagerAdapter 与FragmentPagerAdapter的区别
- 室内膨胀型防火涂料根据BS 476-21 耐火标准测定需要符合几项?
- kotlin 注解声明与使用
- Still using xshell? Try this cool SSH terminal tool, which is very powerful!
- 用Node创建一个服务器
- Common formula of derivative__ Common formulas of indefinite integral
- iframe跨域传值
- 变量
- [MySQL learning notes 14] DQL statement execution sequence
猜你喜欢

How to connect the Internet - FTTH

Mqtt of NLog custom target

Set up your own website (11)

【Leetcode】297. Serialization and deserialization of binary tree (difficult)

20 pygame模块制作一个跳跃的小球游戏

In the "roll out" era of Chinese games, how can small and medium-sized manufacturers solve the problem of going to sea?

Common setting modes

How can aggressive programmers improve R & D efficiency Live broadcast Preview

QT knowledge: using the qgraphicspixmapitem class

shamir
随机推荐
经纬度转换为距离
牛客网:大数加法
Sorting out Android kotlin generic knowledge points
Simple ideas and procedures for quick sorting
AS 3744.1标准中提及ISO8191测试,两者测试一样吗?
一些细节
钣金行业MES系统的特色需求
Common setting modes
[MySQL learning notes 14] DQL statement execution sequence
compose 编程思想
第八章 可编程接口芯片及应用【微机原理】
shamir
Niuke network: verify the IP address
go corn定时任务简单应用
clickhouse学习笔记2:基本使用教程
【没搞懂路由策略?盘它!】
uni-app框架学习笔记
Jetpack Compose 管理状态(一)
AttributeError: ‘Book‘ object has no attribute ‘sheet‘
BFS与DFS