Skip to content

Commit

Permalink
Added << operators for every tree header
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Jul 4, 2024
1 parent 9a6eab5 commit 7f17d18
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 12 deletions.
18 changes: 18 additions & 0 deletions src/classes/tree/avl_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,24 @@ template <typename T> class avl_tree {
tree_visualization::visualize(__generated);
}

/**
* @brief operator << for avl_tree class
*/
friend ostream & operator << (ostream &out, avl_tree<T> &t){
std::vector<std::vector<T> > order = t.level_order();
for(std::vector<T> & x : order){
for(size_t i = 0; i < x.size(); i++){
if(i != x.size() - 1){
out << x[i] << ", ";
}
else{
out << x[i] << '\n';
}
}
}
return out;
}

private:
/**
*@brief Struct for the node type pointer.
Expand Down
19 changes: 19 additions & 0 deletions src/classes/tree/bst.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,25 @@ template <typename T> class bst {
tree_visualization::visualize(__generated);
}


/**
* @brief operator << for bst class
*/
friend ostream & operator << (ostream &out, bst<T> &t){
std::vector<std::vector<T> > order = t.level_order();
for(std::vector<T> & x : order){
for(size_t i = 0; i < x.size(); i++){
if(i != x.size() - 1){
out << x[i] << ", ";
}
else{
out << x[i] << '\n';
}
}
}
return out;
}

private:
/**
*@brief Struct for the node type pointer.
Expand Down
23 changes: 12 additions & 11 deletions src/classes/tree/interval_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,20 @@ template <typename T> class interval_tree {
}

/**
*@brief << operator for interval_tree class.
* @brief operator << for interval tree class
*/
friend std::ostream &operator<<(std::ostream &out, interval_tree<T> &t) {
if (!t.root) {
out << "";
return out;
}
out << '"';
std::vector<std::pair<T, T>> __inorder = t.inorder();
for (auto &x : __inorder) {
out << '"' << x.first << ' ' << x.second << '"' << " ";
friend ostream & operator << (ostream &out, interval_tree<T> &t){
std::vector<std::vector<std::pair<T, T> > > order = t.level_order();
for(std::vector<std::pair<T, T> > & x : order){
for(size_t i = 0; i < x.size(); i++){
if(i != x.size() - 1){
out << '[' << x[i].first << "," << x[i].second << ']' << ", ";
}
else{
out << '[' << x[i].first << "," << x[i].second << ']' << '\n';
}
}
}
out << '"';
return out;
}

Expand Down
20 changes: 19 additions & 1 deletion src/classes/tree/splay_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,24 @@ template <typename T> class splay_tree {
tree_visualization::visualize(__generated);
}

/**
* @brief operator << for splay tree class
*/
friend ostream & operator << (ostream &out, splay_tree<T> &t){
std::vector<std::vector<T> > order = t.level_order();
for(std::vector<T> & x : order){
for(size_t i = 0; i < x.size(); i++){
if(i != x.size() - 1){
out << x[i] << ", ";
}
else{
out << x[i] << '\n';
}
}
}
return out;
}

private:
std::shared_ptr<node> rrotate(std::shared_ptr<node> _node) {
std::shared_ptr<node> y = _node->left;
Expand Down Expand Up @@ -450,4 +468,4 @@ template <typename T> class splay_tree<T>::Iterator {
T operator*() { return elements[index]; }
};

#endif
#endif
19 changes: 19 additions & 0 deletions src/classes/tree/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define TREE_H

#ifdef __cplusplus
#include "../../visualization/tree_visual/tree_visualization.h"
#include <functional>
#include <iostream>
#include <queue>
Expand Down Expand Up @@ -178,6 +179,24 @@ template <typename T> class tree {
return path;
}

/**
* @brief operator << for bst class
*/
friend ostream & operator << (ostream &out, tree<T> &t){
std::vector<std::vector<T> > order = t.level_order();
for(std::vector<T> & x : order){
for(size_t i = 0; i < x.size(); i++){
if(i != x.size() - 1){
out << x[i] << ", ";
}
else{
out << x[i] << '\n';
}
}
}
return out;
}

private:
void __inorder(std::function<void(std::shared_ptr<node>)> callback,
std::shared_ptr<node> root) {
Expand Down

0 comments on commit 7f17d18

Please sign in to comment.