网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
二叉堆的存储方式是顺序存储,即节点数据都存储在数组中。
Java语言:
import java.util.Arrays;
/\*\*
\* 最小堆的上浮和下沉
\*/
public class BinaryHeap {
/\*\*
\* 上浮调整
\*
\* @param array
\*/
public static void upAdjust(int[] array) {
int childIndex = array.length - 1; //最后一个位置得索引
int parentIndex = (childIndex - 1) / 2; //思考一下为什么不论最后一个元素是左孩子还是右孩子,其父节点得索引计算都是这个公式
while (childIndex > 0 && array[childIndex] < array[parentIndex]) { //上浮的元素不能小于等于0,因为等于0时就已经是根节点了,不能再上浮了,小于0时,就数组下标越界了,所以循环条件是(childIndex >0 && array[childIndex] < array[(childIndex-1)/2]
//交换位置
int temp = array[childIndex];
array[childIndex] = array[parentIndex];
array[parentIndex] = temp;
childIndex = parentIndex;
parentIndex = (childIndex - 1) / 2;
}
}
/\*\*
\* 下沉调整
\*
\* @param array 待调整的堆
\* @param parentIndex 要下沉的父节点
\*/
public static void downAdjust(int[] array, int parentIndex) {
int length = array.length;
int childIndex = 2 \* parentIndex + 1; //左孩子的索引
while (childIndex < length) { //边界判断
if (childIndex + 1 < length && array[childIndex + 1] < array[childIndex]) {//当有右孩子时,判断左右孩子的大小
childIndex = childIndex + 1;//右孩子对应的索引
// childIndex++; //等同于childIndex + 1;
}
if (array[parentIndex] < array[childIndex]) { //当父节点比子节点小的时候,退出循环
break;
}
int temp = array[parentIndex];
array[parentIndex] = array[childIndex];
array[childIndex] = temp;
//交换完毕
parentIndex = childIndex;//将交换过的子节点索引重新给父节点
childIndex = 2 \* parentIndex + 1;//重新计算新的左孩子索引
}
}
public static void buildHeap(int[] array) {
//从最后一个非叶子节点开始,依次做下沉操作
for (int i = (array.length - 2) / 2; i >= 0; i--) {
downAdjust(array, i);
}
}
public static void main(String[] args) {
int[] array = new int[]{1, 3, 2, 6, 5, 7, 8, 9, 10, 0};
upAdjust(array);
System.out.println(Arrays.toString(array)); //结果:[0, 1, 2, 6, 3, 7, 8, 9, 10, 5]
array = new int[]{7, 1, 3, 10, 5, 2, 8, 9, 6};
// downAdjust(array,0);
buildHeap(array);
System.out.println(Arrays.toString(array)); //结果:[1, 5, 2, 6, 7, 3, 8, 9, 10]
}
}
Golang语言:
package main
import "fmt"
/\*\*
\* 最小堆的上浮
\*/
func UpAdjust(array []int) {
childIndex := len(array) - 1
parentIndex := (childIndex - 1) / 2
for childIndex > 0 && array[childIndex] < array[parentIndex] {
temp := array[childIndex]
array[childIndex] = array[parentIndex]
array[parentIndex] = temp
childIndex = parentIndex
parentIndex = (childIndex - 1) / 2
}
}
/\*\*
\* 最小堆的下沉
\*/
func DownAdjust(array []int, parentIndex int) {
length := len(array)
childIndex := 2\*parentIndex + 1
for childIndex < length {
if childIndex+1 < length && array[childIndex+1] < array[childIndex] {
childIndex++
}
if array[parentIndex] < array[childIndex] {
break
}
temp := array[parentIndex]
array[parentIndex] = array[childIndex]
array[childIndex] = temp
parentIndex = childIndex
childIndex = 2\*parentIndex + 1
}
}
func BuildHeap(array []int) {
for i := (len(array) - 2) / 2; i >= 0; i-- {
DownAdjust(array, i)
}
}
func main() {
array := []int{1, 3, 2, 6, 5, 7, 8, 9, 10, 0}
UpAdjust(array)
fmt.Println(array) //结果:[0, 1, 2, 6, 3, 7, 8, 9, 10, 5]
array = []int{7, 1, 3, 10, 5, 2, 8, 9, 6}
//DownAdjust(array, 0)
BuildHeap(array)
fmt.Println(array) //结果:[1, 5, 2, 6, 7, 3, 8, 9, 10]
}
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!**
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新