Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/benami171/template_tree_container

Implementation of a template K-ary Tree container, with template iterators and template nodes. Used QT as GUI to print the tree.
https://github.com/benami171/template_tree_container

containers cpp gui iterators templates tree-structure

Last synced: about 1 month ago
JSON representation

Implementation of a template K-ary Tree container, with template iterators and template nodes. Used QT as GUI to print the tree.

Awesome Lists containing this project

README

        

# Iterators and Templates on trees.
In this project i implemented a template container that represents a K-ary tree that can hold Nodes of any type.

## How to run:
- run main program: in order to compile and run the program, type `qmake myProg.pro -config Prog` and than `make tree`.
- run tests: in order to compile and run the tests, type `qmake myProg.pro -config Test` and than `make test`.

## All the different classes and their hierarchy
- `tree.hpp` - a Template K-ary tree container, has a `Node* root`. uses the `iterators.hpp` to travers the tree in different ways (depends what K is the tree).
the `tree` class uses the `type_traits` library to use compile-time type manipulation based on certain conditions (see `Explenation about the tree container usage with iterators` below for additional explenation).
In order to print the tree using gui we used `QT` library.
- `node.hpp` - a Template Node class to hold any type.
- `complex.hpp` - represents complex numbers, can be used by `node.hpp` to store complex numbers as data.
- We have 6 different iterator classes inside the file `iterators.hpp`:
- `in_order_iterator` - Traverses Binary tree in in-order way, for any (K!=2)-Tree will travers the tree using dfs.
- `pre_order_iterator` - Traverses Binary tree in pre-order way, for any (K!=2)-Tree will travers the tree using dfs.
- `post_order_iterator` - Traverses Binary tree in post-order way, for any (K!=2)-Tree will travers the tree using dfs.
- `min_heap_iterator` - Turns a Binary tree to a minimum heap and travers it ,for any (K!=2)-Tree will travers the tree using dfs.
- `dfs_iterator` - Works on any K-tree.
- `bfs_iterator` - Works on any K-tree.

### Explenation about the tree container usage with iterators.
- A template class that represents a K-ary tree (Binary by default), holds an access to the root node and its `K` value that represents the highest degree that a node can have in the tree.
- during Compile-time the program will detect which type of tree the iterator gets called on, and if the iterators `in_order_iterator`, `pre_order_iterator` or `post_order_iterator` was called with a K!=2-Tree, it will direct the type of iterator to be `dfs_iterator` in Compile-time.

- How it does it:
- in the Tree class, we have used `type_traits` in this way:
```cpp
using iterator_pre_order = typename std::conditional, dfs_iterator>::type;
using iterator_post_order = typename std::conditional, dfs_iterator>::type;
using iterator_in_order = typename std::conditional, dfs_iterator>::type;
using iterator_min_heap = typename std::conditional, dfs_iterator>::type;
```

- So for example when we call `in_order_iterator` in the main, on 3-Ary array :
```cpp
Tree three_ary_tree;
for (auto node = three_ary_tree.begin_pre_order(); node != three_ary_tree.end_pre_order();++node) {
cout << node->get_value() << " ";
}
```
It will travers the tree using `bfs_iterator` and not `pre_order_iterator`.

- For further information and usage examples check out `main.cpp` ,`tree.hpp` ,`node.hpp` ,`iterators.hpp` and `complex.hpp`.
- You can see the different tests in the `test.cpp` class.