[プログラミング問題]java既知のツリー前シーケンス/後シーケンス+中シーケンス計算後シーケンス/前シーケンス

3966 ワード

//   
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