Data Structures
Stacks, queues, trees, heaps & hash tables — how they store and retrieve
Choosing the right data structure is often what separates a slow program from a fast one. Each structure trades off how quickly you can insert, remove, search or order data.
Linear structures
Stacks (last-in, first-out) and queues (first-in, first-out) impose an access order — stacks power undo and recursion, queues power scheduling and BFS. Linked lists chain nodes with pointers, giving cheap insertion and deletion anywhere without shifting elements, at the cost of no random access.
Hierarchical & keyed structures
Binary search trees keep keys ordered so lookups, insertions and range queries run in O(log n) on a balanced tree. Heaps are partially-ordered trees that surface the minimum or maximum in O(log n) — the engine behind priority queues and heap sort. Hash tables map keys to buckets through a hash function for average O(1) lookup, at the cost of unordered storage and occasional collisions.
The visualizers below let you push, pop, insert and delete to see exactly how each structure rearranges itself.
Try it interactively
Stack 可视化工具
交互式 LIFO 栈——push 和 pop 操作带动画 top 指针和单步控制,直接在浏览器中运行。
打开工具队列可视化工具
交互式 FIFO 队列 —— 入队(enqueue)和出队(dequeue)元素,配合 front/rear 指针动画和逐步控制按钮。直接在浏览器中运行。
打开工具链表可视化工具
交互式单向链表——支持头部/尾部插入、查找、删除,配有指针遍历动画和单步控制按钮。直接在浏览器中运行。
打开工具二叉搜索树可视化工具
交互式二叉搜索树——插入、查找、删除,配合动态树形图、分步控制和伪代码高亮。直接在浏览器中运行。
打开工具二叉堆可视化工具
交互式二叉最大堆(binary max-heap)——插入元素(insert)并取出最大值(extract-max),配合上浮(sift-up)/下沉(sift-down)动画,支持逐步执行和伪代码展示。完全在浏览器中运行。
打开工具哈希表可视化工具
交互式哈希表演示,采用链地址法(separate chaining)——插入、查找、删除数值,通过动画展示哈希映射与冲突处理过程。直接在浏览器中运行。
打开工具Prefer a focused tool?
- Data Structures → See data structures build and rearrange, step by step
常见问题
什么是哈希表?
哈希表是一种将键值对存储在多个桶(bucket)组成的数组中的数据结构,它使用哈希函数计算每个键对应的桶索引,从而使插入、查找和删除操作能够达到接近常数级的速度。
什么是哈希冲突(hash collision)?
哈希冲突是指两个不同的键被映射到同一个桶中的情况。本工具通过链地址法(separate chaining)处理冲突——每个桶内维护一个存放条目的链表(linked list)。
哈希表各项操作的时间复杂度是多少?
插入、查找和删除的平均时间复杂度均为 O(1)。当大量键冲突集中到同一个桶时,最坏情况下会达到 O(n)。
本工具使用的是哪种哈希函数?
为了便于观察,本工具使用了一个简单的取模哈希函数:index = value % 7。实际生产环境中的哈希表会采用更强的哈希算法,并自动扩容以保持链表长度较短。