插入
Insertion means adding an element to the Heap. After inserting the element, the properties of the Heap should remain unchanged.
方法 (适用于min-heap):
- 先证明是Complete binary tree
- 再证明 value of each node <= values of its children
- 前两步通过了,才insert value, 插在末尾,插入的顺序是from left to right
- 通过调换位置来符合heap的特性
方法 (适用于max-heap)
- 先证明是Complete binary tree
- 再证明 value of each node >= values of its children
- 前两步通过了,才insert value, 插在末尾,插入的顺序是from left to right
- 通过调换位置来符合heap的特性
删除
Deletion means removing the “top” element from the Heap. After deleting the element, the property of Heap should remain unchanged.
方法 (适用于 min-heap)
- 先证明是Complete binary tree
- 再证明 value of each node <= values of its children
- 前两步通过,删除root node, 将最底部的最右边 (有left leave前提下)的值移到root node
- 通过调换位置来符合min-heap的特性
方法 (适用于 max-heap)
- 先证明是Complete binary tree
- 再证明 value of each node >= values of its children
- 前两步通过,删除root node, 将最底部的最右边 (有left leave前提下)的值移到root node
- 通过调换位置来符合min-heap的特性