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

【二叉树】如何构建一个包含大量随机数节点的二叉树测试用例

【二叉树】如何构建一个包含大量随机数节点的二叉树测试用例

  • 前言
  • 一、案例准备
  • 二、自动生成随机二叉树工具类(TreegenerateUtils)
  • 三、如何调用随机二叉树工具类(TreegenerateUtils)?


前言

今天笔者在测试有关二叉树的测试用例时,发现一点一点给节点添加孩子操作十分繁琐,于是写了一个自动生成二叉树测试用例函数,供大家参考。


一、案例准备

准备了一个二叉树节点类Node如下图所示

在这里插入图片描述


二、自动生成随机二叉树工具类(TreegenerateUtils)


  如下面代码所示,定义了一个TreegenerateUtil工具类,其中包含有静态方法generate自动生成随机二叉树,其中该方法有两个重载实现,该方法返回一个随机二叉树实例的根节点,其中两个重载方法都需要四个参数,其中前三个参数相同为currentDepth(树的深度),MaxDepth(树的最大深度),MaxValue(树的每个节点可以达到的最大值),最后一个参数第一个重载方法为Random类实例,目的是使得每次生成的随机树不同,而第二个重载方法的参数为Int数字,跟第一个重载方法也是同样的目的


package net.mooctest;import java.util.Random;/*** @ClassName TreeUtils* @Description* @Author chougou* @Date 2023年11月10日 21:19* @Version 1.0*/
public class TreegenerateUtils {public static Node generate(int currentDepth, int MaxDepth, int MaxValue, Random random1) {       //currentDepth为当前深度,MaxDepth为树的最大深度,MaxValue为节点可以达到的最大数,均可自行调节,random为随机数种子//因为每次递归深度不同,因此currentDepth+random可作为随机数种子if (currentDepth >= MaxDepth) {                     //当递归树的深度超过MaxDepth时候,递归结束return new Node(random1.nextInt(MaxValue));} else {Node node = new Node(random1.nextInt(MaxValue));//随机添加左孩子或者右孩子或者左右孩子或者无孩子//1,只添加左孩子int temp = random1.nextInt(100);if (temp % 3 == 0) {Node nodeleft = generate(currentDepth + 1, MaxDepth, MaxValue, random1);node.left = nodeleft;}//2.只添加右孩子if (temp % 3 == 1) {Node noderight = generate(currentDepth + 1, MaxDepth, MaxValue, random1);node.right = noderight;}//3.左右孩子均添加if (temp % 3 == 2) {Node noderight = generate(currentDepth + 1, MaxDepth, MaxValue, random1);Node nodeleft = generate(currentDepth + 1, MaxDepth, MaxValue, random1);node.left = nodeleft;node.right = noderight;}//4.最后一种即什么也不添加return node;}}public static Node generate(int currentDepth, int MaxDepth, int MaxValue, int random) {       //currentDepth为当前深度,MaxDepth为树的最大深度,MaxValue为节点可以达到的最大数,均可自行调节,random为随机数种子Random random1 = new Random(currentDepth + random);        //因为每次递归深度不同,因此currentDepth+random可作为随机数种子if (currentDepth >= MaxDepth) {                     //当递归树的深度超过MaxDepth时候,递归结束return new Node(random1.nextInt(MaxValue));} else {Node node = new Node(random1.nextInt(MaxValue));//随机添加左孩子或者右孩子或者左右孩子或者无孩子//1,只添加左孩子int temp = random1.nextInt(100);if (temp % 3 == 0) {Node nodeleft = generate(currentDepth + 1, MaxDepth, MaxValue, random);node.left = nodeleft;}//2.只添加右孩子if (temp % 3 == 1) {Node noderight = generate(currentDepth + 1, MaxDepth, MaxValue, random);node.right = noderight;}//3.左右孩子均添加if (temp % 3 == 2) {Node noderight = generate(currentDepth + 1, MaxDepth, MaxValue, random);Node nodeleft = generate(currentDepth + 1, MaxDepth, MaxValue, random);node.left = nodeleft;node.right = noderight;}//4.最后一种即什么也不添加return node;}}
}


三、如何调用随机二叉树工具类(TreegenerateUtils)?


  如下面程序示例,通过调用TreegenerateUtils的静态函数generate生成4个根节点,其中每个根节点均不相同


 @Testpublic void testTreegenerateUtils(){//下面生成四个最大深度为10,节点可达到最大值为100的四个随机二叉树实例Random random1=new Random();Node root1=TreegenerateUtils.generate(1,10,100,random1.nextInt());Node root2=TreegenerateUtils.generate(1,10,100,random1.nextInt());Node root3=TreegenerateUtils.generate(1,10,100,random1);Node root4=TreegenerateUtils.generate(1,10,100,random1);}

运行结果展示:(每个根节点均不相同)
在这里插入图片描述

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

相关文章:

  • 防火防盗防小人 使用 Jasypt 库来加密配置文件
  • Spring Cloud学习(二)【Eureka注册中心】
  • 数据分析实战 | 线性回归——女性身高与体重数据分析
  • python回文日期 并输出下一个ABABBABA型回文日期
  • Zotero拓展功能之Zotero Style
  • 小程序提交表单之后,清除表单form
  • Java程序设计实验5 | Java API应用
  • 自媒体项目详述
  • 客服呼叫中心的语音质检工作
  • 深度解密 | 灵脉SAST 3.0最新特性曝光
  • NowCode JZ39 数组中出现次数超过一半的数字 简单
  • 【SA8295P 源码分析 (一)】119 - QNX 中如何在代码中快速配置 TLMM_GPIO 或 PMIC_GPIO 中断 及 中断回调函数
  • 电大搜题:开启智能学习新时代
  • 19、Flink 的Table API 和 SQL 中的自定义函数及示例(4)
  • Vue23-props配置功能
  • 怎样使用ovsyunlive在web网页上直接播放rtsp/rtmp视频
  • MySQL | 查询接口性能调优、编码方式不一致导致索引失效
  • ASUS华硕灵耀X2 Duo UX481FA(FL,FZ)_UX4000F工厂模式原装出厂Windows10系统
  • 企业安全—三保一评
  • “深入理解机器学习性能评估指标:TP、TN、FP、FN、精确率、召回率、准确率、F1-score和mAP”
  • Linux软件包(源码包和二进制包)
  • Leetcode-394 字符串解码(不会,复习)
  • 如何在Linux上搭建本地Docker Registry并实现远程连接
  • assets_common.min.js
  • 前端工程化(vue2)
  • 深度学习(生成式模型)——Classifier Guidance Diffusion
  • Hadoop架构、Hive相关知识点及Hive执行流程
  • P1529 [USACO2.4] 回家 Bessie Come Home 题解
  • Python语法基础(条件语句 循环语句 函数 切片及索引)
  • Debian 9 Stretch APT问题