您现在的位置是:首页 > 后台技术 > 数据结构与算法数据结构与算法
顺序存储二叉树(图文)
第十三双眼睛2023-10-19【数据结构与算法】人已围观
简介顺序存储二叉树
顺序存储二叉树
package com.xingchen.day009; public class ArrayBinTreeDemo { public static void main(String[] args) { int[] arr = {1,2,3,4,5,6,7}; ArrayBinTree tree = new ArrayBinTree(arr); tree.preOrder(0); } } class ArrayBinTree { public int[] arr; public ArrayBinTree(int[] arr) { this.arr = arr; } public void preOrder(int index) { if (arr == null || arr.length == 0 ) { System.out.println("数组为空"); } // 输出当前元素 System.out.println(arr[index]); //递归左边 if (index * 2 + 1 < arr.length) { preOrder(index * 2 + 1); } //向右递归 if (index * 2 + 2 < arr.length) { preOrder(index * 2 + 2); } } } |
Tags:
很赞哦! ()
上一篇:二叉树(图文)
下一篇:线索化二叉树(图文)
相关文章
随机图文
-
移动零(图文)
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 请注意 ,必须在不复制数组的情况下原地对数组进行操作 -
快乐数(图文)
快乐数 编写一个算法来判断一个数 n 是不是快乐数。 快乐数定义为: 对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。 然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。 如果这个过程 结果为 1,那么这个数就是快乐数。 如果 n 是 快乐数 就返回 true ;不是,则返回 false 。 -
翻转二叉树(图文)
翻转二叉树 给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。 -
KMP算法解决字符传匹配(图文)
KMP算法解决字符传匹配