This page presents common data structures with simple visual descriptions, practical uses, and a Python script for generating diagrams.
Python Script
The following Python code generates the structure diagrams using Matplotlib and NetworkX.
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle, Circle
import networkx as nx
import numpy as np
fig, axes = plt.subplots(4, 3, figsize=(18, 22))
axes = axes.flatten()
def clean_ax(ax, title):
ax.set_title(title, fontsize=14, fontweight='bold')
ax.set_xticks([])
ax.set_yticks([])
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
ax.set_frame_on(False)
# 1. Array
ax = axes[0]
clean_ax(ax, "Array")
arr = [6, 3, 8, 12]
for i, v in enumerate(arr):
ax.add_patch(Rectangle((1.5 + i*1.8, 4), 1.5, 1.2, edgecolor='black', facecolor='#7bed9f'))
ax.text(2.25 + i*1.8, 4.6, str(v), ha='center', va='center', fontsize=12)
ax.text(2.25 + i*1.8, 3.5, str(i), ha='center', va='center', fontsize=10, color='gray')
# 2. Queue
ax = axes[1]
clean_ax(ax, "Queue")
queue = [6, 5, 8]
for i, v in enumerate(queue):
ax.add_patch(Rectangle((2, 2 + i*1.5), 4, 1, edgecolor='black', facecolor='#fd79a8'))
ax.text(4, 2.5 + i*1.5, str(v), ha='center', va='center', fontsize=12)
ax.text(6.5, 2.5, "front", fontsize=10)
ax.text(6.5, 5.5, "rear", fontsize=10)
ax.text(1.2, 4, "FIFO", fontsize=11)
# 3. Stack
ax = axes[2]
clean_ax(ax, "Stack")
stack = [3, 8, 1]
for i, v in enumerate(stack):
ax.add_patch(Rectangle((3, 2 + i*1.2), 4, 1, edgecolor='black', facecolor='#feca57'))
ax.text(5, 2.5 + i*1.2, str(v), ha='center', va='center', fontsize=12)
ax.text(7.2, 2.5, "top", fontsize=10)
ax.text(1.2, 4, "LIFO", fontsize=11)
# 4. Linked List
ax = axes[3]
clean_ax(ax, "Linked List")
vals = [6, 3, 5, 8, 12]
for i, v in enumerate(vals):
x = 1 + i*1.6
ax.add_patch(Circle((x, 5), 0.45, edgecolor='black', facecolor='#81ecec'))
ax.text(x, 5, str(v), ha='center', va='center', fontsize=11)
if i < len(vals)-1:
ax.arrow(x+0.5, 5, 0.7, 0, head_width=0.15, head_length=0.15, fc='black', ec='black')
# 5. Matrix
ax = axes[4]
clean_ax(ax, "Matrix")
mat = np.array([[2,7,3],[4,7,5],[8,9,6]])
for i in range(mat.shape[0]):
for j in range(mat.shape[1]):
ax.add_patch(Rectangle((2 + j*1.5, 3 + (2-i)*1.2), 1.2, 1, edgecolor='black', facecolor='#a29bfe'))
ax.text(2.6 + j*1.5, 3.5 + (2-i)*1.2, str(mat[i, j]), ha='center', va='center', fontsize=11)
# 6. Tree
ax = axes[5]
clean_ax(ax, "Tree")
nodes = {(5,8):"5",(3,6):"3",(7,6):"4",(2,4):"1",(4,4):"4"}
for (x,y), val in nodes.items():
ax.add_patch(Circle((x,y), 0.4, edgecolor='black', facecolor='#55efc4'))
ax.text(x, y, val, ha='center', va='center', fontsize=11)
ax.plot([5,3],[7.6,6.4], 'k-')
ax.plot([5,7],[7.6,6.4], 'k-')
ax.plot([3,2],[5.6,4.4], 'k-')
ax.plot([3,4],[5.6,4.4], 'k-')
# 7. HashMap
ax = axes[6]
clean_ax(ax, "HashMap")
keys = [("K1","V1"),("K2","V2")]
for i, (k,v) in enumerate(keys):
ax.add_patch(Rectangle((1.5, 6-i*2), 2, 1, edgecolor='black', facecolor='#fab1a0'))
ax.text(2.5, 6.5-i*2, k, ha='center', va='center')
ax.add_patch(Rectangle((6, 6-i*2), 2, 1, edgecolor='black', facecolor='#74b9ff'))
ax.text(7, 6.5-i*2, v, ha='center', va='center')
ax.text(4.2, 6.5-i*2, "=>", fontsize=14)
# 8. BST
ax = axes[7]
clean_ax(ax, "BST")
coords = {(5,8):5,(3,6):2,(7,6):6,(2,4):1,(4,4):3,(6,4):5,(8,4):7}
for (x,y), v in coords.items():
ax.add_patch(Circle((x,y), 0.35, edgecolor='black', facecolor='#ffeaa7'))
ax.text(x, y, str(v), ha='center', va='center')
edges = [((5,8),(3,6)),((5,8),(7,6)),((3,6),(2,4)),((3,6),(4,4)),((7,6),(6,4)),((7,6),(8,4))]
for (x1,y1),(x2,y2) in edges:
ax.plot([x1,x2],[y1-0.35,y2+0.35],'k-')
# 9. Heap
ax = axes[8]
clean_ax(ax, "Heap")
vals = [9,5,8,2,3]
coords = [(5,8),(3,6),(7,6),(2,4),(4,4)]
for (x,y), v in zip(coords, vals):
ax.add_patch(Circle((x,y), 0.35, edgecolor='black', facecolor='#ff7675'))
ax.text(x, y, str(v), ha='center', va='center')
for (x1,y1),(x2,y2) in [((5,8),(3,6)),((5,8),(7,6)),((3,6),(2,4)),((3,6),(4,4))]:
ax.plot([x1,x2],[y1-0.35,y2+0.35],'k-')
# 10. Trie
ax = axes[9]
clean_ax(ax, "Trie")
pts = {(5,8):"root",(3,6):"a",(5,6):"b",(7,6):"c",(2,4):"t",(3.8,4):"i",(5,4):"o",(6.2,4):"n"}
for (x,y), v in pts.items():
ax.add_patch(Circle((x,y), 0.3, edgecolor='black', facecolor='#dfe6e9'))
ax.text(x, y, v, ha='center', va='center', fontsize=8)
for (x1,y1),(x2,y2) in [((5,8),(3,6)),((5,8),(5,6)),((5,8),(7,6)),((3,6),(2,4)),((3,6),(3.8,4)),((5,6),(5,4)),((7,6),(6.2,4))]:
ax.plot([x1,x2],[y1-0.3,y2+0.3],'k-')
# 11. Graph
ax = axes[10]
clean_ax(ax, "Graph")
G = nx.Graph()
G.add_edges_from([("A","B"),("A","C"),("B","D"),("C","E")])
pos = {"A":(3,7),"B":(7,7),"C":(3,3),"D":(7,3),"E":(5,1.5)}
nx.draw(G, pos, ax=ax, with_labels=True, node_color='#55efc4', node_size=900, font_size=12)
# 12. Union Find
ax = axes[11]
clean_ax(ax, "Union Find")
vals = [1,2,3,4,5,6]
for i, v in enumerate(vals):
ax.add_patch(Circle((1.5+i*1.3, 5), 0.35, edgecolor='black', facecolor='#ffeaa7'))
ax.text(1.5+i*1.3, 5, str(v), ha='center', va='center')
ax.plot([1.5,2.8],[4.65,4.65],'k-')
ax.plot([1.5,4.1],[4.65,4.65],'k-')
ax.plot([5.4,6.7],[4.65,4.65],'k-')
plt.tight_layout()
plt.show()
For Blogger, place the HTML inside the post’s HTML view so the CSS, script, and code blocks render correctly.
Why this format works
This structure matches the article-style template: a title, a horizontally scrolling image strip, section headings, tables, a toggleable hidden section, and a code block area. It is also more maintainable than embedding everything as one long HTML fragment inside a single paragraph.
No comments:
Post a Comment