/** ******************** * Convert the tree to data arrays, including a char array and an int array. The * results are stored in two member variables. * * @see #valuesArray * @see #indicesArray ********************* */ publicvoidtoDataArraysObjectQueue() { // Initialize arrays. inttempLength= getNumNodes();
// Traverse and convert at the same time. CircleObjectQueuetempQueue=newCircleObjectQueue(); tempQueue.enqueue(this); CircleObjectQueuetempIntQueue=newCircleObjectQueue(); IntegertempIndexInteger= Integer.valueOf(0); tempIntQueue.enqueue(tempIndexInteger);
/** ********************* * The second constructor. The parameters must be correct since no validity * check is undertaken. * * @param paraDataArray The array for data. * @param paraIndicesArray The array for indices. ********************* */ publicBinaryCharTree(char[] paraDataArray, int[] paraIndicesArray) { // Step 1. Use a sequential list to store all nodes. inttempNumNodes= paraDataArray.length; BinaryCharTree[] tempAllNodes = newBinaryCharTree[tempNumNodes]; for (inti=0; i < tempNumNodes; i++) { tempAllNodes[i] = newBinaryCharTree(paraDataArray[i]); } // Of for i
// Step 2. Link these nodes. for (inti=1; i < tempNumNodes; i++) { for (intj=0; j < i; j++) { System.out.println("indices " + paraIndicesArray[j] + " vs. " + paraIndicesArray[i]); if (paraIndicesArray[i] == paraIndicesArray[j] * 2 + 1) { tempAllNodes[j].leftChild = tempAllNodes[i]; System.out.println("Linking " + j + " with " + i); break; } elseif (paraIndicesArray[i] == paraIndicesArray[j] * 2 + 2) { tempAllNodes[j].rightChild = tempAllNodes[i]; System.out.println("Linking " + j + " with " + i); break; } // Of if } // Of for j } // Of for i
// Step 3. The root is the first node. value = tempAllNodes[0].value; leftChild = tempAllNodes[0].leftChild; rightChild = tempAllNodes[0].rightChild; }// Of the the second constructor