1. Trang chủ
  2. » Tất cả

Cấu trúc dữ liệu và thuật toán dsa ch9 heaps

44 29 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

Heaps Dr Rang Nguyen Chapter Heap Definition Heaps Heap Structure Data Structures and Algorithms Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Dr Rang Nguyen Faculty of Computer Science and Engineering University of Technology, VNU-HCM Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues 9.1 Outcomes Heaps Dr Rang Nguyen • L.O.4.1 - List some applications of Heap • L.O.4.2 - Depict heap structure and relate it to array • L.O.4.3 - List necessary methods supplied for heap structure, and describe them using pseudocode • L.O.4.4 - Depict the working steps of methods that maintain the characteristics of heap structure for the cases of adding/removing elements to/from heap Heap Definition Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues 9.2 Outcomes Heaps Dr Rang Nguyen • L.O.4.5 - Implement heap using C/C++ • L.O.4.6 - Analyze the complexity and develop experiment (program) to evaluate methods supplied for heap structures • L.O.8.4 - Develop recursive implementations for methods supplied for the following structures: list, tree, heap, searching, and graphs • L.O.1.2 - Analyze algorithms and use Big-O notation to characterize the computational complexity of algorithms composed by using the following control structures: sequence, branching, and iteration (not recursion) Heap Definition Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues 9.3 Contents Heap Definition Heaps Dr Rang Nguyen Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Definition Heap Structure Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues Heap Applications Selection Algorithms Priority Queues 9.4 Heaps Dr Rang Nguyen Heap Definition Heap Definition Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues 9.5 Heap Definition Definition Heaps Dr Rang Nguyen A heap (max-heap) is a binary tree structure with the following properties: The tree is complete or nearly complete The key value of each node is greater than or equal to the key value in each of its descendents Heap Definition Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues (Source: Data Structures - A Pseudocode Approach with C++) 9.6 Heap Definition Definition Heaps Dr Rang Nguyen A min-heap is a binary tree structure with the following properties: The tree is complete or nearly complete The key value of each node is less than or equal to the key value in each of its descendents Heap Definition Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues (Source: Data Structures - A Pseudocode Approach with C++) 9.7 Heaps Dr Rang Nguyen Heap Definition Heap Structure Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues 9.8 Heap trees Heaps Dr Rang Nguyen Heap Definition Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues 9.9 Invalid Heaps Heaps Dr Rang Nguyen Heap Definition Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues (Source: Data Structures - A Pseudocode Approach with C++) 9.10 Delete a Node from a Heap Heaps Dr Rang Nguyen • When deleting a node from a heap, the most common and meaningful logic is to delete the root • After it has been deleted, the heap is thus left without a root • To reestablish the heap, we move the data in the last heap node to the root and reheap down Heap Definition Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues 9.30 Delete a Node from a Heap Heaps Dr Rang Nguyen Heap Definition Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues (Source: Data Structures - A Pseudocode Approach with C++) 9.31 Delete a Node from a Heap Algorithm deleteHeap(ref heap , ref last , ref dataOut ) Deletes root of heap and passes data back to caller Pre: heap is a valid heap structure last is reference parameter to last node dataOut is reference parameter for output data Heaps Dr Rang Nguyen Heap Definition Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Post: root deleted and heap rebuilt root data placed in dataOut Delete a Node Heap Applications Selection Algorithms Priority Queues 9.32 Delete a Node from a Heap Heaps Dr Rang Nguyen if heap empty then return false end dataOut = heap[0] heap[0] = heap[last] last = last - reheapDown(heap, 0, last) return true End deleteHeap Heap Definition Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues 9.33 Complexity of Binary Heap Operations Heaps Dr Rang Nguyen • ReheapUp: O(log2 n) Heap Definition • ReheapDown: O(log2 n) Heap Structure Basic Heap Algorithms • Build a Heap: O(n log2 n) • Insert a Node into a Heap: O(log2 n) ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown • Delete a Node from a Heap: O(log2 n) Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues 9.34 Heaps Dr Rang Nguyen Heap Definition Heap Applications Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues 9.35 Heap Applications Heaps Dr Rang Nguyen Three common applications of heaps are: selection algorithms, priority queues, and sorting Heap Definition Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure We discuss heap sorting in Chapter 10 and selection algorithms and priority queues here Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues 9.36 Selection Algorithms Heaps Dr Rang Nguyen Problem Determining the k th element in an unsorted list Two solutions: Sort the list and select the element at location k The complexity of a simple sorting algorithm is O(n2 ) Create a heap and delete k − elements from the heap, leaving the desired element at the top The complexity is O(n log2 n) Heap Definition Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp Rather than simply discarding the elements at the top of the heap, a better solution would be to place the deleted element at the end of the heap and reduce the heap size by ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms k th After the element has been processed, the temporarily removed elements can then be inserted into the heap Priority Queues 9.37 Selection Algorithms Heaps Dr Rang Nguyen Heap Definition Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues (Source: Data Structures - A Pseudocode Approach with C++) 9.38 Selection Algorithms Heaps Dr Rang Nguyen Algorithm selectK(ref heap , ref k , ref last ) Select the k-th largest element from a list Heap Definition Heap Structure Pre: heap is an array implementation of a heap k is the ordinal of the element desired last is reference parameter to last element Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Post: k-th largest value returned Delete a Node Heap Applications Selection Algorithms Priority Queues 9.39 Selection Algorithms Heaps Dr Rang Nguyen if k > last + then return end i=1 originalSize = last + while i < k temp = heap[0] deleteHeap(heap, last, dataOut) heap[last + 1] = temp i=i+1 end Heap Definition Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues 9.40 Selection Algorithms Heaps Dr Rang Nguyen // Desired element is now at top of heap holdOut = heap[0] Heap Definition // Reconstruct heap while last < originalSize last = last + reheapUp(heap, last) end return holdOut End selectK Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues 9.41 Priority Queues The heap is an excellent structure to use for a priority queue Heaps Dr Rang Nguyen Example Assume that we have a priority queue with three priorities: high (3), medium (2), and low (1) Of the first five customers who arrive, the second and the fifth are high-priority customers, the third is medium priority, and the first and the fourth are low priority Heap Definition Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues (Source: Data Structures - A Pseudocode Approach with C++) 9.42 Priority Queues Heaps Dr Rang Nguyen The customers are served according to their priority and within equal priorities, according to their arrival Thus we see that customer (3998) is served first, followed by customer (3995), customer (2997), customer (1999), and customer (1996) Heap Definition Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node Heap Applications Selection Algorithms (Source: Data Structures - A Pseudocode Approach with C++) Priority Queues 9.43 Priority Queues Heaps Dr Rang Nguyen Heap Definition Heap Structure Basic Heap Algorithms ReheapUp ReheapDown Heap Data Structure Heap Algorithms ReheapUp ReheapDown Build a Heap Insert a Node Delete a Node (Source: Data Structures - A Pseudocode Approach with C++) Heap Applications Selection Algorithms Priority Queues 9.44 ... Insert a Node Delete a Node Heap Applications Selection Algorithms Priority Queues 9.9 Invalid Heaps Heaps Dr Rang Nguyen Heap Definition Heap Structure Basic Heap Algorithms ReheapUp ReheapDown... Node Delete a Node Heap Applications Selection Algorithms Priority Queues 9.14 Properties of Heaps Heaps Dr Rang Nguyen • A complete or nearly complete binary tree • If the height is h, the number... Applications Selection Algorithms Priority Queues 9.35 Heap Applications Heaps Dr Rang Nguyen Three common applications of heaps are: selection algorithms, priority queues, and sorting Heap Definition

Ngày đăng: 25/03/2023, 07:21

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w