Points streamline data structures and algorithms of the heap summary

stack

Priority Queue

  • Normal queue: FIFO; backward after the
  • Queue Priority: order independent enqueue and dequeue sequence; and priority related

achieve

Life in general use binary heap, and the heap is a complete binary tree, and the heap can be divided into a maximum heap, pile minimum, maximum heap index, index the minimum heap. The maximum (minimum) to delete the heap stack has a top node and the node is inserted into two functions, the two functions in addition to the above, the maximum (minimum) index stack itself can modify the characteristics of the node.

  1. Stack of: since the conditions of complete binary heap to meet, so the new node to be inserted one after the next to the node from the stack after the last bit of the bottom of the heap, as is generally than directly with the parent node, then the switch is greater than until no exchange, so that the maximum heap (minimum heap) the conditions are met.
  2. Delete the top of the heap node: The node top of the heap is about to come up with a maximum (minimum) value to come up with is to use heap characteristics important step generally node out of the top of the heap after, the end of the last one heap nodes fill the seats, and then from the heap top of the heap from top to bottom, can guarantee the properties of a complete binary tree, and can ensure the parent element children of the element is greater than (less than).
  3. Index reactor: Because of the different forms of memory occupied by different elements, if only the index of the stack forms, memory operation is reduced, to avoid a large number of memory read and write operations, the index file by the: index array, the elements, the array index trans composition. Only the elements of the array elements for storing, embodied in the form of stacks indexed array, i.e., only for the process stack array index, both of which can be guaranteed by indexing elements can be modified, while maintaining the characteristics of the maximum (minimum) stack. Anti-indexed array element after the modification for the elements in the array, obtaining the position of the element index in the stack (array index), thereby performing stack operations to ensure its characteristics stack.

Binary heap

An array storing binary heap

//从1开始索引
parent (i) = i/2
left child (i) = 2*i
right child (i) = 2*i+1

//从0开始索引
parent (i) = (i-1)/2
left child (i) = 2*i+1
right child (i) = 2*i+2

Performance Comparison

The team The team
Ordinary arrays O (1) O (n)
Order of the array O (n) O (1)
stack O (lgn) O (lgn)

Heapsort

Heap sort is the sort algorithm to create a data structure on the stack, the algorithm requires the original of the stack array, and then take the top of the stack elements sequentially (maximum or minimum) for the purpose of sorting. In situ is more classic heap sort, the top of stack operation, and speaks removed directly exchange element transducer element top of the stack and the last one end of the element stack, and the stack operation performed from top to bottom. Therefore heap sort operation time + = sorting operation, is n n times enqueue and dequeue operations, the time complexity is O ( n l O g n ) O (nlogn) .

Published 31 original articles · won praise 42 · views 10000 +

Guess you like

Origin blog.csdn.net/Flame_alone/article/details/104253957
Recommended