Add python code of Heap and Graph to docs.

pull/383/head
krahets 2 years ago
parent 1f4dba4845
commit 73c8920c6b

@ -9,7 +9,7 @@ sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from include import *
""" 基于邻接表实现的无向图类 """
""" 基于邻接表实现的无向图类 """
class GraphAdjList:
# 邻接表key: 顶点value该顶点的所有邻接结点
adj_list = {}

@ -8,6 +8,7 @@ import sys, os.path as osp
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from include import *
""" 基于邻接矩阵实现的无向图类 """
class GraphAdjMat:
# 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
@ -75,7 +76,7 @@ class GraphAdjMat:
self.adj_mat[i][j] = 0
self.adj_mat[j][i] = 0
# 打印邻接矩阵
""" 打印邻接矩阵 """
def print(self):
print("顶点列表 =", self.vertices)
print("邻接矩阵 =")

@ -8,9 +8,9 @@ import sys, os.path as osp
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from include import *
# 大顶堆
""" 大顶堆 """
class MaxHeap:
# 使用列表而非数组,这样无需考虑扩容问题
""" 构造方法 """
def __init__(self, nums: List[int]):
# 将列表元素原封不动添加进堆
self.max_heap = nums
@ -18,43 +18,43 @@ class MaxHeap:
for i in range(self.parent(self.size() - 1), -1, -1):
self.sift_down(i)
# 获取左子结点索引
""" 获取左子结点索引 """
def left(self, i: int) -> int:
return 2 * i + 1
# 获取右子结点索引
""" 获取右子结点索引 """
def right(self, i: int) -> int:
return 2 * i + 2
# 获取父结点索引
""" 获取父结点索引 """
def parent(self, i: int) -> int:
return (i - 1) // 2 # 向下整除
# 交换元素
""" 交换元素 """
def swap(self, i: int, j: int):
a, b = self.max_heap[i], self.max_heap[j]
self.max_heap[i], self.max_heap[j] = b, a
# 获取堆大小
""" 获取堆大小 """
def size(self) -> int:
return len(self.max_heap)
# 判断堆是否为空
""" 判断堆是否为空 """
def is_empty(self) -> bool:
return self.size() == 0
# 访问堆顶元素
""" 访问堆顶元素 """
def peek(self) -> int:
return self.max_heap[0]
# 元素入堆
""" 元素入堆 """
def push(self, val: int):
# 添加结点
self.max_heap.append(val)
# 从底至顶堆化
self.sift_up(self.size() - 1)
# 从结点 i 开始,从底至顶堆化
""" 从结点 i 开始,从底至顶堆化 """
def sift_up(self, i: int):
while True:
# 获取结点 i 的父结点
@ -67,7 +67,7 @@ class MaxHeap:
# 循环向上堆化
i = p
# 元素出堆
""" 元素出堆 """
def poll(self) -> int:
# 判空处理
assert not self.is_empty()
@ -80,7 +80,7 @@ class MaxHeap:
# 返回堆顶元素
return val
# 从结点 i 开始,从顶至底堆化
""" 从结点 i 开始,从顶至底堆化 """
def sift_down(self, i: int):
while True:
# 判断结点 i, l, r 中值最大的结点,记为 ma
@ -97,7 +97,7 @@ class MaxHeap:
# 循环向下堆化
i = ma
# 打印堆(二叉树)
""" 打印堆(二叉树) """
def print(self):
print_heap(self.max_heap)

@ -43,7 +43,7 @@ BFS 常借助「队列」来实现。队列具有“先入先出”的性质,
=== "Python"
```python title="graph_bfs.py"
[class]{}-[func]{graph_bfs}
```
=== "Go"
@ -160,7 +160,9 @@ BFS 常借助「队列」来实现。队列具有“先入先出”的性质,
=== "Python"
```python title="graph_dfs.py"
[class]{}-[func]{dfs}
[class]{}-[func]{graph_dfs}
```
=== "Go"

@ -123,7 +123,41 @@ comments: true
=== "Python"
```python title="heap.py"
# 初始化小顶堆
min_heap, flag = [], 1
# 初始化大顶堆
max_heap, flag = [], -1
# Python 的 heapq 模块默认实现小顶堆
# 考虑将“元素取负”后再入堆,这样就可以将大小关系颠倒,从而实现大顶堆
# 在本示例中flag = 1 时对应小顶堆flag = -1 时对应大顶堆
""" 元素入堆 """
heapq.heappush(max_heap, flag * 1)
heapq.heappush(max_heap, flag * 3)
heapq.heappush(max_heap, flag * 2)
heapq.heappush(max_heap, flag * 5)
heapq.heappush(max_heap, flag * 4)
""" 获取堆顶元素 """
peek = flag * max_heap[0] # 5
""" 堆顶元素出堆 """
# 出堆元素会形成一个从大到小的序列
val = flag * heapq.heappop(max_heap) # 5
val = flag * heapq.heappop(max_heap) # 4
val = flag * heapq.heappop(max_heap) # 3
val = flag * heapq.heappop(max_heap) # 2
val = flag * heapq.heappop(max_heap) # 1
""" 获取堆大小 """
size = len(max_heap)
""" 判断堆是否为空 """
is_empty = not max_heap
""" 输入列表并建堆 """
min_heap = [1, 3, 2, 5, 4]
heapq.heapify(min_heap)
```
=== "Go"
@ -311,7 +345,11 @@ comments: true
=== "Python"
```python title="my_heap.py"
[class]{MaxHeap}-[func]{left}
[class]{MaxHeap}-[func]{right}
[class]{MaxHeap}-[func]{parent}
```
=== "Go"
@ -403,7 +441,7 @@ comments: true
=== "Python"
```python title="my_heap.py"
[class]{MaxHeap}-[func]{peek}
```
=== "Go"
@ -493,7 +531,9 @@ comments: true
=== "Python"
```python title="my_heap.py"
[class]{MaxHeap}-[func]{push}
[class]{MaxHeap}-[func]{sift_up}
```
=== "Go"
@ -613,7 +653,9 @@ comments: true
=== "Python"
```python title="my_heap.py"
[class]{MaxHeap}-[func]{poll}
[class]{MaxHeap}-[func]{sift_down}
```
=== "Go"
@ -693,7 +735,7 @@ comments: true
=== "Python"
```python title="my_heap.py"
[class]{MaxHeap}-[func]{__init__}
```
=== "Go"

Loading…
Cancel
Save