代码来自闵老师”日撸 Java 三百行(21-30天)“,链接:https://blog.csdn.net/minfanphd/article/details/116975721
1、Integer 类在对象中包装了一个基本类型int的值;该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,同时还提供了其他一些处理int类型时非常有用的常量和方法。常用方法如下表:
2、循环队列存储对象,利用强制转换实现,代码如下。
/**
* ***************************************************************
* Convert the tree to data arrays, including a char array and an int array.
* The results are stored in two member variables.
*
* @see #valueArray
* @see #indicesArray
* ***************************************************************
*/
public void toDataArraysObjectQueue() {
//Initialize arrays.
int tempLength = getNumNodes();
valueArray = new char[tempLength];
indicesArray = new int[tempLength];
int i = 0;
//Initialize circle queue. Traverse and convert at the same time.
CircleObjectQueue tempQueue = new CircleObjectQueue();
tempQueue.enqueue(this);
CircleObjectQueue tempIntQueue = new CircleObjectQueue();
Integer tempIndexInteger = Integer.valueOf(0);
tempIntQueue.enqueue(tempIndexInteger);
BinaryCharTree tempTree = (BinaryCharTree) tempQueue.dequeue();
int tempIndex = ((Integer) tempIntQueue.dequeue()).intValue();
while (tempTree != null) {
valueArray[i] = tempTree.value;
indicesArray[i] = tempIndex;
i++;
if (tempTree.leftChild != null) {
tempQueue.enqueue(tempTree.leftChild);
tempIntQueue.enqueue(tempIndex * 2 + 1);
}//of if
if (tempTree.rightChild != null) {
tempQueue.enqueue(tempTree.rightChild);
tempIntQueue.enqueue(tempIndex * 2 + 2);
}//of if
tempTree = (BinaryCharTree) tempQueue.dequeue();
if (tempTree == null) {
break;
}//of if
tempIndex = ((Integer) tempIntQueue.dequeue()).intValue();
}//of while
}//of toDataArraysObjectQueue
这里有一个问题没有想明白,为啥最后要加一个if语句判断temptree是否为空。自己也进行了测试,不判断强制转换会报错。前一天的二叉树存储toDataArray()中没有进行判断。
3、泛型实现
/**
* *******************************************************************************
* Convert the tree to data arrays, including a char array and an int array.
* The results are stored in two member variables.
*
* @see #valueArray
* @see #indicesArray
* *******************************************************************************
*/
public void toDataArrayGenericQueue() {
//Initialize arrays.
int tempLength = getNumNodes();
valueArray = new char[tempLength];
indicesArray = new int[tempLength];
int i = 0;
//Initialize circle queue. Traverse and convert at the same time.
CircleGenericQueue<BinaryCharTree> tempQueue = new CircleGenericQueue<BinaryCharTree>();
tempQueue.enqueue(this);
CircleGenericQueue<Integer> tempIntQueue = new CircleGenericQueue<Integer>();
Integer tempIndexInteger = Integer.valueOf(0);
tempIntQueue.enqueue(tempIndexInteger);
BinaryCharTree tempTree = tempQueue.dequeue();
int tempIndex = (tempIntQueue.dequeue()).intValue();
while (tempTree != null) {
valueArray[i] = tempTree.value;
indicesArray[i] = tempIndex;
i++;
if (tempTree.leftChild != null) {
tempQueue.enqueue(tempTree.leftChild);
tempIntQueue.enqueue(tempIndex * 2 + 1);
}//of if
if (tempTree.rightChild != null) {
tempQueue.enqueue(tempTree.rightChild);
tempIntQueue.enqueue(tempIndex * 2 + 2);
}//of if
tempTree = tempQueue.dequeue();
if (tempTree == null) {
break;
}//of if
tempIndex = (tempIntQueue.dequeue()).intValue();
}//of while
}//of toDataArrayGenericQueue
/**
* *********************************************************
* The entrance of the program.
*
* @param args Not used now
* *********************************************************
*/
public static void main(String args[]) {
BinaryCharTree tempTree = manualConstructTree();
System.out.println("\r\nPre-order visit:");
tempTree.preOrderVisit();
System.out.println("\r\nIn-order visit:");
tempTree.inOrderVisit();
System.out.println("\r\nPost-order visit:");
tempTree.postOrderVisit();
int tempDepth = tempTree.getDepth();
System.out.println("\nThe depth is: " + tempDepth);
System.out.println("\rThe number of nodes for the binary tree is: " + tempTree.getNumNodes());
tempTree.toDataArrays();
System.out.println("The values are: " + Arrays.toString(tempTree.valueArray));
System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));
tempTree.toDataArraysObjectQueue();
System.out.println("Only object queue.");
System.out.println("The values are: " + Arrays.toString(tempTree.valueArray));
System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));
tempTree.toDataArrayGenericQueue();
System.out.println("Generic queue.");
System.out.println("The values are: " + Arrays.toString(tempTree.valueArray));
System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));
}//of main