跳到主要内容
Z

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

Prefer a focused tool?

常见问题

什么是哈希表?

哈希表是一种将键值对存储在多个桶(bucket)组成的数组中的数据结构,它使用哈希函数计算每个键对应的桶索引,从而使插入、查找和删除操作能够达到接近常数级的速度。

什么是哈希冲突(hash collision)?

哈希冲突是指两个不同的键被映射到同一个桶中的情况。本工具通过链地址法(separate chaining)处理冲突——每个桶内维护一个存放条目的链表(linked list)。

哈希表各项操作的时间复杂度是多少?

插入、查找和删除的平均时间复杂度均为 O(1)。当大量键冲突集中到同一个桶时,最坏情况下会达到 O(n)。

本工具使用的是哪种哈希函数?

为了便于观察,本工具使用了一个简单的取模哈希函数:index = value % 7。实际生产环境中的哈希表会采用更强的哈希算法,并自动扩容以保持链表长度较短。