Skip to content

Commit

Permalink
Add python code of Heap and Graph to docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Feb 23, 2023
1 parent 1f4dba4 commit 73c8920
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 20 deletions.
2 changes: 1 addition & 1 deletion codes/python/chapter_graph/graph_adjacency_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from include import *


""" 基于邻接表实现的无向图类 """
""" 基于邻接表实现的无向图类 """
class GraphAdjList:
# 邻接表,key: 顶点,value:该顶点的所有邻接结点
adj_list = {}
Expand Down
3 changes: 2 additions & 1 deletion codes/python/chapter_graph/graph_adjacency_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from include import *


""" 基于邻接矩阵实现的无向图类 """
class GraphAdjMat:
# 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
Expand Down Expand Up @@ -75,7 +76,7 @@ def remove_edge(self, i, j):
self.adj_mat[i][j] = 0
self.adj_mat[j][i] = 0

# 打印邻接矩阵
""" 打印邻接矩阵 """
def print(self):
print("顶点列表 =", self.vertices)
print("邻接矩阵 =")
Expand Down
28 changes: 14 additions & 14 deletions codes/python/chapter_heap/my_heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,53 @@
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
# 堆化除叶结点以外的其他所有结点
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 的父结点
Expand All @@ -67,7 +67,7 @@ def sift_up(self, i: int):
# 循环向上堆化
i = p

# 元素出堆
""" 元素出堆 """
def poll(self) -> int:
# 判空处理
assert not self.is_empty()
Expand All @@ -80,7 +80,7 @@ def poll(self) -> int:
# 返回堆顶元素
return val

# 从结点 i 开始,从顶至底堆化
""" 从结点 i 开始,从顶至底堆化 """
def sift_down(self, i: int):
while True:
# 判断结点 i, l, r 中值最大的结点,记为 ma
Expand All @@ -97,7 +97,7 @@ def sift_down(self, i: int):
# 循环向下堆化
i = ma

# 打印堆(二叉树)
""" 打印堆(二叉树) """
def print(self):
print_heap(self.max_heap)

Expand Down
4 changes: 3 additions & 1 deletion docs/chapter_graph/graph_traversal.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ BFS 常借助「队列」来实现。队列具有“先入先出”的性质,
=== "Python"

```python title="graph_bfs.py"

[class]{}-[func]{graph_bfs}
```

=== "Go"
Expand Down Expand Up @@ -160,7 +160,9 @@ BFS 常借助「队列」来实现。队列具有“先入先出”的性质,
=== "Python"

```python title="graph_dfs.py"
[class]{}-[func]{dfs}

[class]{}-[func]{graph_dfs}
```

=== "Go"
Expand Down
48 changes: 45 additions & 3 deletions docs/chapter_heap/heap.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -403,7 +441,7 @@ comments: true
=== "Python"

```python title="my_heap.py"

[class]{MaxHeap}-[func]{peek}
```

=== "Go"
Expand Down Expand Up @@ -493,7 +531,9 @@ comments: true
=== "Python"

```python title="my_heap.py"
[class]{MaxHeap}-[func]{push}

[class]{MaxHeap}-[func]{sift_up}
```

=== "Go"
Expand Down Expand Up @@ -613,7 +653,9 @@ comments: true
=== "Python"

```python title="my_heap.py"
[class]{MaxHeap}-[func]{poll}

[class]{MaxHeap}-[func]{sift_down}
```

=== "Go"
Expand Down Expand Up @@ -693,7 +735,7 @@ comments: true
=== "Python"

```python title="my_heap.py"

[class]{MaxHeap}-[func]{__init__}
```

=== "Go"
Expand Down

0 comments on commit 73c8920

Please sign in to comment.