西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)04 Priority Queues(Heaps)

General Idea Example:Suppose there are a sequence of jobs are sent to a printer.Although jobs sent to a printer are generally placed on a queue,this might not always be the best thing to do. For instance,if,when the printer becomes available, there are several 1-page jobs and one 100-page job, it might be reasonable to make the long job go last, even if it is not the last job submitted. This particular application seems to require a special kind of queue,known as a priority queue
General Idea ◼ Example: Suppose there are a sequence of jobs are sent to a printer. Although jobs sent to a printer are generally placed on a queue, this might not always be the best thing to do. ◼ For instance, if, when the printer becomes available, there are several 1-page jobs and one 100-page job, it might be reasonable to make the long job go last, even if it is not the last job submitted. ◼ This particular application seems to require a special kind of queue, known as a priority queue

Model A priority gueue is a data structure that allows at least the following two operations: (1)Insert:is the equivalent of Engueue (2)DeleteMin:finds,returns,and removes the minimum element in the priority queue. DeleteMin(H) Insert(H) Priority Queue H
Model ◼ A priority queue is a data structure that allows at least the following two operations: ◼ (1) Insert: is the equivalent of Enqueue ◼ (2) DeleteMin: finds, returns, and removes the minimum element in the priority queue. Priority Queue H DeleteMin(H) Insert(H)

Simple Implementations There are several obvious ways to implement a priority queue.We could use a simple linked list performing insertions at the front and traversing the list to delete the minimum. Alternatively,we could insist that the list be kept always sorted. Another way of implementing priority queues would be to use a binary search tree.Recall that the only element we ever delete is the minimum.Repeatedly removing a node that is in the left subtree would seem to hurt the balance of the tree by making the right subtree heavy
Simple Implementations ◼ There are several obvious ways to implement a priority queue. We could use a simple linked list, performing insertions at the front and traversing the list to delete the minimum. ◼ Alternatively, we could insist that the list be kept always sorted. ◼ Another way of implementing priority queues would be to use a binary search tree. Recall that the only element we ever delete is the minimum. Repeatedly removing a node that is in the left subtree would seem to hurt the balance of the tree by making the right subtree heavy

Binary Heap The implementation we will use is known as a binary heap. Like binary search trees,binary heaps have two properties,namely,a structure property and a heap order property
Binary Heap ◼ The implementation we will use is known as a binary heap. ◼ Like binary search trees, binary heaps have two properties, namely, a structure property and a heap order property

Structure Property Structure property:A heap is a binary tree that is completely filled,with the possible exception of the bottom level,which is filled from left to right.Such a tree is known as a complete binary tree. B E
Structure Property ◼ Structure property: A heap is a binary tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right. Such a tree is known as a complete binary tree. D E B C A F G H I J

Structure Property An important observation is that because a complete binary tree is so regular,it can be represented in an array and no pointers are necessary. a For any element in array position /the left child is in position 2/the right child is in the cell after that left child (24+1),and the parent is in position L//2
Structure Property ◼ An important observation is that because a complete binary tree is so regular, it can be represented in an array and no pointers are necessary. ◼ For any element in array position i, the left child is in position 2i, the right child is in the cell after that left child (2i+1), and the parent is in position i/2

Structure Property A B E G A B CDE F GH I J 012345678910111213
Structure Property A B C D E F G H I J 0 1 2 3 4 5 6 7 8 9 10 11 12 13 D E B C A F G H I J

Structure Property Thus,not only are pointers not required,but the operations required to traverse the tree are extremely simple and likely to be very fast on most computers. The only problem with this implementation is that an estimate of the maximum heap size is required in advance. A heap data structure will then consist of an array (of whatever type the key is)and an integer representing the maximum and current heap sizes. Figure 6.4 shows a typical priority queue declaration
Structure Property ◼ Thus, not only are pointers not required, but the operations required to traverse the tree are extremely simple and likely to be very fast on most computers. ◼ The only problem with this implementation is that an estimate of the maximum heap size is required in advance. ◼ A heap data structure will then consist of an array (of whatever type the key is) and an integer representing the maximum and current heap sizes. ◼ Figure 6.4 shows a typical priority queue declaration

Structure Property Struct HeapStruct { int Capacity; int Size; elementType *Elements; Initialize(int MaxElements) { Line 3:H=malloc(sizeof(struct HeapStruct)); Line 6:H->Elements= malloc((MaxElements+1)*sizeof(ElementType)); }
Structure Property Struct HeapStruct { int Capacity; int Size; elementType *Elements; } Initialize(int MaxElements) { Line 3: H=malloc(sizeof(struct HeapStruct)); Line 6: H->Elements= malloc((MaxElements+1)*sizeof(ElementType)); }

Heap Order Property The property that allows operations to be performed quickly is the heap order property. Since we want to be able to find the minimum quickly, it makes sense that the smallest element should be at the root. If we consider that any subtree should also be a heap, then any node should be smaller than all of its descendants
Heap Order Property ◼ The property that allows operations to be performed quickly is the heap order property. ◼ Since we want to be able to find the minimum quickly, it makes sense that the smallest element should be at the root. ◼ If we consider that any subtree should also be a heap, then any node should be smaller than all of its descendants
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)03 Hashing.ppt
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)08 Algorithm Design Techniques.ppt
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)07 Graph Algorithms.ppt
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)06 Algorithm Analysis and Sorting.ppt
- 西安电子科技大学:《算法设计技术 Algorithms Design Techniques》课程教学资源(PPT课件讲稿)Iterative Improvement for Domain-Specific Problems(Chapter 16 Network Flow Chapter 17 Matching).ppt
- 西安电子科技大学:《算法设计技术 Algorithms Design Techniques》课程教学资源(PPT课件讲稿)Coping with Hardness(Chapter 13 Backtracking Chapter 14 Randomized Algorithms Chapter 15 Approximation Algorithms).ppt
- 西安电子科技大学:《算法设计技术 Algorithms Design Techniques》课程教学资源(PPT课件讲稿)First-Cut Techniques(Chapter 8 The Greedy Approach Chapter 9 Graph Traversal).ppt
- 西安电子科技大学:《算法设计技术 Algorithms Design Techniques》课程教学资源(PPT课件讲稿)Techniques Based on Recursion(Chapter 5 Induction Chapter 6 Divide and Conquer Chapter 7 Dynamic Programming).ppt
- 西安电子科技大学:《算法设计技术 Algorithms Design Techniques》课程教学资源(PPT课件讲稿)Chapter 01 Basic Concepts in Algorithmic Analysis(主讲:刘静).ppt
- 《计算机基础》课程教学资源(参考论文)Motif Difficulty(MD):A Predictive Measure of Problem Difficulty for Evolutionary Algorithms using Network Motifs.pdf
- 《计算机基础》课程教学资源(参考论文)An Organizational Evolutionary Algorithm for Numerical Optimization.pdf
- 《计算机基础》课程教学资源(参考论文)A Multiagent Evolutionary Algorithm for Constraint Satisfaction Problems.pdf
- 《计算机基础》课程教学资源(参考论文)Comments on “the 1993 DIMACS Graph Coloring Challenge” and “Energy Function-Based Approaches to Graph Coloring”.pdf
- 《计算机基础》课程教学资源(参考论文)Moving Block Sequence and Organizational Evolutionary Algorithm for General Floorplanning with Arbitrarily Shaped Rectilinear Blocks.pdf
- 《计算机基础》课程教学资源(参考论文)A Multi-Agent Genetic Algorithm for Global Numerical Optimization.pdf
- 《计算机基础》课程教学资源(参考论文)An Organizational Coevolutionary Algorithm for Classification.pdf
- 西安电子科技大学:《大学计算机基础 Fundamentals of Computers》课程教学资源(PPT课件讲稿)Chapter 12,14,15 Lecture_Computer Software(2/2).ppt
- 西安电子科技大学:《大学计算机基础 Fundamentals of Computers》课程教学资源(PPT课件讲稿)Chapter 10-11 Lecture_Computer Software(1/2).ppt
- 西安电子科技大学:《大学计算机基础 Fundamentals of Computers》课程教学资源(PPT课件讲稿)Chapter 1-5 Lecture_Computer Hardware(主讲:刘静).ppt
- 西安电子科技大学:《大学计算机基础 Fundamentals of Computers》课程教学资源(PPT课件讲稿)Chapter 6-8 Lecture_Computer Codes.ppt
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)05 Disjoint Set ADT.ppt
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)02 Trees(主讲:刘静).ppt
- 西安电子科技大学:《数据结构与算法分析 Data Structure and Algorithm Analysis》课程教学资源(PPT课件讲稿)01 Lists, Stacks, and Queues.ppt
- 中国科学技术大学:Robust Speech Recognition with Speech Enhanced Deep Neural Networks.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第九章 机器无关的优化.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第八章 代码生成.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第七章 运行时刻环境.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第六章 中间代码生成.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第五章 语法制导的翻译.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第四章 语法分析.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第三章 词法分析.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)第一章 引论(许畅).pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(课件讲义)编译课程复习(许畅).pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(实验讲义)实验一 词法分析与语法分析.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(实验讲义)实验二 语义分析.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(实验讲义)实验三 中间代码生成.pdf
- 南京大学:《编译原理 Principles and Techniques of Compilers》课程教学电子教案(实验讲义)实验四 目标代码生成.pdf
- 《编译原理 Principles and Techniques of Compilers》课程教学资源(学习资料)Assemblers,Linkers,and the SPIM Simulator(MIPS32 and SPIM).pdf
- 南京大学:《软件工程导论 Introduction to Software Engineering Research》课程教学电子教案(课件讲义)04 Conduct Rigorous and Scientific Research(Experiment Design in Software Engineering Research).pdf
- 华中科技大学:《面向对象程序设计》课程教学资源(课件讲稿)课程简介(李瑞轩).pdf