> For the complete documentation index, see [llms.txt](https://garychang.gitbook.io/data-structure/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://garychang.gitbook.io/data-structure/lecture2-tree-and-binary-tree/lecture-2.8-advanced-trees/lecture2.8.2-deap.md).

# 2.8.2 - Deap

Deap(Double-Ended Heap)

### 1. 定義

Deap是一個Complete Binary Tree且滿足:

1. Root為空(不存Data)
2. Root的左子樹為min-heap
3. Root的右子樹為max-heap
4. 令i為min-heap的一個節點編號，j為max-heap的一個節點編號，則Deap\[i] ≤ Deap\[j]

### 2. 運作

#### 2.1 Insert x in a deap

1. 將x放到最後一個節點的下一個位置
2. &#x20;
   1. x位於min-heap中: 令j是max-heap的對應節點；若x>Deap\[j]，兩節點交換，接著調整min-heap使其不違反定義。反之，則調整max-heap
   2. x位於max-heap中: 令j是min-heap的對應節點；若x\<Deap\[j]，兩截點交換，接著調整max-heap使其不違反定義。反之，則調整min-heap

#### 2.2 Delete min in a deap

1. 取出root左子點的值
2. 將最後一個節點x刪除
3. Root的左子點空缺由其子點的最小值往上遞補，直到空缺在leaf，其位置為i
4. insert(x, i)

![](https://2769815795-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGckN3OAfinKuVIrkMj%2F-LHloCRM-0Osy9ASxjvG%2F-LHloFENSr0E5I6QNGyU%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-07-19%2017.11.22.jpg?alt=media\&token=c43ccd26-b95e-479d-bbe3-6d60e0cfb596)
