TreeNode.java
public class TreeNode{
public TreeNode left;
public TreeNode right;
public String value;
public TreeNode(TreeNode left, TreeNode right, String value) {
this.left = left;
this.right = right;
this.value = value;
}
}
BinaryTree.java
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class BinaryTree{
// 获取二叉树的高度
public int getTreeHeight(TreeNode root) {
if (root == null)
return 0;
if (root.left == null && root.right == null)
return 1;
return 1 + Math.max(getTreeHeight(root.left), getTreeHeight(root.right));
}
// 递归前序遍历
public void preOrder1(TreeNode root) {
if (root != null) {
System.out.print(root.value);
preOrder1(root.left);
preOrder1(root.right);
}
}
// 递归中序遍历
public void inOrder1(TreeNode root) {
if (root != null) {
inOrder1(root.left);
System.out.print(root.value);
inOrder1(root.right);
}
}
// 递归后序遍历
public void postOrder1(TreeNode root) {
if (root != null) {
postOrder1(root.left);
postOrder1(root.right);
System.out.print(root.value);
}
}
// 非递归前序遍历
public void preOrder2(TreeNode root) {
Stack<TreeNode> s = new Stack<TreeNode>();
TreeNode p = root;
while (p != null || !s.isEmpty()) {
while (p != null) {
s.push(p);
System.out.print(p.value);
p = p.left;
}
if (!s.isEmpty()) {
p = s.pop();
p = p.right;
}
}
}
// 非递归中序遍历
public void inOrder2(TreeNode root) {
Stack<TreeNode> s = new Stack<TreeNode>();
TreeNode p = root;
while (p != null || !s.isEmpty()) {
while (p != null) {
s.push(p);
p = p.left;
}
if (!s.isEmpty()) {
p = s.pop();
System.out.print(p.value);
p = p.right;
}
}
}
/**
* 非递归后序遍历
* 只需保证每个节点都要在其左右孩子节点之后进行访问
* 也就是说当该节点没有孩子节点或者有孩子节点但孩子节点已经被访问时才能进行访问
*/
public void postOrder2(TreeNode root) {
Stack<TreeNode> s = new Stack<TreeNode>();
TreeNode p = root;
TreeNode cur = null;
TreeNode pre = null;
s.push(p);
while (!s.isEmpty()) {
cur = s.peek();
if ((cur.left == null && cur.right == null)
|| (pre != null && (pre == cur.left || pre == cur.right))) {
System.out.print(cur.value);
s.pop();
pre = cur;
} else {
if (cur.right != null)
s.push(cur.right);
if (cur.left != null)
s.push(cur.left);
}
}
}
// 二叉树层序遍历
public void levelOrder(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
TreeNode p = root;
if (p != null)
queue.add(p);
while (!queue.isEmpty()) {
p = queue.poll();
System.out.print(p.value);
if (p.left != null)
queue.offer(p.left);
if (p.right != null)
queue.offer(p.right);
}
}
// 测试
public static void main(String[] args) {
// TODO Auto-generated method stub
// 构造二叉树
TreeNode d = new TreeNode(null, null, "D");
TreeNode e = new TreeNode(null, null, "E");
TreeNode f = new TreeNode(null, null, "F");
TreeNode b = new TreeNode(d, e, "B");
TreeNode c = new TreeNode(f, null, "C");
TreeNode root = new TreeNode(b, c, "A");
BinaryTree bt = new BinaryTree();
System.out.println("二叉树的高度:" + bt.getTreeHeight(root));
System.out.println("================二叉树递归遍历===================");
System.out.println("前序遍历:");
bt.preOrder1(root);
System.out.println("\n中序遍历:");
bt.inOrder1(root);
System.out.println("\n后序遍历:");
bt.postOrder1(root);
System.out.println("\n================二叉树非递归遍历=================");
System.out.println("前序遍历:");
bt.preOrder2(root);
System.out.println("\n中序遍历:");
bt.inOrder2(root);
System.out.println("\n后序遍历:");
bt.postOrder2(root);
System.out.println("\n================二叉树层序遍历===================");
bt.levelOrder(root);
}
}