Skip to content

Commit

Permalink
Merge pull request #74 from aveldan/vis_tree.h
Browse files Browse the repository at this point in the history
Added visualise to tree.h
  • Loading branch information
spirosmaggioros authored Jul 26, 2024
2 parents ba20cf6 + 32f1749 commit 3fdf875
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@
build/

# cache folder
.cache/
.cache/
18 changes: 18 additions & 0 deletions examples/tree/tree.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "../../src/classes/tree/tree.h"

int main() {
tree<int> t;
t.insert("", 10);
t.insert("r", 15);
t.insert("l", 20);
t.insert("rr", 13);
t.insert("rrr", 12);
t.insert("rrl", 11);

std::vector<int> ino = t.inorder();
for(auto & x : ino){
std::cout << x << " ";
}
std::cout << '\n';
t.visualize();
}
7 changes: 7 additions & 0 deletions examples/tree/unnamed.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
digraph Tree {
10->20
10->15
15->13
13->11
13->12
}
46 changes: 46 additions & 0 deletions src/classes/tree/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ template <typename T> class tree {
return out;
}

/**
*@brief visualize function
*@returns .dot file that can be previewed using graphviz in vscode.
*/
void visualize() {
std::string _generated = generate_visualization();
tree_visualization::visualize(_generated);
}

private:
void _inorder(std::function<void(std::shared_ptr<node>)> callback,
std::shared_ptr<node> root) {
Expand Down Expand Up @@ -222,6 +231,43 @@ template <typename T> class tree {
_postorder(callback, root->left);
}
}

std::string generate_visualization() {
std::string _generate = _inorder_gen(root);
return _generate;
}

std::string _inorder_gen(std::shared_ptr<node> root) {
std::string _s;
if (std::is_same_v<T, char> || std::is_same_v<T, std::string>) {
if (root->left) {
_s += root->info;
_s += "->";
_s += root->left->info;
_s += "\n";
_s += _inorder_gen(root->left);
}
if (root->right) {
_s += root->info;
_s += "->";
_s += root->right->info;
_s += "\n";
_s += _inorder_gen(root->right);
}
} else {
if (root->left) {
_s += std::to_string(root->info) + "->" +
std::to_string(root->left->info) + "\n" +
_inorder_gen(root->left);
}
if (root->right) {
_s += std::to_string(root->info) + "->" +
std::to_string(root->right->info) + "\n" +
_inorder_gen(root->right);
}
}
return _s;
}
};

template <typename T> class tree<T>::Iterator {
Expand Down
1 change: 1 addition & 0 deletions src/include/algoplus.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "../classes/list/skip_list.h"
#include "../classes/queue/dequeue_list.h"
#include "../classes/stack/stack_list.h"
#include "../classes/tree/tree.h"
#include "../classes/tree/avl_tree.h"
#include "../classes/tree/bst.h"
#include "../classes/tree/interval_tree.h"
Expand Down

0 comments on commit 3fdf875

Please sign in to comment.