map遍历顺序和保存顺序
Here you will learn about tree traversal with program example.
在这里,您将通过程序示例了解有关树遍历的信息。
Tree is a subset of Graph data structure where the number of edges are exactly one less than the number of vertices (nodes). We can call any graph a tree if it does not have any cycle (closed loop).
树是图数据结构的子集,其中边的数量比顶点(节点)的数量少一。 如果任何图没有任何循环(闭环),我们可以称其为树。
Tree traversal refers to the process of visiting each node of the tree at least once. Unlike basic linear data structures like arrays, linked list, stack and queue where there was only one way of traversing, trees can be traversed in different ways.
树遍历是指至少访问树的每个节点一次的过程。 与只有一种遍历方式的基本线性数据结构(如数组,链表,堆栈和队列)不同,可以以不同的方式遍历树。
We can categorize the tree traversal into two categories:
我们可以将树遍历分为两类:
- Breadth-first Traversal 广度优先遍历
- Depth-first Traversal 深度优先遍历
深度优先遍历 (Depth-first Traversal)
In Depth-first traversal, the direction of traversal is from top to bottom. As the name suggests, we traverse to the depth of the tree starting from the root node of the tree. Based on the way of traversal, we have these three types of traversals:
在深度优先遍历中,遍历的方向是从上到下。 顾名思义,我们从树的根节点开始遍历树的深度。 基于遍历的方式,我们有以下三种遍历类型:
- In-order Traversal 有序遍历
- Pre-order Traversal 预购遍历
- Post-order Traversal 订单遍历
These three types of traversal are usually performed on binary trees. Binary tree is a special type of tree in which a parent node can have at most two child nodes. Binary Search Tree (BST) is a special binary tree where every smaller value is on the left