java的堆_java数据结构----堆

1.堆:堆是一种树,由它实现的优先级队列的插入和删除的时间复杂度都是O(logn),用堆实现的优先级队列虽然和数组实现相比较删除慢了些,但插入的时间快的多了。当速度很重要且有很多插入操作时,可以选择堆来实现优先级队列。

2.java的堆和数据结构堆:java的堆是程序员用new能得到的计算机内存的可用部分。而数据结构的堆是一种特殊的二叉树。

3.堆是具有如下特点的二叉树:

3.1.它是完全二叉树,也就是说除了树的最后一层节点不需要是满的,其他的每一层从左到右都必须是满的。

3.1.1.完全二叉树图解:

8c933e7425bb6f331e6c9bfabbe8886d.png

3.2.它常常用数组实现。

3.2.1.数组和堆的对应关系示意图:

790f10b14ade9692c142c054edabdc15.png

3.3.堆中每一个节点都满足堆的条件,也就是说每一个关键字的值都大于或等于这个节点的子节点的关键字值。

堆是完全二叉树的事实说明了表示堆的数组中没有空项,即从0-->n-1的每个数据单元都有数据项。

4.堆在存储器中的表示是数组,堆只是一个概念上的表示。

5.堆的弱序:堆和二叉搜索树相比是弱序的,在二叉搜索树中,当前节点的值总是比左子节点的值大,却比它的右子节点的值小,因此按序遍历相对容易。而堆的组织规则弱,它只要求从根到叶子节点的每一条路径,节点都是按降序排列的。同一节点的左右子节点都没有规律。因此,堆不支持按序遍历,也不能在堆上便利的查找指定关键字,因为在查找的过程中,没有足够的信息决定选择通过节点的两个那一个走向下一层。它也不能在少于O(logn)的时间内删除一个指定的节点,因为没有办法找到这个节点。因此,堆的这种近乎无序的规则似乎毫无用处,不过对于快速移除最大节点的操作,以及快速插入新节点的操作,这种顺序已经足够了。这些操作是使用堆作为优先级队列所需要的全部操作。

6.移除操作:移除是指删掉关键字值最大的节点,即根节点。移除思路如下:

6.1.移走根,

6.2.把左后一个节点移到根的位置,

6.3.一直向下筛选这个节点,知道它在一个大于它的节点之下,小于它的节点之上为止。

6.4.过程图解:

a15b3acdeffdee04d720760af5525a37.png

说明:在被筛选节点的每个暂时停留的位置,向下筛选的算法总是要检查那一个子节点更大,然后目标节点和较大的子节点交换位置,如果要把目标节点和较小的子节点交换,那么这个子节点就会变成大子节点的父节点,这就违背了堆的条件。

7.堆的插入:插入使用向上筛选,节点最后插入到数组最后第一个空着的单元中,数组容量大小增加1。

7.1.插入图解:

f117c5ed114f5a3b6d315dbf794b071c.png

说明:向上筛选的算法比向下筛选的算法相对简单,因为它不需要比较两个子节点关键字值的大小,节点只有一个父节点。目标节点主要和它的父亲节点换位即可。

7.2.不是真的交换:

b5529e977c383b7677ba274f52bd6f4c.png

8.用数组表示一棵树时,如果数组中节点的索引位x,则

a.它的父节点的下标是:(x-1)/2;

b.它的左子节点的下标为2*x + 1;

c.它的右子节点的下标是2*x + 2;

9.堆的代码:

9.1.Node.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.cn.heap;2 /**

3 * 堆的节点类4 *@authorAdministrator5 *6 */

7 public classNode {8 private intiData;9 public Node(intid){10 iData =id;11 }12 public intgetkey(){13 returniData;14 }15 public void setkey(intid){16 iData =id;17 }18 }

View Code

9.2.Heap.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.cn.heap;2 /**

3 * 堆的实现类4 *@authorAdministrator5 *6 */

7 public classHeap {8 privateNode[] heapArray;9 private intmaxSize;10 private intcurrentSize;11 public Heap(intmx){12 maxSize =mx;13 heapArray = newNode[maxSize];14 currentSize = 0;15 }16 public booleanisEmpty(){17 return currentSize == 0;18 }19 public boolean insert(intkey){20 if (currentSize ==maxSize)21 return false;22 Node thenode = newNode(key);23 heapArray[currentSize] =thenode;24 trickleUp(currentSize ++);25 return true;26 }27 public void trickleUp(intindex){28 int parent = (index - 1) / 2;29 Node bottom =heapArray[index];30 while (index > 0 && heapArray[parent].getkey()

52 largeChild =leftChild;53 if (top.getkey() >=heapArray[largeChild].getkey())54 break;55 heapArray[index] =heapArray[largeChild];56 index =largeChild;57 }58 heapArray[index] =top;59 }60 public boolean change(int index,intnewvalue){61 if (index < 0 || index >=currentSize)62 return false;63 int oldvalue =heapArray[index].getkey();64 heapArray[index].setkey(newvalue);65 if (oldvalue

68 trickleDown(index);69 return true;70 }71 public voiddisplayHeap(){72 System.out.print("heapArray:");73 for (int i = 0; i < currentSize; i++) {74 if (heapArray[i] != null)75 System.out.print(heapArray[i].getkey()+" ");76 else

77 System.out.print("--");78 }79 System.out.println("");80 int nBlanks = 32;81 int itemsPerrow = 1;82 int column = 0;83 int j = 0;84 String dots = "........................";85 System.out.println(dots +dots);86 while (currentSize > 0){87 if (column == 0)88 for (int i = 0; i < nBlanks; i++) {89 System.out.print(" ");90 }91 System.out.print(heapArray[j].getkey());92 if (++ j ==currentSize)93 break;94 if (++ column ==itemsPerrow){95 nBlanks /= 2;96 itemsPerrow *= 2;97 column = 0;98 System.out.println();99 }100 else

101 for (int i = 0; i < nBlanks * 2 - 2; i++)102 System.out.print(' ');103 }104 System.out.println("\n"+dots +dots);105 }106 }

View Code

9.3.HTest.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.cn.heap;2 /**

3 * heap类的测试4 *@authorAdministrator5 *6 */

7 public classHTest {8 public static voidmain(String[] args) {9 Heap h = new Heap(10);10 h.insert(10);11 h.insert(30);12 h.insert(20);13 h.insert(18);14 h.insert(12);15 h.displayHeap();16 h.remove();17 h.displayHeap();18 }19 }

View Code

10.堆的效率:上述操作的时间复杂度是:O(logn)。

11.堆排序实现思路:使用insert()向堆中插入所有无序的数据项,然后重复使用remove()方法,就可以按序移除所有数据项,它的效率和快速排序类似,都是O(NlogN),但快排稍微快些,因为堆插入时的向下筛选多出的比较所占用的时间。

11.1.Node.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.cn.heap;2 /**

3 * 堆的节点类4 *@authorAdministrator5 *6 */

7 public classNode {8 private intiData;9 public Node(intid){10 iData =id;11 }12 public intgetkey(){13 returniData;14 }15 public void setkey(intid){16 iData =id;17 }18 }

View Code

11.2.Heap.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.cn.heap;2 /**

3 * 堆的实现类4 *@authorAdministrator5 *6 */

7 public classHeap {8 privateNode[] heapArray;9 private intmaxSize;10 private intcurrentSize;11 public Heap(intmx){12 maxSize =mx;13 heapArray = newNode[maxSize];14 currentSize = 0;15 }16 public booleanisEmpty(){17 return currentSize == 0;18 }19 public boolean insert(intkey){20 if (currentSize ==maxSize)21 return false;22 Node thenode = newNode(key);23 heapArray[currentSize] =thenode;24 trickleUp(currentSize ++);25 return true;26 }27 public void trickleUp(intindex){28 int parent = (index - 1) / 2;29 Node bottom =heapArray[index];30 while (index > 0 && heapArray[parent].getkey()

52 largeChild =leftChild;53 if (top.getkey() >=heapArray[largeChild].getkey())54 break;55 heapArray[index] =heapArray[largeChild];56 index =largeChild;57 }58 heapArray[index] =top;59 }60 public boolean change(int index,intnewvalue){61 if (index < 0 || index >=currentSize)62 return false;63 int oldvalue =heapArray[index].getkey();64 heapArray[index].setkey(newvalue);65 if (oldvalue

68 trickleDown(index);69 return true;70 }71 public voiddisplayHeap(){72 System.out.print("heapArray:");73 for (int i = 0; i < currentSize; i++) {74 if (heapArray[i] != null)75 System.out.print(heapArray[i].getkey()+" ");76 else

77 System.out.print("--");78 }79 System.out.println("");80 int nBlanks = 32;81 int itemsPerrow = 1;82 int column = 0;83 int j = 0;84 String dots = "........................";85 System.out.println(dots +dots);86 while (currentSize > 0){87 if (column == 0)88 for (int i = 0; i < nBlanks; i++) {89 System.out.print(" ");90 }91 System.out.print(heapArray[j].getkey());92 if (++ j ==currentSize)93 break;94 if (++ column ==itemsPerrow){95 nBlanks /= 2;96 itemsPerrow *= 2;97 column = 0;98 System.out.println();99 }100 else

101 for (int i = 0; i < nBlanks * 2 - 2; i++)102 System.out.print(' ');103 }104 System.out.println("\n"+dots +dots);105 }106 public voiddisplayArray(){107 for (int i = 0; i < maxSize; i++)108 System.out.print(heapArray[i].getkey()+" ");109 System.out.println();110 }111 public void insertAt(intindex,Node newnode){112 heapArray[index] =newnode;113 }114 public voidincrementSize(){115 currentSize ++;116 }117 }

View Code

11.3.HeapSort.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.cn.heap;2

3 importjava.util.Scanner;4

5 /**

6 * 基于堆的排序----堆排序7 *@authorAdministrator8 *9 */

10 public classHeapSort {11 public static voidmain(String[] args) {12 intsize,j;13 Scanner in = newScanner(System.in);14 System.out.print("Enter number of items: ");15 size =in.nextInt();16 Heap theheap = newHeap(size);17 for (int i = 0; i < size; i++) {18 int random = (int)(Math.random()*100);19 Node node = newNode(random);20 theheap.insertAt(i, node);21 theheap.incrementSize();22 }23 System.out.print("random: ");24 theheap.displayArray();25 for (int i = size / 2 - 1; i >= 0; i --) {26 theheap.trickleDown(i);27 }28 System.out.print("heap: ");29 theheap.displayArray();30 theheap.displayHeap();31 for (int i = size - 1; i >= 0; i --) {32 Node node =theheap.remove();33 theheap.insertAt(i,node);34 }35 System.out.print("sorted: ");36 theheap.displayArray();37 }38 }

View Code

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值