DAY23 使用具有通用性的队列
1.初始化数组
//Initialize arrays.int tempLength = getNumNodes();valuesArray = new char[tempLength];indicesArray = new int[tempLength];int i = 0;
2.初始化队列
创建了一个名为tempQueue的CircleObjectQueue对象,即一个循环对象队列。
将当前对象(即this所指向的对象)入队到tempQueue队列尾部。
//Traverse and convert at the same time.CircleObjectQueue tempQueue = new CircleObjectQueue();tempQueue.enqueue(this);
创建了另一个名为tempIntQueue的CircleObjectQueue对象,即又一个循环对象队列。
创建了一个Integer对象tempIndexInteger,其值为0。这里使用了Integer.valueOf方法来获取一个值为0的Integer对象,而不是直接使用基本数据类型int,可能是因为队列中需要存储的是对象。
将tempIndexInteger对象加入到tempIntQueue队列中。
CircleObjectQueue tempIntQueue = new CircleObjectQueue();Integer tempIndexInteger = Integer.valueOf(0);tempIntQueue.enqueue(tempIndexInteger);
什么是Integer.valueOf方法?Integer.valueOf 是 Java 中 Integer 类的一个静态方法,用于将一个 int 值或 String 对象转换为 Integer 对象。
什么是表示该 int 值的 Integer 对象?通过Integer类将一个基本数据类型int的值封装成一个对象。在Java中,int是基本数据类型,而Integer是对应的包装类。包装类Integer提供了将int值转换为对象的能力,这样就可以在需要对象的上下文中使用int值。
3.层次遍历
从队列中取出BinaryCharTree对象
BinaryCharTree tempTree = (BinaryCharTree) tempQueue.dequeue();
tempIntQueue是一个队列,存储了Integer对象。
dequeue方法从队列中取出一个元素,并返回该元素。
((Integer) tempIntQueue.dequeue())是一个类型转换操作,将从队列中取出的对象转换为Integer类型。
intValue()方法将Integer对象转换为基本数据类型int。
int tempIndex = ((Integer) tempIntQueue.dequeue()).intValue();
打印tempIndex的值,遍历tempTree并存储数据到数组中
System.out.println("tempIndex = " + tempIndex);while (tempTree != null) {valuesArray[i] = tempTree.value;indicesArray[i] = tempIndex;i++;
检查 tempTree 的左子节点是否为空,如果tempTree的左子节点不为null,则将左子节点加入到tempQueue队列中。同时,将左子节点的索引值tempIndex * 2 + 1加入到tempIntQueue队列中。这里的索引值计算方式是基于二叉树的层次遍历索引,假设根节点的索引为0,左子节点的索引为2 * 父节点索引 + 1。
右子节点同上。
if (tempTree.leftChild != null) {tempQueue.enqueue(tempTree.leftChild);tempIntQueue.enqueue(Integer.valueOf(tempIndex * 2 + 1));} // Of ifif (tempTree.rightChild != null) {tempQueue.enqueue(tempTree.rightChild);tempIntQueue.enqueue(Integer.valueOf(tempIndex * 2 + 2));} // Of if
从队列中取出一个BinaryCharTree对象,并检查该对象是否为null。如果为null,则跳出当前的循环。该操作用于避免空指针异常。
tempTree = (BinaryCharTree) tempQueue.dequeue();if (tempTree == null) {break;
4.main函数
构造二叉树,进行前序遍历、中序遍历、后序遍历并计算树的深度和节点数。
public static void main(String args[]) {BinaryCharTree tempTree = manualConstructTree();System.out.println("\r\nPreorder visit:");tempTree.preOrderVisit();System.out.println("\r\nIn-order visit:");tempTree.inOrderVisit();System.out.println("\r\nPost-order visit:");tempTree.PostOrderVisit();System.out.println("\r\n\r\nThe depth is: " + tempTree.getDepth());System.out.println("The number of nodes is: " + tempTree.getNumNodes());
将树的数据存储到数组中
tempTree.toDataArrays();System.out.println("The values are: " + Arrays.toString(tempTree.valuesArray));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.valuesArray));System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));
5.运行结果
Preorder visit:
a b d f g c e
In-order visit:
b f d g a e c
Post-order visit:
f g d b e c a
The depth is: 4
The number of nodes is: 7
No element in the queue
The values are: [a, b, c, d, e, f, g]
The indices are: [0, 1, 2, 4, 5, 9, 10]
tempIndex = 0
Only object queue.
The values are: [a, b, c, d, e, f, g]
The indices are: [0, 1, 2, 4, 5, 9, 10]
注意
main要包含在class类中
今天运行的过程中报错,原因是main函数放在了最外层class的花括号外。