feat: Add visualizing code blocks based on the pythontutor (#1029)

* Update copyright

* Update the Python code

* Fix the code comments in ArrayBinaryTree

* Fix the code comments in ArrayBinaryTree

* Roll back time_comlexity.py

* Add the visualizing code(pythontutor) blocks to the chapter complexity, data structure, array and linked list, stack and queue, hash table, and backtracking

* Fix the code comments
This commit is contained in:
Yudong Jin
2024-01-07 04:04:01 +08:00
committed by GitHub
parent 0f5b924036
commit ddd375af20
61 changed files with 434 additions and 40 deletions

View File

@ -63,7 +63,7 @@ class MyList:
if index < 0 or index >= self._size:
raise IndexError("索引越界")
num = self._arr[index]
# 索引 i 之后的元素都向前移动一位
# 索引 index 之后的元素都向前移动一位
for j in range(index, self._size - 1):
self._arr[j] = self._arr[j + 1]
# 更新元素数量

View File

@ -19,7 +19,7 @@ class ArrayBinaryTree:
self._tree = list(arr)
def size(self):
"""节点数"""
"""列表容"""
return len(self._tree)
def val(self, i: int) -> int:

View File

@ -7,7 +7,6 @@ from .list_node import (
ListNode,
list_to_linked_list,
linked_list_to_list,
get_list_node,
)
from .tree_node import TreeNode, list_to_tree, tree_to_list
from .vertex import Vertex, vals_to_vets, vets_to_vals

View File

@ -6,7 +6,7 @@ Author: Krahets (krahets@163.com)
class ListNode:
"""Definition for a singly-linked list node"""
"""链表节点类"""
def __init__(self, val: int):
self.val: int = val # 节点值
@ -14,7 +14,7 @@ class ListNode:
def list_to_linked_list(arr: list[int]) -> ListNode | None:
"""Generate a linked list with a list"""
"""将列表序列化为链表"""
dum = head = ListNode(0)
for a in arr:
node = ListNode(a)
@ -24,16 +24,9 @@ def list_to_linked_list(arr: list[int]) -> ListNode | None:
def linked_list_to_list(head: ListNode | None) -> list[int]:
"""Serialize a linked list into an array"""
"""将链表反序列化为列表"""
arr: list[int] = []
while head:
arr.append(head.val)
head = head.next
return arr
def get_list_node(head: ListNode | None, val: int) -> ListNode | None:
"""Get a list node with specific value from a linked list"""
while head and head.val != val:
head = head.next
return head

View File

@ -9,7 +9,7 @@ from .list_node import ListNode, linked_list_to_list
def print_matrix(mat: list[list[int]]):
"""Print a matrix"""
"""打印矩阵"""
s = []
for arr in mat:
s.append(" " + str(arr))
@ -17,7 +17,7 @@ def print_matrix(mat: list[list[int]]):
def print_linked_list(head: ListNode | None):
"""Print a linked list"""
"""打印链表"""
arr: list[int] = linked_list_to_list(head)
print(" -> ".join([str(a) for a in arr]))
@ -39,7 +39,7 @@ def print_tree(
root: TreeNode | None, prev: Trunk | None = None, is_right: bool = False
):
"""
Print a binary tree
打印二叉树
This tree printer is borrowed from TECHIE DELIGHT
https://www.techiedelight.com/c-program-print-binary-tree/
"""
@ -68,13 +68,13 @@ def print_tree(
def print_dict(hmap: dict):
"""Print a dict"""
"""打印字典"""
for key, value in hmap.items():
print(key, "->", value)
def print_heap(heap: list[int]):
"""Print a heap both in array and tree representations"""
"""打印堆"""
print("堆的数组表示:", heap)
print("堆的树状表示:")
root: TreeNode | None = list_to_tree(heap)