Simple implementation of some data structures in C++
- Linear
- Trees
- Other
Documentation in separate file
T
- Type of elements.
Return: none
Arguments: T
Pushes element with a given value on the top of a stack.
Return: none
Arguments: none
Removes element from the top of a stack.
Return: T
Arguments: none
Gets value of the top element.
Return: unsigned int
Arguments: none
Gets size of the stack.
Return: bool
Arguments: none
Returns whether the stack is empty.
Return: iterator
Arguments: none
Returns iterator object to the first element.
Return: iterator
Arguments: none
Returns iterator object to the element past the last.
Library include
#include "path/to/Basic-Data-Structures/include/stack"
Stack declaration
Stack<int> s;
Basic push
, pop
and top
usage
s.push(4); // stack: 4
s.push(1); // stack: 1 4
s.pop(); // stack: 4
s.push(3); // stack: 3 4
std::cout << s.top() << "\n"; // prints: 3
s.push(2); // stack: 2 3 4
std::cout << s.top() << "\n"; // prints: 2
Print stack using empty
, top
and pop
while(!s.empty()) {
std::cout << s.top() << "\n";
s.pop();
}
Print stack using iterator
(recommended way)
for(Stack<int>::iterator i = s.begin(); i != s.end(); ++i) {
std::cout << *i << "\n";
}
Print stack using iterator
with range-based for loop
and auto
(recommended way for C++11 and higher)
for(auto i : s) {
std::cout << i << "\n";
}
Documentation in separate file
T
- Type of elements.
Return: none
Arguments: T
Pushes element with a given value to the front.
Return: none
Arguments: none
Removes element from the back.
Return: T
Arguments: none
Gets value of the front element.
Return: T
Arguments: none
Gets value of the back element.
Return: unsigned int
Arguments: none
Gets size of the queue.
Return: bool
Arguments: none
Returns whether the queue is empty.
Library include
#include "path/to/Basic-Data-Structures/include/queue"
Queue declaration
Queue<int> q;
Basic push
, pop
, front
and back
usage
q.push(2); // queue: 2
q.push(4); // queue: 4 2
q.push(3); // queue: 3 4 2
std::cout << q.front() << " " << q.back() << "\n"; // prints: 3 2
q.push(1); // queue: 1 3 4 2
q.pop(); // queue: 1 3 4
std::cout << q.front() << " " << q.back() << "\n"; // prints: 1 4
Documentation in separate file
T
- Type of elements.
Return: none
Arguments: T
Pushes element with a given value to the front.
Return: none
Arguments: T
Pushes element with a given value to the back.
Return: none
Arguments: none
Removes element from the front.
Return: none
Arguments: none
Removes element from the back.
Return: T
Arguments: none
Returns front element.
Return: T
Arguments: none
Returns back element.
Return: unsigned int
Arguments: none
Returns size of the list.
Return: bool
Arguments: none
Returns whether the list is empty.
Return: iterator
Arguments: none
Returns iterator object to the front element.
Return: iterator
Arguments: none
Returns iterator object to past the back element.
Library include
#include "path/to/Basic-Data-Structures/include/list"
List declaration
List<int> l;
Basic push_front
, push_back
, pop_front
, pop_back
, front
and back
usage
l.push_front(3); // list: 3
l.push_front(2); // list: 2 3
l.push_back(4); // list: 2 3 4
l.push_back(5); // list: 2 3 4 5
l.push_front(1); // list: 1 2 3 4 5
std::cout << l.front() << "\n"; // prints 1
std::cout << l.back() << "\n"; // prints 5
l.pop_front(); // list: 2 3 4 5
l.pop_back(); // list: 2 3 4
l.pop_front(); // list: 3 4
std::cout << l.front() << "\n"; // prints 3
std::cout << l.back() << "\n"; // prints 4
Print list using empty
, front
and pop_front
while(!l.empty()) {
std::cout << l.front() << "\n";
l.pop_front();
}
Print stack using iterator
(recommended way)
for(List<int>::iterator i = l.begin(); i != l.end(); ++i) {
std::cout << *i << "\n";
}
Print list using iterator
with range-based for loop
and auto
(recommended way for C++11 and higher)
for(auto i : l) {
std::cout << i << "\n";
}
Documentation in separate file
T
- Type of elements.
Return: none
Arguments: T
Adds an element to the back.
Return: T
Arguments: unsigned int
Access specified element.
Return: unsigned int
Arguments: none
Returns the number of elements.
Return: bool
Arguments: none
Returns whether the vector is empty.
Return: iterator
Arguments: none
Returns an iterator to the begining.
Return: iterator
Arguments: none
Returns an iterator to the end.
Library include
#include "path/to/Basic-Data-Structures/include/vector"
Vector declaration
Vector<int> v;
Basic push_back
and operator[]
usage
v.push_back(3); // vector: 3
v.push_back(7); // vector: 3 7
v.push_back(-2); // vector: 3 7 -2
v[1] = 4; // vector: 3 4 -2
std::cout << v[2] << "\n"; // prints -2
std::cout << v[1] << "\n"; // prints 4
Print vector using size
and operator[]
for(int i = 0; i < v.size(); i++) {
std::cout << v[i] << "\n";
}
Print vector using iterator
with range-based for loop
and auto
(C++11)
for(auto i : v) {
std::cout << i << "\n";
}
Documentation in separate file
T
- Type of elements.
Return type: none
Arguments: T
Inserts a given value to the tree.
Return type: bool
Arguments: T
Returns whether a given value exist in the tree.
Return type: none
Arguments: T
Removes given value from the tree if it exist.
Library include
#include "path/to/Basic-Data-Structures/include/bstree"
BSTree declaration
BSTree<int> l;
Insertion
tree.insert(2);
tree.insert(4);
tree.insert(1);
tree.insert(5);
tree.insert(3);
/*
now tree looks like this:
2
/ \
1 4
/ \
3 5
*/
You can print BSTree using BSTreePrinter class. Library include:
#include "path/to/Basic-Data-Structures/include/bstreeprinter"
Available functions:
static void print(BSTree<T>*);
static void print(BSTree<T>&);
Example:
BSTree<int> t;
// some insert operations here
BSTreePrinter<int>::print(t); // tree object passed by reference
or
BSTreePrinter<int>::print(&t); // tree object passed by pointer
Documentation in separate file
T
- Type of elements
Operation
- Struct that defines what operation tree uses More here
()
- Constructs tree of size 0
(unsigned int size)
- Constructs tree of a given size
(unsigned int size, T fill)
- Constructs tree of a given size
and fills it with fill
value
(const T* array, unsigned int size)
- Constructs tree from an array
Return: none
Arguments: T
Adds an element to the end.
Return: none
Arguments: unsigned int
, T
Changes element on a given position.
Return: T
Arguments: unsigned int
, unsigned int
Returns answer for query on interval [p,q]
.
Return: unsigned int
Arguments: none
Returns size of the tree.
Return: bool
Arguments: none
Returns whether the tree is empty.
Return: none
Arguments: none
Prints whole tree using std::cout
.
Interval tree can do every associative operation. Those are implemented operations:
- addition -
IntervalTreeOperation<T>::Sum
- maximum -
IntervalTreeOperation<T>::Max
- minimum -
IntervalTreeOperation<T>::Min
- multiplication -
IntervalTreeOperation<T>::Product
- bitwise xor -
IntervalTreeOperation<T>::Xor
To implement custom operation, struct with operation
method and identityElement
attribute must be passed as the second template argument. Static function operation
takes 2 arguments (either passed by value or reference) and returns one value. They must all be of the same type. Static constant variable identityElement
is a value that for every x operation(x,identityElement) = x
, for example 0 is indentity element for addition.
static T operation(T a, T b); // pass by value
static T operation(const T &a, const T &b); // pass by reference
static const T identityElement;
Here is an example that implements bitwise and operation for integers:
struct BitwiseAnd {
static const int identityElement = 0;
static int operation(int a, int b) {
return a^b;
}
};
IntervalTree<int,BitwiseAnd> tree;
Library include
#include "path/to/Basic-Data-Structures/include/intervaltree"
Tree declaration
IntervalTree<int> tree; // Sum operation by default
IntervalTree<int,IntervalTreeOperation<int>::Sum> tree; // Sum
IntervalTree<int,IntervalTreeOperation<int>::Max> tree; // Maximum
IntervalTree<int,IntervalTreeOperation<int>::Min> tree; // Minimum
IntervalTree<int,IntervalTreeOperation<int>::Product> tree; // Product
IntervalTree<int,IntervalTreeOperation<int>::Xor> tree; // Bitwise xor
int array[8] = {1,4,-2,7,0,2,3,2};
IntervalTree<int> tree(array,8); // Sum tree created from array
Basic query
and set
usage
/* if tree looks like this:
* 17
* 10 7
* 5 5 2 5
* 1 4 -2 7 0 2 3 2
*/
std::cout << tree.query(2,6) << "\n"; // answer is 11 (4-2+7+0+2)
std::cout << tree.query(4,7) << "\n"; // answer is 12 (7+0+2+3)
tree.set(5,6); // sets 5th element to 6
std::cout << tree.query(4,7) << "\n"; // answer is 18 (7+6+2+3)
Documentation in separate file
()
- Constructs structure with 0 sets.
(unsigned int)
- Constructs structure with given number of sets.
Return: unsigned int
Arguments: none
Adds new element to the structure and returns its id.
Return: unsigned int
Arguments: unsigned int
Returns representative's id of the element with the given id.
Return: none
Arguments: unsigned int
, unsigned int
Merges sets to which given elements belong.
Return: unsigned int
Arguments: none
Returns number of elements in the structure.
Library include
#include "path/to/Basic-Data-Structures/include/disjoint_set"
Disjoint-set declaration
DisjointSet s; // size 0
DisjointSet s(5); // size 5
Basic find
and merge
usage
// {1} {2} {3} {4} {5}
std::cout << s.find(1) == s.find(2) << "\n"; // false
s.merge(1,2); // {1} {2} => {1,2}
// {1,2} {3} {4} {5}
std::cout << s.find(1) == s.find(2) << "\n"; // true
std::cout << s.find(2) == s.find(4) << "\n"; // false
s.merge(1,4); // {1,2} {4} => {1,2,4}
// {1,2,4} {3} {5}
std::cout << s.find(2) == s.find(4) << "\n"; // true