二叉树遍历主要分为四种:前序遍历,中序遍历,后序遍历以及层序遍历
二叉树的遍历,就是按照某条搜索路径访问树中的每一个结点,使得每个结点均被访问一次,而且仅被访问一次。常见的遍历次序有:
- 先序遍历:先访问根结点,再访问左子树,最后访问右子树
- 中序遍历:先访问左子树,再访问根结点,最后访问右子树
- 后序遍历:先访问左子树,再访问右子树,最后访问根结点
- 层序遍历:按照遍历的次序,对当前结点分别调用左子树和右子树即可。
!.新建一个TreeNode
package com.pingan.pahm.tree;
/**
* @author xhw
* @version 1.0
* @apiNote:
* @date 2021/11/4 13:34
*/
public class TreeNode {
/**
* 根节点
*/
String value = null;
/**
* 左子节点
*/
TreeNode leftChildren = null;
/**
* 右子节点
*/
TreeNode rightChildren = null;
public TreeNode(String value, TreeNode leftChildren, TreeNode rightChildren) {
this.value = value;
this.leftChildren = leftChildren;
this.rightChildren = rightChildren;
}
public TreeNode(String value) {
this.value = value;
}
public void setValue(String value) {
this.value = value;
}
public void setLeftChildren(TreeNode leftChildren) {
this.leftChildren = leftChildren;
}
public void setRightChildren(TreeNode rightChildren) {
this.rightChildren = rightChildren;
}
public String getValue() {
return value;
}
public TreeNode getLeftChildren() {
return leftChildren;
}
public TreeNode getRightChildren() {
return rightChildren;
}
@Override
public String toString() {
return "TreeNode{" +
"value='" + value + '\'' +
", leftChildren=" + leftChildren +
", rightChildren=" + rightChildren +
'}';
}
}
2.遍历
package com.pingan.pahm.tree;
import java.util.LinkedList;
/**
* @author xhw
* @version 1.0
* @apiNote:
* @date 2021/11/4 13:37
*/
public class TreeTest {
public TreeNode getTargetTree() {
/**
* 叶子节点
*/
TreeNode G = new TreeNode("G");
TreeNode D = new TreeNode("D");
TreeNode E = new TreeNode("E", G, null);
TreeNode B = new TreeNode("B", D, E);
TreeNode H = new TreeNode("H");
TreeNode I = new TreeNode("I");
TreeNode F = new TreeNode("F", H, I);
TreeNode C = new TreeNode("C", null, F);
/**
* 构造根节点
*/
TreeNode root = new TreeNode("A", B, C);
return root;
}
/**
* 前序遍历 先根节点->左子树->右子树;
*/
public void preOrderVisitTreeNode(TreeNode node) {
if (null != node) {
System.out.print(node.value);
if (null != node.getLeftChildren()) {
preOrderVisitTreeNode(node.getLeftChildren());
}
if (null != node.getRightChildren()) {
preOrderVisitTreeNode(node.getRightChildren());
}
}
}
/**
* 中序遍历 先左子树->根节点->右子树;
*/
public void inorderVisitTreeNode(TreeNode node) {
if (null != node) {
if (null != node.getLeftChildren()) {
inorderVisitTreeNode(node.getLeftChildren());
}
System.out.print(node.value);
if (null != node.getRightChildren()) {
inorderVisitTreeNode(node.getRightChildren());
}
}
}
/**
* 后序遍历 先左子树->右子树->根节点;
*/
public void postOrderVisitTreeNode(TreeNode node) {
if (null != node) {
if (null != node.getLeftChildren()) {
postOrderVisitTreeNode(node.getLeftChildren());
}
if (null != node.getRightChildren()) {
postOrderVisitTreeNode(node.getRightChildren());
}
System.out.print(node.value);
}
}
/**
* 层次遍历
*/
public void levelOrderVisitTreeNode(TreeNode node) {
if (null != node) {
LinkedList<TreeNode> list = new LinkedList<>();
list.add(node);
TreeNode currentNode;
while (!list.isEmpty()) {
currentNode = list.poll();
System.out.print(currentNode.value);
if (null != currentNode.getLeftChildren()) {
list.add(currentNode.getLeftChildren());
}
if (null != currentNode.getRightChildren()) {
list.add(currentNode.getRightChildren());
}
}
}
}
public static void main(String[] args) {
TreeTest treeSearch = new TreeTest();
TreeNode tree = treeSearch.getTargetTree();
System.out.print("前序遍历:");
treeSearch.preOrderVisitTreeNode(tree);
System.out.println("");
System.out.print("中序遍历:");
treeSearch.inorderVisitTreeNode(tree);
System.out.println("");
System.out.print("后序遍历:");
treeSearch.postOrderVisitTreeNode(tree);
System.out.println("");
System.out.print("层次遍历:");
treeSearch.levelOrderVisitTreeNode(tree);
System.out.println("");
}
}