本二叉树可以通过迭代得到二叉树的高度、节点个数,通过迭代实现了前序中序后序遍历二叉树的方法,同时通过栈实现了非迭代的前序中序后序遍历二叉树的方法。
import java.util.Stack;
public class BinaryTree {
TreeNode<String> root = null;
public BinaryTree() {
this.root = new TreeNode<String>("A");
}
/*
* 构建二叉树
* A
* B C
* D E F
*/
public void createTree() {
TreeNode<String> nodeb = new TreeNode<String>("B");
TreeNode<String> nodec = new TreeNode<String>("C");
TreeNode<String> noded = new TreeNode<String>("D");
TreeNode<String> nodee = new TreeNode<String>("E");
TreeNode<String> nodef = new TreeNode<String>("F");
root.leftchild = nodeb;
root.rightchild = nodec;
nodeb.leftchild = noded;
nodeb.rightchild = nodee;
nodec.rightchild = nodef;
}
/*
* 构建另一个二叉树来测试代码是否正确
* A
* B C
* D E F G
* H I J K
*/
public void createTestTree() {
TreeNode<String> nodeb = new TreeNode<String>("B");
TreeNode<String> nodec = new TreeNode<String>("C");
TreeNode<String> noded = new TreeNode<String>("D");
TreeNode<String> nodee = new TreeNode<String>("E");
TreeNode<String> nodef = new TreeNode<String>("F");
TreeNode<String> nodeg = new TreeNode<String>("G");
TreeNode<String> nodeh = new TreeNode<String>("H");
TreeNode<String> nodei = new TreeNode<String>("I");
TreeNode<String> nodej = new TreeNode<String>("J");
TreeNode<String> nodek = new TreeNode<String>("K");
root.leftchild = nodeb;
root.rightchild = nodec;
nodeb.leftchild = noded;
nodeb.rightchild = nodee;
noded.leftchild = nodeh;
nodee.leftchild = nodei;
nodec.leftchild = nodef;
nodec.rightchild = nodeg;
nodef.rightchild = nodej;
nodeg.leftchild = nodek;
}
/*
* 求二叉树的高度
*/
public int getHeight() {
return getHeight(this.root);
}
private int getHeight(TreeNode<String> r) {
if(r == null) {
return 0;
}
else {
int heightL = getHeight(r.leftchild);
int heightR = getHeight(r.rightchild);
return (heightL > heightR) ? (heightL+1): (heightR+1);
}
}
/*
* 求二叉树节点个数
*/
public int getSize() {
return getSize(this.root);
}
private int getSize(TreeNode<String> r) {
if(r == null) {
return 0;
}
else {
return 1 + getSize(r.leftchild) + getSize(r.rightchild);
}
}
/*
* 前序遍历
*/
public void preOrder(TreeNode<String> node) {
if(node == null) {
return;
}
else {
System.out.print(node.value + " ");
preOrder(node.leftchild);
preOrder(node.rightchild);
}
}
/*
* 中序遍历
*/
public void inOrder(TreeNode<String> node) {
if(node == null) {
return;
}else {
inOrder(node.leftchild);
System.out.print(node.value + " ");
inOrder(node.rightchild);
}
}
/*
* 后序遍历
*/
public void postOrder(TreeNode<String> node) {
if(node == null) {
return;
}else {
postOrder(node.leftchild);
postOrder(node.rightchild);
System.out.print(node.value + " ");
}
}
/*
* 用非迭代方法实现前序遍历,用栈
* tip 判断栈是否为空 不能用 a != null, 应该用 !a.isEmpty()
*/
public void preOrderInStack(TreeNode<String> node) {
Stack<TreeNode<String>> a = new Stack<>();
if(node == null) {
System.out.println("空树");
}else {
a.push(node);
while(!a.isEmpty()) {
TreeNode<String> b = a.pop();
System.out.print(b.value + " ");
if(b.rightchild != null) {
a.push(b.rightchild);
}
if(b.leftchild != null) {
a.push(b.leftchild);
}
}
}
}
/*
* 用非迭代方法实现中序遍历
*/
public void inOrderInStack(TreeNode<String> node) {
Stack<TreeNode<String>> a = new Stack<>();
if(node == null) {
System.out.println("空树");
}else {
a.push(node);
TreeNode<String> b = a.peek();
while(b.leftchild != null) {
a.push(b.leftchild);
b = a.peek();
}
while(!a.isEmpty()) {
TreeNode<String> c = a.peek();
if(c.leftchild == null || c.leftchild.isChecked == true) {
a.pop();
c.isChecked = true;
System.out.print(c.value + " ");
}else {
a.push(c.leftchild);
continue;
}
if(c.rightchild != null) {
a.push(c.rightchild);
}
}
}
}
/*
* 用非迭代方法实现后序遍历
*/
public void postOrderInStack(TreeNode<String> node) {
Stack<TreeNode<String>> a = new Stack<>();
if(node == null) {
System.out.println("空树");
}else {
a.push(node);
TreeNode<String> b = a.peek();
while(b.leftchild != null) {
a.push(b.leftchild);
b = a.peek();
}
while(!a.isEmpty()) {
TreeNode<String> c = a.peek();
if(c.rightchild != null && c.rightchild.isChecked == false) {
a.push(c.rightchild);
}
if(c.leftchild != null && c.leftchild.isChecked == false) {
a.push(c.leftchild);
}
if(c != a.peek()) {
continue;
}else {
a.pop();
c.isChecked = true;
System.out.print(c.value + " ");
}
}
}
}
/*
* 进行非迭代的中序或者后序遍历之后,要重新把元素的isChecked属性设置为false,以免影响另一个遍历
*/
public void unchecked(TreeNode<String> node) {
if(node == null) {
return;
}else {
node.isChecked = false;
unchecked(node.leftchild);
unchecked(node.rightchild);
}
}
public class TreeNode<E>{
private E value;
private TreeNode<E> leftchild;
private TreeNode<E> rightchild;
private boolean isChecked;
public TreeNode(E val) {
this.value = val;
this.leftchild = null;
this.rightchild = null;
this.isChecked = false;
}
public E getValue() {
return value;
}
public void setValue(E value) {
this.value = value;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
BinaryTree tree = new BinaryTree();
tree.createTestTree();
TreeNode<String> r = tree.root;
System.out.println(tree.getHeight());
System.out.println(tree.getSize());
tree.preOrder(r);
System.out.print("\n");
tree.inOrder(r);
System.out.print("\n");
tree.postOrder(r);
System.out.print("\n");
tree.preOrderInStack(r);
System.out.print("\n");
tree.inOrderInStack(r);
System.out.print("\n");
tree.unchecked(r);
tree.postOrderInStack(r);
}
}