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的特性
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值