让我们先建立一棵树:
A B C D E F
package 树;
import javax.swing.tree.TreeNode;
import java.util.Stack;
/**
* @author Dracular
* @version $Rev$
* @des ${TODO}
* @date 2019/2/1 下午4:00
* @updateAuthor $Author$
* @updateDes ${TODO}
*/
public class BinaryTree {
/**
* 根结点
*/
private TreeNode root = null;
public BinaryTree() {
root = new TreeNode(1, "A");
}
/**
* 构造一颗二叉树
* A
* B C
* D E F
*/
public void createBinaryTree() {
TreeNode nodeB = new TreeNode<>(2, "B");
TreeNode nodeC = new TreeNode<>(3, "C");
TreeNode nodeD = new TreeNode<>(4, "D");
TreeNode nodeE = new TreeNode<>(5, "E");
TreeNode nodeF = new TreeNode<>(6, "F");
root.leftChild = nodeB;
root.rightChild = nodeC;
nodeB.leftChild = nodeD;
nodeB.rightChild = nodeE;
nodeC.rightChild = nodeF;
}
/**
* 计算树的高度
*
* @return
*/
public int getHeight() {
return getHeight(root);
}
private int getHeight(TreeNode root) {
if (root == null) {
return 0;
} else {
int i = getHeight(root.leftChild);
int j = getHeight(root.rightChild);
return i > j ? i + 1 : j + 1;
}
}
/**
* 计算树的结点数
*
* @return
*/
public int getSize() {
return getSize(root);
}
private int getSize(TreeNode root) {
if (root == null) {
return 0;
} else {
return getSize(root.leftChild) + getSize(root.rightChild) + 1;
}
}
/**
* 先序遍历
*
* @param node
*/
public void preOrder(TreeNode node) {
if (node == null) {
return;
} else {
System.out.println("preOrder:" + node.getData());
preOrder(node.leftChild);
preOrder(node.rightChild);
}
}
/**
* 先序遍历 - 非递归
*
* @param node
*/
public void noonPreRecOrder(TreeNode node) {
if (node == null) {
return;
}
Stack stack = new Stack<>();
stack.push(node);
while (!stack.isEmpty()) {
TreeNode n = stack.pop();
System.out.println("noonRecOrder:" + n.getData());
if (n.rightChild != null) {
stack.push(n.rightChild);
}
if (n.leftChild != null) {
stack.push(n.leftChild);
}
}
}
/**
* 中序遍历
*
* @param node
*/
public void midOrder(TreeNode node) {
if (node == null) {
return;
} else {
midOrder(node.leftChild);
System.out.println("midOrder:" + node.getData());
midOrder(node.rightChild);
}
}
/**
* 后序遍历 - 非递归
*
* @param node
*/
public void nonMidRecOrder(TreeNode node) {
if (node == null) {
return;
}
Stack stack = new Stack<>();
while (node != null || !stack.isEmpty()) {
while (node != null) {
stack.push(node);
node = node.leftChild;
}
if (!stack.isEmpty()) {
node = stack.pop();
System.out.println("nonMidRecOrder:" + node.getData());
node = node.rightChild;
}
}
}
/**
* 后序遍历
*
* @param node
*/
public void postOrder(TreeNode node) {
if (node == null) {
return;
} else {
postOrder(node.leftChild);
postOrder(node.rightChild);
System.out.println("postOrder" + node.getData());
}
}
/**
* 后序遍历 - 非递归
*
* @param node
*/
public void noonPostRecOrder(TreeNode node) {
if (node == null) {
return;
}
Stack stack1 = new Stack<>();
Stack stack2 = new Stack<>();
while (node != null || !stack1.isEmpty()) {
while (node != null) {
stack1.push(node);
stack2.push(0);
node = node.leftChild;
}
while (!stack1.isEmpty() && stack2.peek() == 1) {
stack2.pop();
System.out.println("noonPostRecOrder:" + stack1.pop().getData());
}
if (!stack1.isEmpty()) {
stack2.pop();
stack2.push(1);
node = stack1.peek();
node = node.rightChild;
}
}
}
/**
* 树的结点
*
* @param 数据类型
*/
public class TreeNode {
/**
* 下标
*/
private int index;
private T data;
/**
* 左结点
*/
private TreeNode leftChild;
/**
* 右结点
*/
private TreeNode rightChild;
public TreeNode(int index, T data) {
this.index = index;
this.data = data;
this.leftChild = null;
this.rightChild = null;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
binaryTree.createBinaryTree();
int height = binaryTree.getHeight();
System.out.println("Height:" + height);
int size = binaryTree.getSize();
System.out.println("Size:" + size);
System.out.println("----------------------------");
binaryTree.preOrder(binaryTree.root);
System.out.println("----------------------------");
binaryTree.midOrder(binaryTree.root);
System.out.println("----------------------------");
binaryTree.postOrder(binaryTree.root);
System.out.println("----------------------------");
binaryTree.noonPreRecOrder(binaryTree.root);
System.out.println("----------------------------");
binaryTree.nonMidRecOrder(binaryTree.root);
System.out.println("----------------------------");
binaryTree.noonPostRecOrder(binaryTree.root);
}
}
Height:3
Size:6
----------------------------
preOrder:A
preOrder:B
preOrder:D
preOrder:E
preOrder:C
preOrder:F
----------------------------
midOrder:D
midOrder:B
midOrder:E
midOrder:A
midOrder:C
midOrder:F
----------------------------
postOrderD
postOrderE
postOrderB
postOrderF
postOrderC
postOrderA
----------------------------
noonRecOrder:A
noonRecOrder:B
noonRecOrder:D
noonRecOrder:E
noonRecOrder:C
noonRecOrder:F
----------------------------
nonMidRecOrder:D
nonMidRecOrder:B
nonMidRecOrder:E
nonMidRecOrder:A
nonMidRecOrder:C
nonMidRecOrder:F
----------------------------
noonPostRecOrder:D
noonPostRecOrder:E
noonPostRecOrder:B
noonPostRecOrder:F
noonPostRecOrder:C
noonPostRecOrder:A