Last active
August 16, 2021 11:09
-
-
Save RashidLadj/8739c95ec2700e95617a6f2e14ebe49e to your computer and use it in GitHub Desktop.
class template representing the tree structure that can take any type in data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "treeClass.hpp" | |
ostream& operator<<(ostream& os, const vector<int>& myVector){ | |
os << "{ "; | |
for (int i = 0; i < myVector.size(); i++){ | |
if (i!=0) | |
os << ", " ; | |
os << myVector[i] << " " ; | |
} | |
os << "}"; | |
return os; | |
} | |
int main() { | |
tree<int>* mytree_int = new tree<int> (NULL, 5, 0); | |
tree<char>* mytree_char = new tree<char> (NULL, 't', 0); | |
tree<float>* mytree_float = new tree<float>(NULL, .5, 0); | |
tree<vector<int>>* mytree_vect = new tree<vector<int>>(NULL, {0, 1, 2}, 0); | |
cout << *mytree_int << endl; | |
cout << *mytree_char << endl; | |
cout << *mytree_float << endl; | |
cout << *mytree_vect << endl; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef TREE_H | |
#define TREE_H | |
#include <iostream> | |
#include <vector> | |
using namespace std; | |
template <typename T> | |
class tree{ | |
public: | |
/** constructors **/ | |
tree(tree<T>* fatherNode, T val, int depth); | |
tree(tree<T>* fatherNode, T val, int depth, vector<tree<T>>* sonNodes); | |
/** methods **/ | |
void addSon(tree<T>* son); | |
void addSon(T valSon); | |
/** Guetters **/ | |
vector<tree<T>>* getSons(); | |
tree<T>* getFather(); | |
template<typename U> | |
friend ostream& operator<<(ostream& os, const tree<U> &myTree); | |
private: | |
tree<T> *fatherNode; | |
T data; | |
int depth; | |
vector<tree<T>*> sonNodes; | |
}; | |
template <typename T> | |
tree<T>::tree(tree<T>* fatherNode, T val, int depth){ | |
this->fatherNode = fatherNode; | |
this->data = val; | |
this->depth = depth; | |
} | |
template <typename T> | |
tree<T>::tree(tree<T>* fatherNode, T val, int depth, vector<tree<T>>* sonNodes){ | |
tree(fatherNode, val, depth); | |
// this->sonNodes = &sonNodes; | |
} | |
template <typename T> | |
void tree<T>::addSon(tree<T>* son){ | |
assert(son->depth == (this->depth + 1)); | |
this->sonNodes.push_back(son); | |
} | |
template <typename T> | |
void tree<T>::addSon(T valSon){ | |
this->sonNodes.push_back(new tree(this, valSon, depth + 1)); | |
} | |
template <typename T> | |
vector<tree<T>>* tree<T>::getSons(){ | |
return this->sonNodes; | |
} | |
template <typename T> | |
tree<T>* tree<T>::getFather(){ | |
return this->fatherNode; | |
} | |
template <typename T> | |
ostream& operator<<(ostream& os, const tree<T>& myTree){ | |
tree<T> t = myTree; | |
os << "{" ; | |
os << "depth == " << myTree.depth << ", val == " << myTree.data << " " ; | |
if (t.sonNodes.size() != 0){ | |
for (const auto &t_: t.sonNodes) | |
os << t_ ; | |
} | |
os << "}"; | |
return os; | |
} | |
#endif // TREE_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment