当前位置: 首页 > news >正文

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的花括号外。

http://www.lryc.cn/news/522584.html

相关文章:

  • 汽车网络信息安全-ISO/SAE 21434解析(上)
  • 通用查询类接口开发的另类思路
  • uc/os-II 原理及应用(八) 系统裁减以及移植到51单片机-下
  • 职场沟通与行为
  • 【tailscale 和 ssh】当服务器建立好节点,但通过客户端无法通过 ssh 连接
  • Spark 之 Aggregate
  • 【JSqlParser】Java使用JSqlParser解析SQL语句总结
  • Linux下的dev,sys和proc(TODO)
  • 【Unity3D】利用Hinge Joint 2D组件制作绳索效果
  • Springer Nature——Applied Intelligence 投稿指南
  • 数据结构、数据类型、数字编码、字符编码:保姆级图文详解
  • DM适配连接kettle迁移工具(资源库+数据源配置)
  • WINFORM - DevExpress -> alertControl1提示信息框
  • STM32-串口-UART-Asynchronous
  • Nginx三种不同类型的虚拟主机(基于域名、IP 和端口)
  • Shell控监Kafka积压
  • element-ui textarea备注 textarea 多行输入框
  • Transformer创新模型!Transformer+BO-SVR多变量回归预测,添加气泡图、散点密度图(Matlab)
  • 大疆机场及无人机上云
  • 用Cursor生成一个企业官网前端页面(生成腾讯、阿里官网静态页面)
  • Java 数组排序
  • LeetCode:78.子集
  • 【经济学通识——国债】
  • 联合体(Union)
  • Kibana:ES|QL 编辑器简介
  • 【工具】curl工具
  • idea中远程调试中配置的参数说明
  • JavaWeb 前端基础 html + CSS 快速入门 | 018
  • Debian 设定 tomcat 定时重启
  • 【QT】: 初识 QWidget 控件 | QWidget 核心属性(API) | qrc 文件