//树节点
class DataNode{
//节点内容
String data;
//左子节点
DataNode leftChild = null;
//右子节点
DataNode rightChild = null;
}
//二叉树
public class NodeTree {
//二叉树根节点
DataNode rootNode;
//
DataNode left_childDataNode;
DataNode right_childDataNode;
public DataNode initRootNode(String[] preArray){
rootNode = new DataNode();
rootNode.data = preArray[0];
return rootNode;
}
public void BuildTree(String[] preArray,String[] midArray,DataNode rootNode){
int index_root = getIndex(midArray, rootNode.data);
int lengthOfRightTree = preArray.length - index_root -1;
String[] preArray_left;
String[] preArray_right;
String[] midArray_left;
String[] midArray_right;
if (index_root>0) {
left_childDataNode = new DataNode();
if (index_root==1) {
left_childDataNode.data = midArray[0];
rootNode.leftChild = left_childDataNode;
}else {
preArray_left = new String[index_root];
midArray_left = new String[index_root];
System.arraycopy(preArray, 1, preArray_left, 0, index_root);
System.arraycopy(midArray, 0, midArray_left, 0, index_root);
left_childDataNode.data = preArray_left[0];
rootNode.leftChild = left_childDataNode;
BuildTree(preArray_left, midArray_left, left_childDataNode);
}
}
if (lengthOfRightTree>0) {
right_childDataNode = new DataNode();
if (lengthOfRightTree==1) {
right_childDataNode.data = midArray[index_root+1];
rootNode.rightChild = right_childDataNode;
return;
}else {
preArray_right = new String[lengthOfRightTree];
midArray_right = new String[lengthOfRightTree];
System.arraycopy(preArray, index_root+1, preArray_right, 0,lengthOfRightTree);
System.arraycopy(midArray, index_root+1, midArray_right, 0, lengthOfRightTree);
right_childDataNode.data = preArray_right[0];
rootNode.rightChild = right_childDataNode;
BuildTree(preArray_right, midArray_right,right_childDataNode);
}
}
}
public int getIndex(String[] array,String temp){
int index = -1;
for (int i = 0; i < array.length; i++) {
if (array[i]==temp) {
index = i;
return index;
}
}
return index;
}
//后序遍历
public void postOrderTraverse(DataNode node){
if (node==null) {
return;
}
postOrderTraverse(node.leftChild);
postOrderTraverse(node.rightChild);
System.out.print(node.data);
}
//前序遍历
public void preOrderTraverse(DataNode node){
if (node==null) {
return;
}
System.out.print(node.data);
preOrderTraverse(node.leftChild);
preOrderTraverse(node.rightChild);
}
//中序遍历
public void inOrderTraverse(DataNode node){
if (node==null) {
return;
}
inOrderTraverse(node.leftChild);
System.out.print(node.data);
inOrderTraverse(node.rightChild);
}
public static void main(String args[]){
String[] preArray = {"G","D","A","F","E","M","H","Z"};
String[] midArray = {"A","D","E","F","G","H","M","Z"};
NodeTree tree = new NodeTree();
DataNode headNode = tree.initRootNode(preArray);
tree.BuildTree(preArray, midArray, headNode);
tree.postOrderTraverse(headNode);
}
}
输入前序和中序计算输出 后序为:AEFDHZMG