1.创建对象
package com.zl;
public class TreeNode {
private String vlaue;
private TreeNode leftTreeNode;
private TreeNode rightTreeNode;
public TreeNode(String vlaue, TreeNode leftTreeNode, TreeNode rightTreeNode) {
this.vlaue = vlaue;
this.leftTreeNode = leftTreeNode;
this.rightTreeNode = rightTreeNode;
}
public String getVlaue() {
return vlaue;
}
public void setVlaue(String vlaue) {
this.vlaue = vlaue;
}
public TreeNode getLeftTreeNode() {
return leftTreeNode;
}
public void setLeftTreeNode(TreeNode leftTreeNode) {
this.leftTreeNode = leftTreeNode;
}
public TreeNode getRightTreeNode() {
return rightTreeNode;
}
public void setRightTreeNode(TreeNode rightTreeNode) {
this.rightTreeNode = rightTreeNode;
}
}
2.遍历算法实现
package com.zl;
import java.util.LinkedList;
import java.util.Stack;
public class BinaryTree {
/* 迭代方式 先序遍历二叉树 */
public static void preOrderIteration(TreeNode tree) {
Stack<TreeNode> stack = new Stack<>();
while (true) {
while (tree != null) {
System.out.printf(tree.getVlaue() + "\t");
stack.push(tree);
tree = tree.getLeftTreeNode();
}
if (stack.empty()) {
break;
}
TreeNode popTreeNode = stack.pop();
tree = popTreeNode.getRightTreeNode();
}
}
/* 迭代方式 中序遍历二叉树 */
public static void midOrderIteration(TreeNode tree) {
Stack<TreeNode> stack = new Stack<>();
while (true) {
while (tree != null) {
stack.push(tree);
tree = tree.getLeftTreeNode();
}
if (stack.empty()) {
break;
}
TreeNode popTreeNode = stack.pop();
System.out.printf(popTreeNode.getVlaue() + "\t");
tree = popTreeNode.getRightTreeNode();
}
}
/* 迭代方式 后序遍历二叉树 */
public static void postOrderIteration(TreeNode tree) {
TreeNode hasPrintTree = null;
Stack<TreeNode> stack = new Stack<>();
while (true) {
while (tree != null) {
stack.push(tree);
tree = tree.getLeftTreeNode();
}
if (stack.empty()) {
break;
}
if (stack.lastElement().getRightTreeNode() == null) {
//1.右子树为空
TreeNode treeNode = stack.pop();
hasPrintTree = treeNode;
System.out.print(treeNode.getVlaue() + "\t");
tree = null;
} else if (stack.lastElement().getRightTreeNode() == hasPrintTree) {
//2.右子树不为空,且已经打印过了
TreeNode treeNode = stack.pop();
hasPrintTree = treeNode;
System.out.print(treeNode.getVlaue() + "\t");
tree = null;
} else {
//3.右子树不为空 没有打印过
TreeNode rightTreeNode = stack.lastElement().getRightTreeNode();
tree = rightTreeNode;
}
}
}
/* 迭代方式层序遍历二叉树 */
public static void levelOrderIteration(TreeNode tree) {
if (tree == null) return;
LinkedList<TreeNode> treeNodeList = new LinkedList<>();
treeNodeList.add(tree);
while (!treeNodeList.isEmpty()) {
TreeNode pushTree = treeNodeList.removeFirst();
System.out.print(pushTree.getVlaue() + "\t");
if (pushTree.getLeftTreeNode() != null) {
treeNodeList.addLast(pushTree.getLeftTreeNode());
}
if (pushTree.getRightTreeNode() != null) {
treeNodeList.addLast(pushTree.getRightTreeNode());
}
}
}
public static void main(String[] args) {
//测试二叉树形式
/*
1
/ \
2 3
/ / \
4 5 6
/ \
7 8
\
9
*/
//构建二叉树
TreeNode treeNode9 = new TreeNode("9", null, null);
TreeNode treeNode4 = new TreeNode("4", null, null);
TreeNode treeNode7 = new TreeNode("7", null, null);
TreeNode treeNode6 = new TreeNode("6", null, null);
TreeNode treeNode8 = new TreeNode("8", null, treeNode9);
TreeNode treeNode2 = new TreeNode("2", treeNode4, null);
TreeNode treeNode5 = new TreeNode("5", treeNode7, treeNode8);
TreeNode treeNode3 = new TreeNode("3", treeNode5, treeNode6);
TreeNode tree = new TreeNode("1", treeNode2, treeNode3);
System.out.print("递归先序遍历tree:");
preOrderRecursive(tree);
System.out.print(System.lineSeparator());
System.out.print("迭代先序遍历tree:");
preOrderIteration(tree);
System.out.print(System.lineSeparator());
System.out.print(System.lineSeparator());
System.out.print("递归中序遍历tree:");
midOrderRecursive(tree);
System.out.print(System.lineSeparator());
System.out.print("迭代中序遍历tree:");
midOrderIteration(tree);
System.out.print(System.lineSeparator());
System.out.print(System.lineSeparator());
System.out.print("递归后序遍历tree:");
postOrderRecursive(tree);
System.out.print(System.lineSeparator());
System.out.print("迭代后序遍历tree:");
postOrderIteration(tree);
System.out.print(System.lineSeparator());
System.out.print(System.lineSeparator());
System.out.print("迭代层序遍历tree:");
levelOrderIteration(tree);
System.out.print(System.lineSeparator());
System.out.print(System.lineSeparator());
}
/* 递归方式先序遍历二叉树 */
public static void preOrderRecursive(TreeNode tree) {
if (null == tree) {
return;
} else {
System.out.print(tree.getVlaue() + "\t");
preOrderRecursive(tree.getLeftTreeNode());
preOrderRecursive(tree.getRightTreeNode());
}
}
/* 递归方式中序遍历二叉树 */
public static void midOrderRecursive(TreeNode tree) {
if (null == tree) {
return;
} else {
midOrderRecursive(tree.getLeftTreeNode());
System.out.print(tree.getVlaue() + "\t");
midOrderRecursive(tree.getRightTreeNode());
}
}
/* 递归方式后序遍历二叉树 */
public static void postOrderRecursive(TreeNode tree) {
if (null == tree) {
return;
} else {
postOrderRecursive(tree.getLeftTreeNode());
postOrderRecursive(tree.getRightTreeNode());
System.out.print(tree.getVlaue() + "\t");
}
}
}