Add the Python codes for the chapter of Graph and Heap (#382)

This commit is contained in:
Yudong Jin
2023-02-23 20:00:40 +08:00
committed by GitHub
parent 776f2d82bb
commit 1f4dba4845
10 changed files with 553 additions and 5 deletions

View File

@ -1,5 +1,6 @@
import copy
import math
import heapq
import queue
import random
import functools
@ -7,4 +8,5 @@ import collections
from typing import Optional, List, Dict, DefaultDict, OrderedDict, Set, Deque
from .linked_list import ListNode, list_to_linked_list, linked_list_to_list, get_list_node
from .binary_tree import TreeNode, list_to_tree, tree_to_list, get_tree_node
from .print_util import print_matrix, print_linked_list, print_tree, print_dict
from .vertex import Vertex, vals_to_vets, vets_to_vals
from .print_util import print_matrix, print_linked_list, print_tree, print_dict, print_heap

View File

@ -6,7 +6,7 @@ Author: Krahets (krahets@163.com), msk397 (machangxinq@gmail.com)
import copy
import queue
from .binary_tree import TreeNode, tree_to_list
from .binary_tree import TreeNode, tree_to_list, list_to_tree
from .linked_list import ListNode, linked_list_to_list
def print_matrix(mat):
@ -80,4 +80,10 @@ def print_dict(d):
d ([type]): [description]
"""
for key, value in d.items():
print(key, '->', value)
print(key, '->', value)
def print_heap(heap):
print("堆的数组表示:", heap);
print("堆的树状表示:");
root = list_to_tree(heap)
print_tree(root);

View File

@ -0,0 +1,18 @@
# File: vertex.py
# Created Time: 2023-02-23
# Author: Krahets (krahets@163.com)
from typing import List
# 顶点类
class Vertex:
def __init__(self, val: int) -> None:
self.val = val
# 输入值列表 vals ,返回顶点列表 vets
def vals_to_vets(vals: List[int]) -> List['Vertex']:
return [Vertex(val) for val in vals]
# 输入顶点列表 vets ,返回值列表 vals
def vets_to_vals(vets: List['Vertex']) -> List[int]:
return [vet.val for vet in vets]