The Princeton Algorithm part1--heap--notes April 20 to April 27, 2018

This week, I learned about priority queues, and the most important operations are removing the largest element and inserting an element. The class includes the constructor to create the priority queue, insert an element into the priority queue, return the largest element, delete the largest element, determine whether the queue is empty, and return the number of elements in the priority queue. When each node of a binary tree is greater than or equal to its two child nodes, it is heap-ordered, then the root node is the largest node in the heap-ordered binary tree. Using a binary heap allows you to use arrays instead of pointers.

 

Implementation : Unordered array: Use selection sort inner loop to remove largest element, but making it ordered makes subsequent removals more efficient, but when ordered, insertion takes N in worst case . Sorted array: stack-like pop () operation.

 

Algorithms about heaps: If the ordered state of the heap is broken because a node becomes larger than its parent, then swap it with its parent, and so on. If the ordered state of the heap is because a node becomes smaller than one or both of its two children, swap it with the larger of its children. If you want to insert a new element, you can insert it to the root node and then sink it down. If you want to delete the root node, replace the position of the root node with the last element, and then sink

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325042080&siteId=291194637