Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. One starts at the root (selecting some arbitrary node as the root in the case of a graph) and explores as far as possible along each branch before backtracking.

代码如下:

/**
* @typedef {Object} Callbacks
* @property {function(node: BinaryTreeNode, child: BinaryTreeNode): boolean} allowTraversal -
* Determines whether DFS should traverse from the node to its child.
* @property {function(node: BinaryTreeNode)} enterNode - Called when DFS enters the node.
* @property {function(node: BinaryTreeNode)} leaveNode - Called when DFS leaves the node.
*/ /**
* @param {Callbacks} [callbacks]
* @returns {Callbacks}
*/
function initCallbacks(callbacks = {}) {
const initiatedCallback = callbacks; const stubCallback = () => {};
const defaultAllowTraversal = () => true; initiatedCallback.allowTraversal = callbacks.allowTraversal || defaultAllowTraversal;
initiatedCallback.enterNode = callbacks.enterNode || stubCallback;
initiatedCallback.leaveNode = callbacks.leaveNode || stubCallback; return initiatedCallback;
} /**
* @param {BinaryTreeNode} node
* @param {Callbacks} callbacks
*/
export function depthFirstSearchRecursive(node, callbacks) {
callbacks.enterNode(node); // Traverse left branch.
if (node.left && callbacks.allowTraversal(node, node.left)) {
depthFirstSearchRecursive(node.left, callbacks);
} // Traverse right branch.
if (node.right && callbacks.allowTraversal(node, node.right)) {
depthFirstSearchRecursive(node.right, callbacks);
} callbacks.leaveNode(node);
} /**
* @param {BinaryTreeNode} rootNode
* @param {Callbacks} [callbacks]
*/
export default function depthFirstSearch(rootNode, callbacks) {
depthFirstSearchRecursive(rootNode, initCallbacks(callbacks));
}
 

树的DFS的更多相关文章

  1. CodeForces 343D 线段树维护dfs序

    给定一棵树,初始时树为空 操作1,往某个结点注水,那么该结点的子树都注满了水 操作2,将某个结点的水放空,那么该结点的父亲的水也就放空了 操作3,询问某个点是否有水 我们将树进行dfs, 生成in[u ...

  2. [2]树的DFS序

    定义: 树的DFS序就是在对树进行DFS的时候,对树的节点进行重新编号:DFS序有一个很强的性质: 一颗子树的所有节点在DFS序内是连续的一段, 利用这个性质我们可以解决很多问题. 代码: void ...

  3. 树的dfs序 && 系统栈 && c++ rope

    利用树的dfs序解决问题: 就是dfs的时候记录每个节点的进入时间和离开时间,这样一个完整的区间就是一颗完整的树,就转化成了区间维护的问题. 比如hdu3887 本质上是一个求子树和的问题 #incl ...

  4. CF877E Danil and a Part-time Job 线段树维护dfs序

    \(\color{#0066ff}{题目描述}\) 有一棵 n 个点的树,根结点为 1 号点,每个点的权值都是 1 或 0 共有 m 次操作,操作分为两种 get 询问一个点 x 的子树里有多少个 1 ...

  5. HDU4117 GRE WORDS(AC自动机+线段树维护fail树的dfs序)

    Recently George is preparing for the Graduate Record Examinations (GRE for short). Obviously the mos ...

  6. Codeforces Round #381 (Div. 2)D. Alyona and a tree(树+二分+dfs)

    D. Alyona and a tree Problem Description: Alyona has a tree with n vertices. The root of the tree is ...

  7. Weak Pair---hud5877大连网选(线段树优化+dfs)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5877  题意:给你一颗树,有n个节点,每个节点都有一个权值v[i]:现在求有多少对(u,v ...

  8. bzoj 3551 [ONTAK2010]Peaks加强版(kruskal,主席树,dfs序)

    Description [题目描述]同3545 Input 第一行三个数N,M,Q. 第二行N个数,第i个数为h_i 接下来M行,每行3个数a b c,表示从a到b有一条困难值为c的双向路径. 接下来 ...

  9. 镜像树(dfs)

    1214: J.镜像树 时间限制: 1 Sec  内存限制: 64 MB提交: 18  解决: 7 标签提交统计讨论版 题目描述 一棵二叉树,若其与自己的镜像完全相同,就称其为镜像树(即这棵二叉树关于 ...

  10. poj3140(树的dfs)

    题目链接:http://poj.org/problem?id=3140 题意:给定一棵n棵节点的树,求删去某条边后两个分支的最小差异值. 分析:num[u]表示以u点为根节点的子树的总人数,那么不在该 ...

随机推荐

  1. Mac中制作USB系统启动盘

    .iso镜像文件转 .dmg文件 hdiutil convert -format UDRW -o linuxmint.dmg ~/Desktop/linuxmint-19-cinnamon-64bit ...

  2. python学习笔记-面向对象设计

    前言 1.三大编程范式: 面向过程编程 函数式编程 面向对象编程 2.编程进化论 1.编程最开始就是无组织无结构,从简单控制流中按步写指令 2.从上述的指令中提取重复的代码块或逻辑,组织到一起,便实现 ...

  3. LGOJ1264 K-联赛

    这题其实不难想到 Description link 题意太长了,概括不来,去题库里扫一眼吧(但是很好懂) Solution \[Begin\] 考虑一个事情:每一个队伍的输局是没有用的 贪心一下,让每 ...

  4. oracle中带参存储过程的使用

    Oracle中存储过程带参分为:输入参数(in)和输出参数(out) 例如: create or replace procedure out_test(v_user in emp.user_name% ...

  5. DOCKER 学习笔记2 认识dockerfile自定义镜像

    Dockerfile 概念 Dockerfile 是一个文本文件,但包含所构建容器在运行时候的参数.库.资源配置.可以简单理解为我们现有的镜像,比如Centos/Nginx 但我们需要构建一个容器的时 ...

  6. AI精灵

    由于使用的CRM系统是Aras Innovator系统,所有的任务分配必须登入系统查看,故做出以下自动接受任务信息的小工具. 1.登入,实现自动记录上次登入的信息,支持多账户   登入成功后会以图标运 ...

  7. 【YOLO学习】召回率(Recall),精确率(Precision),平均正确率(Average_precision(AP) ),交除并(Intersection-over-Union(IoU))

    摘要 在训练YOLO v2的过程中,系统会显示出一些评价训练效果的值,如Recall,IoU等等.为了怕以后忘了,现在把自己对这几种度量方式的理解记录一下. 这一文章首先假设一个测试集,然后围绕这一测 ...

  8. shell day01总结

    ,Iptables –Z 清空再计数 内存是如何工作的?是干什么的? 随机存取存储器又称作“随机存储器”,是与CPU直接交换的内部存储器,也叫主存.它可以随时读写,而且速度很快,通常作为操作系统或其他 ...

  9. 【shell基础】

    Ctrl+R 搜索之前的命令 Ctrl+D 退出 Ctrl+A 移动到行首 Ctrl+E 移动到行尾 Ctrl+U 删除光标前的内容 Ctrl+K 删除光标后的内容 Ctrl+S 锁频 Ctrl+Q ...

  10. 论文翻译——Attention Is All You Need

    Attention Is All You Need Abstract The dominant sequence transduction models are based on complex re ...