二叉树的遍历算法,用递归和栈实现前序遍历、中序遍历和后序遍历,用队列实现层序遍历。代码构造的二叉树如下所示:
1.二叉树实体类
package com.zzl.test;
public class BinaryTree {
int data; // 节点数据
BinaryTree left; // 左子树
BinaryTree right; // 右子树
public BinaryTree(int data)
{
this.data = data;
left = null;
right = null;
}
public void insert(BinaryTree root, int data) {
// 向二叉树中插入子节点,每次添加都从根节点向下遍历添加
if (data > root.data) // 大于根节点的放右边
{
if (root.right == null) {
//刚创建时为空,以后则执行else
root.right = new BinaryTree(data);
} else {
this.insert(root.right, data);
}
} else { // 小于根节点的放左边
if (root.left == null) {
root.left = new BinaryTree(data);
} else {
this.insert(root.left, data);
}
}
}
}
2.二叉树遍历类
package com.zzl.test;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;
public class BinaryTreeTraversal {
public static void preOrder(BinaryTree root) { // 前序遍历
if (root != null) {
System.out.print(root.data + "-");
preOrder(root.left); // 递归调用,将左子树递归遍历后,再遍历右子树
preOrder(root.right);
}
}
public static void inOrder(BinaryTree root) { // 中序遍历
if (root != null) {
inOrder(root.left);
System.out.print(root.data + "--");
inOrder(root.right);
}
}
public static void postOrder(BinaryTree root) { // 后序遍历
if (root != null) {
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data + "---");
}
}
public static void preOrderByStack(BinaryTree root)// 前序非递归遍历
{
Stack<BinaryTree> stack = new Stack<BinaryTree>();
while (!stack.empty() || root != null) {
if (root == null) { //左节点为null,开始遍历右节点
BinaryTree temp = stack.pop();
root = temp.right;
} else { //遍历左节点
System.out.print(root.data + "-");
stack.push(root);
root = root.left;
}
}
}
public static void inOrderByStack(BinaryTree root)// 中序非递归遍历
{
Stack<BinaryTree> stack = new Stack<BinaryTree>();
while (!stack.empty() || root != null) {
if (root == null) {
BinaryTree temp = stack.pop();
System.out.print(temp.data + "--");
root = temp.right;
} else {
stack.push(root);
root = root.left; //向左下搜索
}
}
}
public static void postOrderByStack(BinaryTree root) // 后序非递归遍历
{
Stack<BinaryTree> stack = new Stack<BinaryTree>();
BinaryTree p = root;
do {
// 先执行一次循环
while (p != null) {// 一直往左下搜索
stack.push(p);
p = p.left;
}
BinaryTree q = null;
while (!stack.empty()) {
p = stack.pop();
if (p.right == q) {
//p的右节点等于q,若q为null,没有右节点,则直接打印p,若q不为null
//则上个打印的点是p的右节点,则该打印p点
System.out.print(p.data + "---");
q = p;// 记录下此时的节点
} else {
stack.push(p);
p = p.right;
break;
}
}
} while (!stack.empty());
}
public static void leaveOrder(BinaryTree root) { // 层序遍历 广度优先遍历
Deque<BinaryTree> deque = new ArrayDeque<BinaryTree>();
deque.add(root);// 队列,先进先出,队列为空时,输出完成
while (!deque.isEmpty()) {
BinaryTree temp = deque.remove();
System.out.print(temp.data + "----");
if (temp.left != null) {
deque.add(temp.left);
}
if (temp.right != null) {
deque.add(temp.right);
}
}
}
public static void main(String[] str) {
int[] array = { 34, 16, 27, 45, 20, 53, 90, 77, 68, 82 };
BinaryTree root = new BinaryTree(array[0]); // 创建二叉树
for (int i = 1; i < array.length; i++) {
root.insert(root, array[i]); // 向二叉树中插入数据
}
System.out.print("前序遍历-递归:");
preOrder(root);
System.out.println();
System.out.print("中序遍历-递归:");
inOrder(root);
System.out.println();
System.out.print("后序遍历-递归:");
postOrder(root);
System.out.println();
System.out.print("前序遍历-栈:");
preOrderByStack(root);
System.out.println();
System.out.print("中序遍历-栈:");
inOrderByStack(root);
System.out.println();
System.out.print("后序遍历-栈:");
postOrderByStack(root);
System.out.println();
System.out.print("层序遍历:");
leaveOrder(root);
System.out.println();
}
}
代码运行结果如下: