Heap 的讲解 - 插入和删除

插入
Insertion means adding an element to the Heap. After inserting the element, the properties of the Heap should remain unchanged.

方法 (适用于min-heap):

  1. 先证明是Complete binary tree
  2. 再证明 value of each node <= values of its children
  3. 前两步通过了,才insert value, 插在末尾,插入的顺序是from left to right
  4. 通过调换位置来符合heap的特性

方法 (适用于max-heap)

  1. 先证明是Complete binary tree
  2. 再证明 value of each node >= values of its children
  3. 前两步通过了,才insert value, 插在末尾,插入的顺序是from left to right
  4. 通过调换位置来符合heap的特性

删除
Deletion means removing the “top” element from the Heap. After deleting the element, the property of Heap should remain unchanged.

方法 (适用于 min-heap)

  1. 先证明是Complete binary tree
  2. 再证明 value of each node <= values of its children
  3. 前两步通过,删除root node, 将最底部的最右边 (有left leave前提下)的值移到root node
  4. 通过调换位置来符合min-heap的特性

方法 (适用于 max-heap)

  1. 先证明是Complete binary tree
  2. 再证明 value of each node >= values of its children
  3. 前两步通过,删除root node, 将最底部的最右边 (有left leave前提下)的值移到root node
  4. 通过调换位置来符合min-heap的特性

おすすめ

転載: blog.csdn.net/BSCHN123/article/details/121457485