package com.commontest.tree;/**
* @author jiangfuqiang
* @version 2016/7/12.
* @see com.commontest.tree
*/
import java.util.LinkedList;
import java.util.List;
/**
* 二叉树
* @author jiangfuqiang
* @date 2016-07-12
*/
public class TestTree {
private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
private static List nodeList = null;
public static void main(String[] args) {
String[] datas = {
"7","2","6","5","11","5","9","4"
};
TestTree tt = new TestTree();
tt.createBinTree();
Node rootTemp = nodeList.get(0);
System.out.println(tt.getNodeNum(rootTemp));
System.out.println(tt.getDepth(rootTemp));
tt.preOrderTraverse(rootTemp);
System.out.println();
tt.inOrderTraverse(rootTemp);
System.out.println();
tt.postOrderTraverse(rootTemp);
}
public void createBinTree() {
nodeList = new LinkedList();
// 将一个数组的值依次转换为Node节点
for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) {
nodeList.add(new Node(array[nodeIndex]+""));
}
// 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树
for (int parentIndex = 0; parentIndex < array.length / 2 - 1; parentIndex++) {
// 左孩子
nodeList.get(parentIndex).setLeftNode(nodeList.get(parentIndex * 2 + 1));
// 右孩子
nodeList.get(parentIndex).setRightNode(nodeList.get(parentIndex * 2 + 2));
}
// 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理
int lastParentIndex = array.length / 2 - 1;
// 左孩子
nodeList.get(lastParentIndex).setLeftNode(nodeList.get(lastParentIndex * 2 + 1));
// 右孩子,如果数组的长度为奇数才建立右孩子
if (array.length % 2 == 1) {
nodeList.get(lastParentIndex).setRightNode(nodeList.get(lastParentIndex * 2 + 2));
}
}
/**
* 二叉树的节点个数
* @param root
* @return
*/
public int getNodeNum(Node root) {
if (root == null) {
return 0;
}
return getNodeNum(root.getLeftNode()) + getNodeNum(root.getRightNode()) +1;
}
/**
* 二叉树的深度
* @param root
* @return
*/
public int getDepth(Node root) {
if (root == null) {
return 0;
}
int leftDepth = getDepth(root.getLeftNode());
int rightDepth = getDepth(root.getRightNode());
return Math.max(leftDepth+1,rightDepth+1);
}
/**
* 前序遍历递归解法:
(1)如果二叉树为空,空操作
(2)如果二叉树不为空,访问根节点,前序遍历左子树,前序遍历右子树
* @param root
*/
public void preOrderTraverse(Node root) {
if (root == null) {
return;
}
visit(root);
preOrderTraverse(root.getLeftNode());
preOrderTraverse(root.getRightNode());
}
/**
*中序遍历递归解法
(1)如果二叉树为空,空操作。
(2)如果二叉树不为空,中序遍历左子树,访问根节点,中序遍历右子树
* @param root
*/
public void inOrderTraverse(Node root) {
if (root == null) {
return;
}
inOrderTraverse(root.getLeftNode());
visit(root);
inOrderTraverse(root.getRightNode());
}
/**
* 后序遍历递归解法
(1)如果二叉树为空,空操作
(2)如果二叉树不为空,后序遍历左子树,后序遍历右子树,访问根节点
* @param root
*/
public void postOrderTraverse(Node root) {
if (root == null) {
return;
}
postOrderTraverse(root.getLeftNode());
postOrderTraverse(root.getRightNode());
visit(root);
}
private void visit(Node root) {
System.out.print("visit: "+root.getValue()+" ");
}
}
class Node {
private String value;
private Node leftNode;
private Node rightNode;
public Node(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public Node getLeftNode() {
return leftNode;
}
public void setLeftNode(Node leftNode) {
this.leftNode = leftNode;
}
public Node getRightNode() {
return rightNode;
}
public void setRightNode(Node rightNode) {
this.rightNode = rightNode;
}
}