Skip to content

Commit

Permalink
Merge pull request #73 from aveldan/red-black-tree
Browse files Browse the repository at this point in the history
Added visualise for red black tree
  • Loading branch information
spirosmaggioros authored Jul 16, 2024
2 parents 66db487 + 365a07b commit b2c568a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
25 changes: 25 additions & 0 deletions examples/tree/red_black_tree.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifdef __cplusplus
#include "../../src/classes/tree/red_black_tree.h"
#include <iostream>
#endif

int main() {
red_black_tree<char> rb({'a','b','c'});
rb.insert('d');
rb.insert('t');
rb.remove('b');
// Will print the inorder traversal.
cout<<rb<<std::endl;

// Will return the preorder traversal.
std::vector<char> v = rb.preorder();
for(int i=0;i<v.size();i++)
std::cout<<v[i]<<" ";
std::cout<<std::endl;


// example for visualize.
red_black_tree<int> rb2({1, 2, 3, 4, 5, 6, 7, 8, 9});
rb2.visualize();
return 0;
}
51 changes: 51 additions & 0 deletions src/classes/tree/red_black_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define RED_BLACK_TREE_H

#ifdef __cplusplus
#include "../../visualization/tree_visual/tree_visualization.h"
#include <memory>
#include <bitset>
#include <vector>
Expand Down Expand Up @@ -260,6 +261,32 @@ template <typename T> class red_black_tree {
_preorder(callback, t_node->right);
}
}

std::string _vis_gen(std::shared_ptr<node> t_node, T parent_info) {
std::string _s = "";
if (std::is_same_v<T, char> || std::is_same_v<T, std::string>) {
_s += t_node->info + " [shape=circle fontcolor=black color=";
if(t_node->is_red == 1)
_s += "red";
else
_s += "black";
_s += "]\n";
_s += parent_info + "->" + t_node->info + '\n';
} else {
_s += std::to_string(t_node->info) + " [shape=circle fontcolor=black color=";
if(t_node->is_red == 1)
_s += "red";
else
_s += "black";
_s += "]\n";
_s += std::to_string(parent_info) + "->" + std::to_string(t_node->info) + '\n';
}
if (t_node->left)
_s += _vis_gen(t_node->left, t_node->info);
if (t_node->right)
_s += _vis_gen(t_node->right, t_node->info);
return _s;
}
public:
/**
*@brief Contructor for red black tree class.
Expand Down Expand Up @@ -473,6 +500,30 @@ template <typename T> class red_black_tree {
std::vector<T> ino = this->inorder();
return Iterator(ino.size(), ino);
}

/**
*@brief visualize function
*@returns .dot file that can be previewed using graphviz in vscode.
*/
void visualize() {
std::string _generated;
if(this->root){
if (std::is_same_v<T, char> || std::is_same_v<T, std::string>)
_generated += root->info;
else
_generated += std::to_string(root->info);
_generated += " [shape=circle fontcolor=black color=";
if(root->is_red == 1)
_generated += "red]\n";
else
_generated += "black]\n";
if(this->root->left)
_generated += this->_vis_gen(this->root->left, root->info);
if(this->root->right)
_generated += this->_vis_gen(this->root->right, root->info);
}
tree_visualization::visualize(_generated);
}
};


Expand Down

0 comments on commit b2c568a

Please sign in to comment.