Skip to main content
This module provides visualization classes for drawing trees, heaps, tries, and graphs using NetworkX and Matplotlib.

Base Class

Draw

Base class for drawing various data structures.
from dsa.draw import Draw

drawer = Draw()

Methods

set_figsize
Sets the figure size for the plot.
drawer.set_figsize(figsize)
figsize
tuple
required
Tuple representing the figure size (width, height)
save
Saves the rendered plot to a file.
drawer.save(filename, **kwargs)
filename
str
required
Name of the file to save the plot to
kwargs
dict
Additional keyword arguments for customization
draw
Displays the rendered plot.
drawer.draw(**kwargs)
kwargs
dict
Additional keyword arguments for customization

TreeDraw

Class for drawing binary tree structures.
from dsa.draw import TreeDraw
from dsa.tree import Tree

td = TreeDraw(tree)
tree
Tree
required
The tree structure to be drawn

Methods

add_edges
Recursively adds edges to the graph and positions nodes in a tree layout.
pos = td.add_edges(graph, node, pos=None, x=0, y=0, layer=1)
graph
networkx.DiGraph
required
The graph object where edges are added
node
TreeNode
required
The current node in the tree
pos
dict
default:"None"
Dictionary to store node positions
x
float
default:"0"
X-coordinate for the current node
y
float
default:"0"
Y-coordinate for the current node
layer
int
default:"1"
Current layer/level of the node in the tree
pos
dict
Dictionary containing positions of all nodes in the tree
from dsa.tree import Tree, TreeNode
from dsa.draw import TreeDraw

# Create a tree
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)

tree = Tree(root)
td = TreeDraw(tree)
td.draw()

HeapDraw

Class for drawing heap structures.
from dsa.draw import HeapDraw
from dsa.heap import MinHeap

hd = HeapDraw(heap)
heap
Heap
required
The heap structure to be drawn

Methods

array_to_node
Converts an array-based heap into a tree node structure.
node = hd.array_to_node(index, array)
index
int
required
Current index in the array representing the node
array
list
required
Array containing heap values
node
TreeNode
Root node of the constructed subtree
from dsa.heap import MinHeap
from dsa.draw import HeapDraw

heap = MinHeap()
for val in [5, 3, 7, 1, 9, 4, 6]:
    heap.insert(val)

hd = HeapDraw(heap)
hd.draw()

TrieDraw

Class for drawing trie structures.
from dsa.draw import TrieDraw
from dsa.trie import Trie

trd = TrieDraw(trie)
trie
Trie
required
The trie structure to be drawn

Methods

to_networkx
Converts the trie into a NetworkX directed graph.
graph = trd.to_networkx()
graph
networkx.DiGraph
NetworkX graph representation of the trie
hierarchical_pos
Computes hierarchical positions for nodes in the graph.
pos = trd.hierarchical_pos(G, root=None, width=1.0, vert_gap=0.2, vert_loc=0, xcenter=0.5)
G
networkx.Graph
required
The graph for which to compute positions
root
str
default:"None"
Root node of the graph (auto-determined if None)
width
float
default:"1.0"
Width of the entire drawing
vert_gap
float
default:"0.2"
Gap between levels in the hierarchy
vert_loc
float
default:"0"
Vertical location of the root node
xcenter
float
default:"0.5"
Horizontal center of the root node
pos
dict
Dictionary mapping each node to its (x, y) position
from dsa.trie import Trie
from dsa.draw import TrieDraw

trie = Trie()
words = ["cat", "car", "card", "care", "careful", "dog", "dodge"]
for word in words:
    trie.insert(word)

trd = TrieDraw(trie)
trd.draw()

GraphDraw

Class for drawing graphs (both directed and undirected, weighted and unweighted).
from dsa.draw import GraphDraw

gd = GraphDraw(graph, directed=None, weighted=None)
graph
Graph
required
The graph to be drawn (any graph type: AdjacencyMatrixGraph, AdjacencyListGraph, etc.)
directed
bool
default:"None"
Specifies if the graph is directed (inferred from graph if None)
weighted
bool
default:"None"
Specifies if the graph has weighted edges (inferred from graph if None)

Methods

render
Renders the graph using Matplotlib.
plt = gd.render(pos=None, show_mst=False, mst_only=False, **kwargs)
pos
dict
default:"None"
Node positions dictionary (auto-computed if None)
show_mst
bool
default:"False"
If True, highlights the minimum spanning tree in red
mst_only
bool
default:"False"
If True, shows only the MST edges
kwargs
dict
Additional keyword arguments
plt
matplotlib.pyplot
Matplotlib plot object
from dsa.graph import AdjacencyListGraph
from dsa.draw import GraphDraw

graph = AdjacencyListGraph()
graph.add_edge("A", "B")
graph.add_edge("B", "C")
graph.add_edge("C", "D")
graph.add_edge("D", "A")

gd = GraphDraw(graph)
gd.draw()
Choosing Figure Size:
  • Small structures (< 10 nodes): (5, 3) or (6, 4)
  • Medium structures (10-50 nodes): (10, 6) or (12, 8)
  • Large structures (> 50 nodes): (15, 10) or larger
Graph Layouts:
  • Default layout is shell_layout for graphs
  • For hierarchical structures (trees, heaps, tries), custom hierarchical positioning is used
MST Visualization:
  • Use show_mst=True to overlay MST edges in red on the full graph
  • Use mst_only=True to show only the MST edges
  • MST is computed using NetworkX’s minimum spanning tree algorithm
Saving vs Drawing:
  • draw() displays the plot interactively
  • save() writes the plot to a file without displaying
  • Both methods accept the same kwargs for customization

Build docs developers (and LLMs) love