二叉樹的前序官疲、中序、后序亮隙、層次遍歷
基本概念可以查看什么是二叉樹
import lombok.Data;
import java.util.LinkedList;
/**
* 二叉樹的前序袁余、中序、后序咱揍、層次遍歷
*
* @author shenjg
* @date 2019/07/15
*/
@Data
public class TreeNode {
/**
* 數(shù)據(jù)
*/
private Integer data;
/**
* 左孩子
*/
private TreeNode leftChild;
/**
* 右孩子
*/
private TreeNode rightChild;
/**
* 前序遍歷
*
* @param node
*/
private static void preOrder(TreeNode node) {
System.out.println(node.getData());
if (node.getLeftChild() != null) {
preOrder(node.getLeftChild());
}
if (node.getRightChild() != null) {
preOrder(node.getRightChild());
}
}
/**
* 中序遍歷
*
* @param node
*/
private static void mediumOrder(TreeNode node) {
if (node == null) {
return;
} else {
mediumOrder(node.getLeftChild());
System.out.println(node.getData());
mediumOrder(node.getRightChild());
}
}
/**
* 后序遍歷
*
* @param node
*/
private static void postOrder(TreeNode node) {
if (node == null) {
return;
} else {
mediumOrder(node.getLeftChild());
mediumOrder(node.getRightChild());
System.out.println(node.getData());
}
}
/**
* 層次遍歷
*
* @param node
*/
private static void levelOrder(TreeNode node) {
if (node == null) {
return;
}
LinkedList<TreeNode> nodeLinkedList = new LinkedList<>();
nodeLinkedList.add(node);
TreeNode treeNode;
while (!nodeLinkedList.isEmpty()) {
treeNode = nodeLinkedList.poll();
System.out.println(treeNode.getData());
if (treeNode.getLeftChild() != null) {
nodeLinkedList.add(treeNode.getLeftChild());
}
if (treeNode.getRightChild() != null) {
nodeLinkedList.add(treeNode.getRightChild());
}
}
}
public static void main(String[] args) {
TreeNode[] treeNodes = new TreeNode[11];
for (int i = 0; i <= 10; i++) {
TreeNode treeNode = new TreeNode();
treeNode.setData(i + 1);
treeNodes[i] = treeNode;
}
for (int i = 0; i < 5; i++) {
treeNodes[i].setLeftChild(treeNodes[(i + 1) * 2 - 1]);
treeNodes[i].setRightChild(treeNodes[(i + 1) * 2]);
}
System.out.println(treeNodes);
System.out.println("先序遍歷");
preOrder(treeNodes[0]);
System.out.println("中序遍歷");
mediumOrder(treeNodes[0]);
System.out.println("后序遍歷");
postOrder(treeNodes[0]);
System.out.println("層次遍歷");
levelOrder(treeNodes[0]);
}
}