This commit is contained in:
krahets
2024-04-28 22:35:59 +08:00
parent f986ae3c8c
commit f748af6aa4
34 changed files with 588 additions and 136 deletions
+67 -9
View File
@@ -676,11 +676,20 @@ comments: true
=== "Ruby"
```ruby title="my_heap.rb"
[class]{MaxHeap}-[func]{left}
### 獲取左子節點的索引 ###
def left(i)
2 * i + 1
end
[class]{MaxHeap}-[func]{right}
### 獲取右子節點的索引 ###
def right(i)
2 * i + 2
end
[class]{MaxHeap}-[func]{parent}
### 獲取父節點的索引 ###
def parent(i)
(i - 1) / 2 # 向下整除
end
```
=== "Zig"
@@ -817,7 +826,10 @@ comments: true
=== "Ruby"
```ruby title="my_heap.rb"
[class]{MaxHeap}-[func]{peek}
### 訪問堆積頂元素 ###
def peek
@max_heap[0]
end
```
=== "Zig"
@@ -1211,9 +1223,27 @@ comments: true
=== "Ruby"
```ruby title="my_heap.rb"
[class]{MaxHeap}-[func]{push}
### 元素入堆積 ###
def push(val)
# 新增節點
@max_heap << val
# 從底至頂堆積化
sift_up(size - 1)
end
[class]{MaxHeap}-[func]{sift_up}
### 從節點 i 開始,從底至頂堆積化 ###
def sift_up(i)
loop do
# 獲取節點 i 的父節點
p = parent(i)
# 當“越過根節點”或“節點無須修復”時,結束堆積化
break if p < 0 || @max_heap[i] <= @max_heap[p]
# 交換兩節點
swap(i, p)
# 迴圈向上堆積化
i = p
end
end
```
=== "Zig"
@@ -1649,7 +1679,7 @@ comments: true
// 交換根節點與最右葉節點(交換首元素與尾元素)
self.swap(0, self.size() - 1);
// 刪除節點
let val = self.max_heap.remove(self.size() - 1);
let val = self.max_heap.pop().unwrap();
// 從頂至底堆積化
self.sift_down(0);
// 返回堆積頂元素
@@ -1767,9 +1797,37 @@ comments: true
=== "Ruby"
```ruby title="my_heap.rb"
[class]{MaxHeap}-[func]{pop}
### 元素出堆積 ###
def pop
# 判空處理
raise IndexError, "堆積為空" if is_empty?
# 交換根節點與最右葉節點(交換首元素與尾元素)
swap(0, size - 1)
# 刪除節點
val = @max_heap.pop
# 從頂至底堆積化
sift_down(0)
# 返回堆積頂元素
val
end
[class]{MaxHeap}-[func]{sift_down}
### 從節點 i 開始,從頂至底堆積化 ###
def sift_down(i)
loop do
# 判斷節點 i, l, r 中值最大的節點,記為 ma
l, r, ma = left(i), right(i), i
ma = l if l < size && @max_heap[l] > @max_heap[ma]
ma = r if r < size && @max_heap[r] > @max_heap[ma]
# 若節點 i 最大或索引 l, r 越界,則無須繼續堆積化,跳出
break if ma == i
# 交換兩節點
swap(i, ma)
# 迴圈向下堆積化
i = ma
end
end
```
=== "Zig"
+22 -1
View File
@@ -437,7 +437,28 @@ comments: true
=== "Ruby"
```ruby title="top_k.rb"
[class]{}-[func]{top_k_heap}
### 基於堆積查詢陣列中最大的 k 個元素 ###
def top_k_heap(nums, k)
# 初始化小頂堆積
# 請注意:我們將堆積中所有元素取反,從而用大頂堆積來模擬小頂堆積
max_heap = MaxHeap.new([])
# 將陣列的前 k 個元素入堆積
for i in 0...k
push_min_heap(max_heap, nums[i])
end
# 從第 k+1 個元素開始,保持堆積的長度為 k
for i in k...nums.length
# 若當前元素大於堆積頂元素,則將堆積頂元素出堆積、當前元素入堆積
if nums[i] > peek_min_heap(max_heap)
pop_min_heap(max_heap)
push_min_heap(max_heap, nums[i])
end
end
get_min_heap(max_heap)
end
```
=== "Zig"