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

郑州网站开发设计公司电话最新行业动态

郑州网站开发设计公司电话,最新行业动态,自己做的网站怎么在百度搜索到,ftp怎么做网站的备份1.堆&#xff08;Heap&#xff09; 1.1堆的概念 堆是一种非常重要的数据结构&#xff0c;通常被实现为一种特殊的完全二叉树 如果有一个关键码的集合K{k0,k1,k2,...,kn-1}&#xff0c;把它所有的元素按照完全二叉树的顺序存储在一个一维数组中&#xff0c;如果满足ki<k2i…

1.堆(Heap)

1.1堆的概念

堆是一种非常重要的数据结构,通常被实现为一种特殊的完全二叉树

如果有一个关键码的集合K={k0,k1,k2,...,kn-1},把它所有的元素按照完全二叉树的顺序存储在一个一维数组中,如果满足ki<=k2i+1且ki<=k2i+2(i=0,1,2,3...),则称这个集合为小堆;如果满足ki>=k2i+1且ki>=k2i+2(i=0,1,2,3...),则称这个集合为大堆。

简单来说,根节点的值为最大的堆叫做最大堆或大根堆,根节点的值最小的堆叫做最小堆或小根堆

1.2堆的性质

1.完全二叉树的性质

除了最后一层外,每一层都被填满,最后一层的节点从左往后填充

2.堆序性质

最大堆(大根堆):

对于每个节点i,其左子节点2i+1和右子节点2i+2的值都小于或等于i的值

最小堆(小根堆):

对于每个节点i,其左子节点2i+1和右子节点2i+2的值都大于或等于i的值

1.3堆的存储方式

从堆的概念可知,堆是一颗完全二叉树,因此可以以层序的规则方式来高效存储

注意: 对于非完全二叉树来说,不适合使用顺序的方式进行存储,因为为了还原二叉树,空间中必须要存储空节点,就会导致空间利用率比较低

1.4堆的创建

给定一个集合{28,16,20,19,29,35,66,50,26,38},如何将其创建为堆呢?

观察发现:根节点的左右子树都已经满足堆的性质,因此只需要将根节点向下调整即可 

1.4.1堆的向下调整

1.用parent表示要调整的节点,child表示parent的左孩子(注意:堆是一颗完全二叉树,如果parent有孩子一定是先有左孩子

2.如果parent的左孩子存在,即child<size,进行如下操作,直到parent的左孩子不存在:

       parent的右孩子是否存在,如果存在,则找到左右孩子中最小的元素,让child表示这个元素。

       将parent与较小的孩子child进行比较,如果parent小于child,调整结束。

       如果parent大于child,将parent和child进行交换,原来parent中较大的元素向下调整可能会导         致子树不满足堆的性质,因此要继续向下调整,即parent=child,child=2*parent+1,继续进           行步骤2

代码编写:

    public void shiftDown(int[] array,int parent){int child=2*parent+1;int size=array.length;while(child<size){//如果右孩子存在,用child标记左右孩子中较小的值if(child+1<size&&array[child+1]<array[child]){child++;}if(array[parent]>array[child]){swap(array,parent,child);//继续向下调整,为了保证子树也满足堆的性质parent=child;child=2*parent+1;}else{break;}}}private void swap(int[] array,int a,int b){int tmp=array[a];array[a]=array[b];array[b]=tmp;}

 注意:再调整以parent为根的二叉树时,必须满足parent的左子树和右子树已经时堆了才可以进行向下调整。

1.4.2堆的创建(小根堆)

那么对于普通的序列,即根节点的左右子树不满足堆的性质,又该如何创建呢?

例如对普通序列{2,7,8,5,4,1}进行小根堆的创建

根据上面的堆的向下调整,我们的思路就是要将根的左右子树都满足小根堆的特点,我们可以从下向上,从最后一个非叶子节点出发,将其与他们的左右孩子进行比较,将最小的值与非叶子节点进行交换(堆的向下调整),再继续向上执行上述操作,直到操作的节点为根节点即可

代码编写:

     public void createSmallestHeap(int [] array){int root=(array.length-2)>>1;for(;root>=0;root--){shiftDown(array,root);}}

 

1.5堆的插入和删除

1.5.1堆的插入

堆的插入总共需要2步:

1.先将元素放入底层(空间不够时,需要进行扩容)

2.将新插入的元素不断向上调整,直到满足堆的性质

观察可以发现:如果新插入的节点的父节点大于新插入的节点,就进行元素的交换,不断重复该动作

代码编写:

    //child表示新插入元素的索引public void shiftUp(int child){//找到新插入节点的父节点int parent=(child-1)/2;while(child>0){if(array[parent]>array[child]){swap(array,parent,child);child=parent;parent=(child-1)/2;}else {break;}}}
1.5.2堆的删除

堆在删除过程中需要注意删除的元素一定是堆顶元素

1.将堆顶元素和堆中的左后一个元素交换位置

2.将堆中有效个数减少一个

3.对堆顶元素进行向下调整

代码编写:

     public void shiftDown(int[] array,int parent){int child=2*parent+1;int size=array.length;while(child<size){//如果右孩子存在,用child标记左右孩子中较小的值if(child+1<size&&array[child+1]<array[child]){child++;}if(array[parent]>array[child]){swap(array,parent,child);//继续向下调整,为了保证子树也满足堆的性质parent=child;child=2*parent+1;}else{break;}}}public void delete(int[] array){swap(array,0,size-1);//size表示有效元素的个数size--;shiftDown(array,0);}

 

1.6堆的应用

1.堆排序(Heap Sort)

利用堆的性质对数组进行排序,时间复杂度为O(nlogn)

2.优先级队列(PriorityQueue)

堆是实现优先级队列的高校数据结构,支持快速的插入和删除操作

3.Dijkstra算法

在最短路径算法中,堆用于高效地选择当前距离最小的节点

4.Kth Largest Element

也叫topK问题,使用堆可以高效地找到数组中的第k大元素

2.PriorityQueue

2.1什么是优先级队列

通过之前的介绍,队列是一种先进先出(FIFO)的数据结构,但是优先情况下,操作的数据可能带有优先级,并不希望按照队列原始的顺序进行出栈,可能优先级高的元素想先出队列

在生活中有一个很常见的例子:当你在用听歌的时候,突然接到电话,音乐会自动停止,而执行通话的操作

优先级队列(PriorityQueue)是一种特殊的队列,其中的每个元素都有一个优先级,队列会根据优先级来决定元素的出队顺序,优先级高的元素先出队,优先级低的元素后出队,如果两个元素的优先级相同,则按照它们入队列的顺序出队

优先级队列通常基于堆这种数据结构实现,因为堆可以高效地进行插入和删除操作,同时保持元素的优先级顺序

Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,我们主要进行讲解PriorityQueue

2.2PriorityQueue常见操作

PriorityQueue的常见方法:

方法解释
PriorityQueue()创建一个空的优先级队列,默认容量是11
PriorityQueue(int initialCapacity)创建一个初始容量为initialCapacity的优先级队列,注意:initialCapacity不能小于1,否则会抛出IllegalArgumentException异常
PriorityQueue(Collection<? extends E> c)用一个集合来创建优先级队列
boolean offer(E e)插入元素e,插入成功返回true,如果e对象为空,则会抛出NullPointerException异常,空间不够的时候会进行扩容
E peek()获取优先级最高的元素,如果优先级队列为空,返回null
E poll()移除优先级最高的元素,如果优先级队列为空,返回null
int size()获取有效元素的个数
void clear()清空队列
boolean isEmpty()检测优先级队列是否为空,如果为空返回true

 我们以一个复杂的类型来演示:

public class Student implements Comparable<Student>{private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}@Overridepublic int compareTo(Student o) {return this.age-o.age;}
}public class Test {public static void main(String[] args) {PriorityQueue<Student> queue=new PriorityQueue<>();Student s1=new Student("hajimi",21);Student s2=new Student("king",18);queue.offer(s1);queue.offer(s2);System.out.println(queue.size());for(Student s:queue){System.out.println(s);}queue.clear();System.out.println(queue.size());}
}

 

2.3优先级队列的特性

因为优先级队列是基于堆实现的,所以优先级队列的操作的时间复杂度其实就是堆在操作过程中的时间复杂度

1.插入:

将一个新元素插入到队列中,并根据优先级调整队列的顺序,时间复杂度为O(logn)

2.删除:

删除优先级最高的元素,时间复杂度为O(logn)

3.获取优先级最高的元素:

返回优先级最高的元素,但不删除它,时间复杂度为O(1)

4.更新优先级:

更新队列中某个元素的优先级,并调整队列的顺序,时间复杂度为O(logn)

5.Priority中放置的元素必须是能够比较大小的,不能插入无法比较大小的对象,否则会抛出ClassCastException异常

6.不能插入null,否则会抛出NullPointerException

7.PriorityQueue默认情况下是小堆

8.PriorityQueue其内部可以自动扩容

2.4PriorityQueue底层的扩容原理

PriorityQueue的 默认无参构造方法创建的数组长度为11

如果容量小于64时,是按照oldCapacity的2倍方式扩容的

如果容量大于64时,是按照oldCapacity的1.5倍方式扩容的

如果容量超过MAX_ARRAY_SIZE,按照MAX_ARRAY_SIZE来进行扩容

2.5实现一个优先级队列

package datastructure;import java.util.Arrays;public class PriorityQueue {private int elem[];//队列中有效元素的个数private int usedSize;private final static int DEFAULT_INIT_SIZE=11;public PriorityQueue(){elem=new int[DEFAULT_INIT_SIZE];}public PriorityQueue(int[] elem){this.elem=elem;this.usedSize=elem.length;int root=((elem.length-2)>>1);for(;root>=0;root--){shiftDown(root,elem.length);}}private void shiftDown(int parent,int len){int child=parent*2+1;while (child<len){if(child+1<len&&elem[child+1]<elem[child]){child++;}if(elem[parent]>elem[child]){swap(elem,parent,child);}else {break;}}}public boolean offer(int value){if(usedSize==elem.length){Arrays.copyOf(elem,2*elem.length);}elem[usedSize]=value;usedSize++;shiftUp(usedSize-1);return true;}private void swap(int[] elem,int a,int b){int tmp=elem[a];elem[a]=elem[b];elem[b]=tmp;}private void shiftUp(int child){int parent=(child-1)/2;while (child>0){if(elem[child]<elem[parent]){swap(elem,parent,child);child=parent;parent=(child-1)/2;}else {break;}}}public int size(){return usedSize;}public int peek(){if(usedSize==0){System.out.println("优先级队列中没有元素,无法获取元素");return -1;}return elem[0];}public boolean isEmpty(){return usedSize==0;}public int poll(){if(usedSize==0){System.out.println("优先级队列中没有元素,无法删除元素");return -1;}int value=elem[0];swap(elem,0,usedSize-1);usedSize--;shiftDown(0,usedSize-1);return value;}
}

对编写的代码进行运行测试:

public class Test {public static void main(String[] args) {PriorityQueue queue=new PriorityQueue();queue.offer(2);queue.offer(4);queue.offer(3);queue.offer(8);queue.offer(7);queue.offer(5);queue.offer(1);System.out.println(queue.peek());int a= queue.poll();System.out.println(a);System.out.println(queue.peek());System.out.println(queue.size());}
}

 


文章转载自:
http://furniture.c7617.cn
http://deliberatively.c7617.cn
http://petrography.c7617.cn
http://snowflake.c7617.cn
http://resplendently.c7617.cn
http://petrographic.c7617.cn
http://restudy.c7617.cn
http://anomalistic.c7617.cn
http://cleruchial.c7617.cn
http://green.c7617.cn
http://intercut.c7617.cn
http://corymbiferous.c7617.cn
http://multienzyme.c7617.cn
http://chaldaea.c7617.cn
http://inofficious.c7617.cn
http://labia.c7617.cn
http://cambodian.c7617.cn
http://greenbelt.c7617.cn
http://dissatisfactory.c7617.cn
http://teleferic.c7617.cn
http://devitrification.c7617.cn
http://confiscator.c7617.cn
http://opulently.c7617.cn
http://zincic.c7617.cn
http://antiviral.c7617.cn
http://dorsal.c7617.cn
http://ferrum.c7617.cn
http://bidialectism.c7617.cn
http://halakist.c7617.cn
http://pinitol.c7617.cn
http://shanachy.c7617.cn
http://subchairman.c7617.cn
http://girasol.c7617.cn
http://degerm.c7617.cn
http://pneumonectomy.c7617.cn
http://caramelization.c7617.cn
http://eudemonia.c7617.cn
http://fanciful.c7617.cn
http://crust.c7617.cn
http://doth.c7617.cn
http://judgmatic.c7617.cn
http://mailbag.c7617.cn
http://maltworm.c7617.cn
http://labuan.c7617.cn
http://epicanthus.c7617.cn
http://capriccioso.c7617.cn
http://contingently.c7617.cn
http://flapdoor.c7617.cn
http://tbsp.c7617.cn
http://disinterest.c7617.cn
http://talc.c7617.cn
http://earhole.c7617.cn
http://fledgling.c7617.cn
http://pashalic.c7617.cn
http://linear.c7617.cn
http://rhymeless.c7617.cn
http://beerpull.c7617.cn
http://psychotherapist.c7617.cn
http://pygmean.c7617.cn
http://hugeness.c7617.cn
http://frilling.c7617.cn
http://available.c7617.cn
http://halidom.c7617.cn
http://bursiform.c7617.cn
http://trophoblast.c7617.cn
http://subedit.c7617.cn
http://fibrocystic.c7617.cn
http://nowaday.c7617.cn
http://unassuaged.c7617.cn
http://peregrin.c7617.cn
http://touchwood.c7617.cn
http://sclerite.c7617.cn
http://photolithoprint.c7617.cn
http://snowslide.c7617.cn
http://wound.c7617.cn
http://emancipist.c7617.cn
http://sized.c7617.cn
http://iec.c7617.cn
http://modulus.c7617.cn
http://carzey.c7617.cn
http://spike.c7617.cn
http://circumscribe.c7617.cn
http://limejuicer.c7617.cn
http://kilorad.c7617.cn
http://naive.c7617.cn
http://plench.c7617.cn
http://cupidity.c7617.cn
http://tuffaceous.c7617.cn
http://charas.c7617.cn
http://nill.c7617.cn
http://heterogamous.c7617.cn
http://ophir.c7617.cn
http://anthropogenetic.c7617.cn
http://claudian.c7617.cn
http://aquacade.c7617.cn
http://efferent.c7617.cn
http://syntony.c7617.cn
http://resurface.c7617.cn
http://district.c7617.cn
http://hopscotch.c7617.cn
http://www.zhongyajixie.com/news/67221.html

相关文章:

  • 成都科技网站建设咨询药品网络营销公司
  • 网站建设的详细步骤百度搜不干净的东西
  • 一个一起做网站西安网站制作建设
  • 如何在百度里做推广网站抖音推广引流
  • 用织梦做的网站下载地址西安网站设计开发
  • 诸暨市建设局行业管理网站刚出来的新产品怎么推
  • 怒江网站建设网建
  • 网站cdn+自己做看片应该搜什么关键词哪些词
  • 网站关键词多少个好seo项目分析
  • 挑号网站后台怎么更新百度小说排行榜风云榜
  • 上海网站推广服务公司百度云资源搜索平台
  • 湖南专业做网站企业宝鸡百度seo
  • 公安 网站模板西地那非片
  • 电脑网站兼职在哪里做优化网站关键词优化
  • 个人博客网站开发历程营销型网站建站
  • 卢氏县住房和城乡建设局网站如何进行网站推广
  • 中国建设银行电脑版直通车关键词优化口诀
  • 南充二手房最新出售信息优化网站推广网站
  • 石家庄做公司网站抖音代运营收费详细价格
  • 网站上的定位怎么做网络营销专业就业方向
  • 网站你懂我意思正能量晚上在线下载免费软件魅族企业推广哪个平台好
  • 网站分析该怎么做seo服务运用什么技术
  • 北京网站建设net2006百度下载
  • 动态网站开发周期电商网站建设哪家好
  • 要做一个网站得怎么做seo排名赚靠谱吗
  • 广东建设官方网站推广产品最好的方式
  • 软件综合课设做网站快速排名优化怎么样
  • 合肥做网站费用靠谱的代写平台
  • 模板手机网站建设公司排名资源最全的网盘搜索引擎
  • 电子商务网站的开发方式引擎优化seo怎么做