数据结构可视化辅助函数
本文档概述了可用于可视化各种数据结构的辅助函数。
目录
链表函数
创建链表
根据值的数组创建一个链表。
function createLinkedList<T>(arr: T[]): ListNode<T> | null;
def create_linked_list(arr: List[T]) -> Optional[ListNode]
template <typename T> ListNode<T> *createLinkedList(const vector<T> &vec);
正在生成交互式预览...
参数:
返回值:
- 一个链表,每个节点包含输入数组中的一个值
- 输入数组为空时返回
null/None/nullptr
示例:
const listHead = createLinkedList([1, 2, 3])
list_head = create_linked_list([1, 2, 3])
ListNode<int>* listHead = createLinkedList(vector<int>{1, 2, 3});
正在生成交互式预览...
将链表转换为数组
将链表转换回数组。
function linkedListToArray<T>(head: ListNode<T> | null): T[];
def linked_list_to_array(head: Optional[ListNode]) -> List[T]:
template <typename T> vector<T> linkedListToArray(ListNode<T> *head);
正在生成交互式预览...
参数:
返回值:
示例:
const arr = linkedListToArray(listHead) // [1, 2, 3]
arr = linked_list_to_array(list_head) # [1, 2, 3]
vector<int> arr = linkedListToArray(listHead); // {1, 2, 3}
正在生成交互式预览...
队列函数
创建队列
根据字符串或数组创建一个队列。
function createQueue(input: string | (string | number)[]): Queue;
def create_queue(s: str | list) -> Optional[Queue]:
template <typename T> std::queue<T> createQueue(const std::vector<T> &vec);
正在生成交互式预览...
参数:
返回值:
- 包含输入所有元素的队列
- 输入为空时返回
None/nullptr
示例:
const queue1 = createQueue("abc") // 包含元素 'a', 'b', 'c' 的队列
const queue2 = createQueue([1, 2, 3]) // 包含元素 1, 2, 3 的队列
queue1 = create_queue("abc") # 包含元素 'a', 'b', 'c' 的队列
queue2 = create_queue([1, 2, 3]) # 包含元素 1, 2, 3 的队列
auto queue1 = createQueue(vector<char>{'a', 'b', 'c'});
auto queue2 = createQueue(vector<int>{1, 2, 3});
正在生成交互式预览...
将队列转换为数组
将队列转换为数组并保留原队列。
function queueToArray(queue: Queue): (string | number)[];
def queue_to_array(queue: Queue) -> List[Union[str, int]]:
template <typename T> std::vector<T> queueToArray(std::queue<T> &q);
正在生成交互式预览...
参数:
返回值:
示例:
const arr = queueToArray(queue) // [1, 2, 3]
arr = queue_to_array(queue) # [1, 2, 3]
vector<int> arr = queueToArray(queue); // {1, 2, 3}
正在生成交互式预览...
二叉树函数
创建二叉树
通过层序遍历(广度优先)从数组创建二叉树。
function createBinaryTree(arr: (string | number | null)[]): TreeNode | null;
def create_binary_tree(arr: List[Optional[Union[str, int]]]) -> Optional[BinaryTreeNode]:
// -1||"-1" 表示空节点
template <typename T> TreeNode<T> *createBinaryTree(const vector<T> &arr);
正在生成交互式预览...
参数:
arr: 表示层序遍历的数值/字符串/null 数组
- 用
null/None/-1 表示空节点
返回值:
- 创建的二叉树的根节点
- 输入数组为空或第一个元素为 null 时返回
null/None/nullptr
示例:
const root = createBinaryTree([1, 2, 3, null, 4])
root = create_binary_tree([1, 2, 3, None, 4])
TreeNode<int>* root = createBinaryTree(vector<int>{1, 2, 3, -1, 4});
正在生成交互式预览...
将二叉树转换为数组
通过层序遍历将二叉树转换为数组。
function binaryTreeToArray(root: TreeNode | null): (string | number | null)[];
def binary_tree_to_array(root: Optional[BinaryTreeNode]) -> List[Optional[Union[str, int]]]:
template <typename T> vector<T> binaryTreeToArray(TreeNode<T> *root);
正在生成交互式预览...
参数:
返回值:
- 层序遍历表示的数组
- 空节点表示为
null/None/T()
- 去除末尾的 null
示例:
const arr = binaryTreeToArray(root) // [1, 2, 3, null, 4]
arr = binary_tree_to_array(root) # [1, 2, 3, None, 4]
vector<optional<int>> arr = binaryTreeToArray(root); // {1, 2, 3, 0, 4}
正在生成交互式预览...
栈函数
创建栈
从数组创建栈,最后一个元素成为栈顶。
function createStack<T extends string | number>(arr: T[]): Stack<T>;
def create_stack(arr: List[T]) -> Stack[T]:
template <typename T> std::stack<T> createStack(const std::vector<T> &vec);
正在生成交互式预览...
参数:
返回值:
示例:
const stack = createStack([1, 2, 3]) // 栈顶为 3 的栈
stack = create_stack([1, 2, 3]) # 栈顶为 3 的栈
stack<int> s = createStack(vector<int>{1, 2, 3}); // 栈顶为 3
正在生成交互式预览...
将栈转换为数组
将栈转换为数组并保留原栈。
function stackToArray<T extends string | number>(stack: Stack<T>): T[];
def stack_to_array(stack: Stack[T]) -> List[T]:
template <typename T> std::vector<T> stackToArray(std::stack<T> &s);
正在生成交互式预览...
参数:
返回值:
示例:
const arr = stackToArray(stack) // [3, 2, 1]
arr = stack_to_array(stack) # [3, 2, 1]
vector<int> arr = stackToArray(stack); // {3, 2, 1}
正在生成交互式预览...
图函数
创建图
function createGraph(data: T, directedGraph: boolean = true): T;
def create_graph(data: T, directedGraph: boolean = True) -> T:
正在生成交互式预览...
参数:
data: 邻接表格式的图数据。详情见示例
directedGraph: 是否为有向图,默认为 true/True
返回值:
- 返回类 data 数据,您可以将其视为 data 本身
示例:
// 创建有向权重图
const graph1 = Graph({
A: { B: 4, C: 5 },
B: { C: -2, D: 3 },
C: { D: 4 },
D: { E: 2 },
E: {},
})
// 创建无向权重图
const graph2 = Graph(
{
A: { B: 1, C: 4 },
B: { A: 1, C: 2, D: 5 },
C: { A: 4, B: 2, D: 1 },
D: { B: 5, C: 1 },
},
false
)
// 创建有向无权图
const graph3 = Graph({
数据结构: ["算法"],
算法: ["机器学习"],
数学: ["机器学习", "深度学习"],
机器学习: ["深度学习"],
深度学习: [],
Python基础: ["数据结构", "数学"],
})
# 创建有向权重图
graph1 = Graph({
'A': {'B': 4, 'C': 5},
'B': {'C': -2, 'D': 3},
'C': {'D': 4},
'D': {'E': 2},
'E': {}
})
# 创建无向权重图
graph1 = Graph({
'A': {'B': 1, 'C': 4},
'B': {'A': 1, 'C': 2, 'D': 5},
'C': {'A': 4, 'B': 2, 'D': 1},
'D': {'B': 5, 'C': 1}
}, False)
# 创建有向无权图
graph1 = Graph({
'数据结构': ['算法'],
'算法': ['机器学习'],
'数学': ['机器学习', '深度学习'],
'机器学习': ['深度学习'],
'深度学习': [],
'Python基础': ['数据结构', '数学']
})
正在生成交互式预览...