|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Skip List: A probabilistic alternative to balanced trees. More...
#include <iostream>#include <iomanip>#include <string>#include <chrono>#include <random>#include <vector>#include <set>#include <tpl_dynSkipList.H>#include <tclap/CmdLine.h>Go to the source code of this file.
Functions | |
| void | demonstrate_basic_operations () |
| Demonstrate basic Skip List operations. | |
| void | demonstrate_string_set () |
| Demonstrate skip list with strings. | |
| void | demonstrate_functional () |
| Demonstrate functional programming features. | |
| void | benchmark_comparison (size_t n, unsigned seed, bool verbose) |
| Benchmark skip list vs std::set. | |
| void | visualize_structure () |
| Visualize skip list structure (small example) | |
| int | main (int argc, char *argv[]) |
Skip List: A probabilistic alternative to balanced trees.
This example demonstrates Skip Lists, an elegant randomized data structure invented by William Pugh in 1990. Skip Lists provide expected O(log n) performance for search, insert, and delete operations without the complexity of balanced tree rotations, making them easier to implement and understand.
A Skip List is a probabilistic data structure that uses multiple sorted linked lists at different "levels" to enable fast search. Think of it as a subway system with express trains (higher levels) and local trains (lower levels).
A Skip List consists of multiple linked lists (levels):
For elements {3, 6, 7, 9}:
Key insight: Higher levels act as "express lanes" allowing you to skip over many elements quickly.
Randomization: Each element has 50% chance of being at level i+1 if it's at level i. This creates the probabilistic structure.
❌ Probabilistic: No worst-case guarantee (though extremely unlikely) ❌ Memory overhead: Multiple pointers per element ❌ Randomization: Requires good random number generator
| Operation | Expected | Worst Case | Notes |
|---|---|---|---|
| Search | O(log n) | O(n) | Extremely rare worst case |
| Insert | O(log n) | O(n) | Includes search + insertion |
| Delete | O(log n) | O(n) | Includes search + deletion |
| Range scan (by iteration) | O(n) | O(n) | Iterate and filter as needed |
Expected height: O(log n) with high probability Worst case height: O(n) but probability is negligible
| Feature | Skip List | Balanced Tree (AVL/RB) |
|---|---|---|
| Implementation | Simpler | More complex |
| Rotations | None | Required |
| Worst-case guarantee | Probabilistic | Guaranteed |
| Concurrent version | Easier | Harder |
| Cache performance | Good | Variable |
| Memory overhead | Higher | Lower |
✅ Good for:
❌ Not good for:
Definition in file skiplist_example.C.
| void benchmark_comparison | ( | size_t | n, |
| unsigned | seed, | ||
| bool | verbose | ||
| ) |
Benchmark skip list vs std::set.
Definition at line 337 of file skiplist_example.C.
References StlAlephIterator< SetName >::end(), Aleph::DynSkipList< Key, Compare >::has(), Aleph::DynSkipList< Key, Compare >::insert(), Aleph::DynList< T >::insert(), Aleph::maps(), rng, Aleph::HTList::size(), Aleph::DynSkipList< Key, Compare >::size(), and Aleph::verbose.
Referenced by main().
| void demonstrate_basic_operations | ( | ) |
Demonstrate basic Skip List operations.
DynSkipList is an ordered set (not a map). Each element is stored and accessed by its own value.
Definition at line 211 of file skiplist_example.C.
References Aleph::DynSkipList< Key, Compare >::get_it(), Aleph::DynSkipList< Key, Compare >::has(), Aleph::DynSkipList< Key, Compare >::Iterator::has_curr(), Aleph::DynSkipList< Key, Compare >::insert(), Aleph::maps(), Aleph::DynSkipList< Key, Compare >::remove(), Aleph::DynSkipList< Key, Compare >::search(), and Aleph::DynSkipList< Key, Compare >::size().
Referenced by main().
| void demonstrate_functional | ( | ) |
Demonstrate functional programming features.
Definition at line 309 of file skiplist_example.C.
References FunctionalMethods< Container, T >::all(), FunctionalMethods< Container, T >::exists(), FunctionalMethods< Container, T >::for_each(), Aleph::DynList< T >::insert(), Aleph::maps(), and Aleph::sum().
Referenced by main().
| void demonstrate_string_set | ( | ) |
Demonstrate skip list with strings.
Definition at line 279 of file skiplist_example.C.
References Aleph::DynSkipList< Key, Compare >::get_it(), Aleph::DynSkipList< Key, Compare >::has(), Aleph::DynSkipList< Key, Compare >::Iterator::has_curr(), Aleph::DynSkipList< Key, Compare >::insert(), and Aleph::maps().
Referenced by main().
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 444 of file skiplist_example.C.
References benchmark_comparison(), demonstrate_basic_operations(), demonstrate_functional(), demonstrate_string_set(), Aleph::maps(), Aleph::size(), Aleph::verbose, and visualize_structure().
| void visualize_structure | ( | ) |
Visualize skip list structure (small example)
Definition at line 421 of file skiplist_example.C.
References Aleph::maps().
Referenced by main().