An open API service indexing awesome lists of open source software.

https://github.com/chopikus/ll-rb-tree

This repository aims at making the best implementation of the LLRB-tree data structure.
https://github.com/chopikus/ll-rb-tree

Last synced: 7 months ago
JSON representation

This repository aims at making the best implementation of the LLRB-tree data structure.

Awesome Lists containing this project

README

          


Left-leaning Red-black Tree 🌳

This repository aims at making the best implementation of the [LLRB-tree data structure](https://sedgewick.io/wp-content/themes/sedgewick/papers/2008LLRB.pdf).

The data structure resembles a usual Red-Black tree, remade with simplicity in mind.

## Features
* `insert`;
* `erase`;
* `lower_bound`;
* `find`;
* iterators, `begin()`, `end()`;
* initalizing from `std::initializer_list`;
* copy & move semantics;
* and many more!

## Usage

All needed implementation is written in the `src/set.h` file.

The usage is very similar to `std::set`.

### Avaliable classes

* `Set` - set that stores `ValueType` elements. `ValueType` should be comparable by operator `<`.
* `Set::iterator` - set iterator, can be got using `begin()`, `end()`, `find(x)`, `lower_bound(x)`.

### Avaliable functions and constructors

* `Set()` - empty constructor

Example: `Set my_set;`
* `Set(Set& another)` - constructor that copies all elements

Example: `Set my_set(another_set);`
* `Set(const std::initializer_list &list)` - creating `Set`, copying elements out of `list`.

Example: `Set my_set{1, 2, 3}`.
* `void insert(const ValueType& value)` - adds element to `Set`;
* `void erase(const ValueType& value)` - removes element `value` if present in `Set`;
* `void erase(const Set::iterator& iterator)` - removes element by iterator;
* `bool empty()` - returns `true` if `Set` is empty;
* `size_t size()` - returns amount of elements in `Set`;
* `Set::iterator begin()` - returns iterator to the first element of `Set`
* `Set::iterator end()` - returns iterator to past-the-end element of `Set`

Avaliable `Set::iterator` methods:
* `++`, `--` - changing iterator's element to it's next/previous;
* `*` - returns the element correspondant to the iterator;
* `->` - returns a reference to the element correspondant to the iterator.
* `==`, `!=` - comparing iterators.

### Code example (C++)
This program reads characters and numbers line by line and modifies data structure accoring to the input.
* `+ x` means adding number `x` to the Set;
* `- x` - removing number `x` from the Set;
* `> x` prints first stored number, that's greater or equal to `x`;
* `?` - prints all numbers stored in Set.
```
#include
#include
#include
#include "src/set.h"

using namespace std;

int main() {
int q=0;
cin >> q;
int cnt=0;
Set my_set;
for (int i=0; i> x;
if (x=='+') {
int value = 0;
cin >> value;
cnt++;
my_set.insert(value);
} else if (x=='?') {
for (auto s : my_set) {
cout << s << ' ';
}
cout << endl;
} else if (x == '-') {
int value = 0;
cin >> value;
my_set.erase(value);
} else if (x == '>') {
int y=0;
cin >> y;
if (my_set.lower_bound(y) != my_set.end()) {
cout << *(my_set.lower_bound(y)) << endl;
}
}
}
return 0;
}
```

As you can see, the usage is very similar to the `std::set`.

### Testing/Running the example
1. Clone this repository -- `git clone https://github.com/chopikus/ll-rb-tree.git`.
2. Open the cloned folder -- `cd ll-rb-tree`.
3. Create new folder for building tests, and open it -- `mkdir build && cd build`.
4. Run the following command: `cmake .. && make tests`.

4.1. You can also build the example using `cmake .. && make`. Then the executable called `usage_example` will appear.

All tests are written in the `tests/tests.cpp` file.

## Benchmarks
TODO