当前位置:网站首页>顺序二叉树---实现数组的二叉树前序遍历输出
顺序二叉树---实现数组的二叉树前序遍历输出
2022-07-30 05:46:00 【RSDYS】
特点:
1.顺序二叉树只考虑完全二叉树
2.第n个元素的左子节点为2*n+1
3.第n个元素的右子节点为2*n+2
4.第n个元素的父节点为(n-1)/2
代码:
/*
* 给你一个数组,要求以二叉树前序遍历的方式进行遍历
* */
public class ArrayBinaryTreeDemo {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4 , 5, 6, 7};
ArrBinaryTree arrBinaryTree = new ArrBinaryTree(arr);
arrBinaryTree.preOrder(0);
}
}
//顺序二叉树
class ArrBinaryTree{
private int[] arr; //存储数据节点的数组
public ArrBinaryTree(int[] arr){
this.arr = arr;
}
//重载
public void preOrder(){
this.preOrder(0);
}
//完成前序遍历
/* index 为数组下标
* */
public void preOrder(int index){
//如果数组为空,或者arr.length = 0;
if(arr == null || arr.length == 0){
System.out.println("数组为空,不能按照二叉树前序遍历");
}
//输出当前这个元素
System.out.println(arr[index]);
//向左递归遍历
if((index * 2 + 1) < arr.length){
preOrder(2 * index + 1);
}
if((index * 2 + 2) < arr.length){
preOrder(2 * index + 2);
}
}
}边栏推荐
- 华秋第八届硬创赛与安创加速器达成战略合作,助力硬科技项目成长
- QT每周技巧(2)~~~~~~~~~界面按钮
- 电子工程师怎么才能规范设计标准、提高设计效率?
- vs编译boost库脚本
- Diwen serial screen production (serialization 1) ===== preparation work
- openssl1.1.1ARM双编译
- QT serial 4: LORA test platform based on QT and STM32H750 (3)
- 三种内核结构---宏内核、微内核、混合内核
- 删除当前路径下含某个关键字的所有文件
- 【江科大自化协stm32F103c8t6】笔记之【入门32单片机及EXTI外部中断初始化参数配置】
猜你喜欢

QT serial 2: LORA test platform based on QT and STM32H750 (1)

探究make_shared效率

pdf和word等文档添加水印
![[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Entry 32 MCU and GPIO initialization parameter configuration]](/img/96/a98e8b813a2fd9d0a44d3121aaee6a.png)
[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Entry 32 MCU and GPIO initialization parameter configuration]

Target detection, object classification and semantic segmentation of UAV remote sensing images based on PyTorch deep learning

动态规划入门 JS

js高级学习笔记(详细)

干货 | 什么是FOC?一文带你看BLDC电机驱动芯片及解决方案

Application of remote sensing, GIS and GPS technology in hydrology, meteorology, disaster, ecology, environment and health

QT weekly skills (3)~~~~~~~~~ serial port addition
随机推荐
FPGA parsing B code----serial 1
Cannnot download sources不能下载源码百分百超详细解决方案
QT serial 2: LORA test platform based on QT and STM32H750 (1)
《C陷阱和缺陷》void (*signal(int , void(*)(int)))(int)的深刻解读
QT连载4:基于QT和STM32H750的LORA试验平台(3)
>>> /deep/ ::v-deep 深度作用选择器
昆仑通态屏幕制作(连载3)---基础篇(按钮串口发送)
clinit方法
【markdown常用用法】
IEEE在指定期刊下搜索相关论文
Acwing刷题第一节
租用服务器训练yolov3模型
C语言,库函数中qsort的用法,及解释
【正点原子】IIC的学习与使用(未完...)
JS的值和引用,复制和传递
QT serialization 1: readyRead() function, the solution to incomplete data subcontracting
jvm之方法区
工程师必看:常见的PCB检测方法有哪些?
Kunlun State Screen Production (Serialization 2)---Basic Chapter (setting and display, serial transmission)
this是什么(你不知道的JS)