Data Structure Visualization Helper Functions
This document outlines the helper functions available for visualizing various data structures.
Table of Contents
Linked List Functions
Create Linked List
Creates a linked list from an array of values.
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);
Generating interactive preview...
Parameters:
arr: Array of numbers or strings
Returns:
- A linked list where each node contains a value from the input array
- Returns
null/None/nullptr if the input array is empty
Example:
const listHead = createLinkedList([1, 2, 3])
list_head = create_linked_list([1, 2, 3])
ListNode<int>* listHead = createLinkedList(vector<int>{1, 2, 3});
Generating interactive preview...
Convert Linked List to Array
Converts a linked list back to an array.
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);
Generating interactive preview...
Parameters:
head: The head node of the linked list
Returns:
- Array containing all values from the linked list in order
Example:
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}
Generating interactive preview...
Queue Functions
Create Queue
Creates a queue from either a string or an array.
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);
Generating interactive preview...
Parameters:
input: Either a string or an array of numbers/strings
Returns:
- A queue containing all elements from the input
- Returns
None/nullptr if input is empty
Example:
const queue1 = createQueue("abc") // Queue with elements 'a', 'b', 'c'
const queue2 = createQueue([1, 2, 3]) // Queue with elements 1, 2, 3
queue1 = create_queue("abc") # Queue with elements 'a', 'b', 'c'
queue2 = create_queue([1, 2, 3]) # Queue with elements 1, 2, 3
auto queue1 = createQueue(vector<char>{'a', 'b', 'c'});
auto queue2 = createQueue(vector<int>{1, 2, 3});
Generating interactive preview...
Convert Queue to Array
Converts a queue to an array while preserving the original queue.
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);
Generating interactive preview...
Parameters:
Returns:
- Array containing all elements in the queue in FIFO order
Example:
const arr = queueToArray(queue) // [1, 2, 3]
arr = queue_to_array(queue) # [1, 2, 3]
vector<int> arr = queueToArray(queue); // {1, 2, 3}
Generating interactive preview...
Binary Tree Functions
Create Binary Tree
Creates a binary tree from an array using level-order traversal (breadth-first).
function createBinaryTree(arr: (string | number | null)[]): TreeNode | null;
def create_binary_tree(arr: List[Optional[Union[str, int]]]) -> Optional[BinaryTreeNode]:
// -1||"-1" for empty nodes
template <typename T> TreeNode<T> *createBinaryTree(const vector<T> &arr);
Generating interactive preview...
Parameters:
arr: Array of numbers/strings/null values representing the tree in level-order
- Use
null/None/nullptr to represent empty nodes
Returns:
- Root node of the created binary tree
- Returns
null/None/nullptr if input array is empty or starts with null
Example:
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});
Generating interactive preview...
Convert Binary Tree to Array
Converts a binary tree to an array using level-order traversal.
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);
Generating interactive preview...
Parameters:
root: Root node of the binary tree
Returns:
- Array representing the tree in level-order
- Empty nodes are represented as
null/None/T()
- Trailing nulls are removed
Example:
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}
Generating interactive preview...
Stack Functions
Create Stack
Creates a stack from an array, where the last element becomes the top of the stack.
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);
Generating interactive preview...
Parameters:
arr: Array of numbers or strings
Returns:
- A stack containing elements from the array
Example:
const stack = createStack([1, 2, 3]) // Stack with 3 at top
stack = create_stack([1, 2, 3]) # Stack with 3 at top
stack<int> s = createStack(vector<int>{1, 2, 3}); // 3 at top
Generating interactive preview...
Convert Stack to Array
Converts a stack to an array while preserving the original stack.
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);
Generating interactive preview...
Parameters:
Returns:
- Array containing elements in LIFO order (top of stack first)
Example:
const arr = stackToArray(stack) // [3, 2, 1]
arr = stack_to_array(stack) # [3, 2, 1]
vector<int> arr = stackToArray(stack); // {3, 2, 1}
Generating interactive preview...
Graph Functions
Creating a Graph
function createGraph(data: T, directedGraph: boolean = true): T;
def create_graph(data: T, directedGraph: bool = True) -> T:
Generating interactive preview...
Parameters:
data: Graph data in adjacency list format. See examples for details.
directedGraph: Whether the graph is directed. Defaults to true/True.
Return Value:
- Returns data similar to the input, which can be treated as the data itself.
Examples:
// Create a directed weighted graph
const graph1 = Graph({
A: { B: 4, C: 5 },
B: { C: -2, D: 3 },
C: { D: 4 },
D: { E: 2 },
E: {},
})
// Create an undirected weighted graph
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
)
// Create a directed unweighted graph
const graph3 = Graph({
'数据结构': ["算法"],
'算法': ["机器学习"],
'数学': ["机器学习", "深度学习"],
'机器学习': ["深度学习"],
'深度学习': [],
'Python基础': ["数据结构", "数学"],
})
# Create a directed weighted graph
graph1 = Graph({
'A': {'B': 4, 'C': 5},
'B': {'C': -2, 'D': 3},
'C': {'D': 4},
'D': {'E': 2},
'E': {}
})
# Create an undirected weighted graph
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)
# Create a directed unweighted graph
graph3 = Graph({
'数据结构': ['算法'],
'算法': ['机器学习'],
'数学': ['机器学习', '深度学习'],
'机器学习': ['深度学习'],
'深度学习': [],
'Python基础': ['数据结构', '数学']
})
Generating interactive preview...