Skip to content

Commit

Permalink
Merge pull request #65 from aveldan/visualise
Browse files Browse the repository at this point in the history
Adding visualise for skiplist
  • Loading branch information
spirosmaggioros authored Jul 7, 2024
2 parents 436fef6 + 1798973 commit 9ebc38c
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 14 deletions.
3 changes: 2 additions & 1 deletion examples/list/skip_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ int main() {
s.insert(5);
s.insert(6);
std::cout << s << '\n';
}
s.visualize();
}
37 changes: 24 additions & 13 deletions examples/list/unnamed.dot
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
digraph list {
rankdir=LR;
node [shape=record;]
3 [label=<{ 3 | 3 | }>] ;
2 [label=<{ 2 | 2 | }>] ;
4 [label=<{ 4 | 2 | }>] ;
1 [label=<{ 1 | 1 | }>] ;
5 [label=<{ 5 | 1 | }>] ;
3:ref -> 2:data [arrowhead=vee, arrowtail=dot, dir=both];
2:data -> 3:ref [arrowhead=vee, arrowtail=dot, dir=both];
2:ref -> 4:data [arrowhead=vee, arrowtail=dot, dir=both];
4:data -> 2:ref [arrowhead=vee, arrowtail=dot, dir=both];
4:ref -> 1:data [arrowhead=vee, arrowtail=dot, dir=both];
1:data -> 4:ref [arrowhead=vee, arrowtail=dot, dir=both];
1:ref -> 5:data [arrowhead=vee, arrowtail=dot, dir=both];
5:data -> 1:ref [arrowhead=vee, arrowtail=dot, dir=both];
root [label="<3> root | <2> root | <1> root | <0> root"];
NULL [label="<3> NULL | <2> NULL | <1> NULL | <0> NULL"];
root:3 -> NULL:3 ;
0 [label="<2> 0 | <1> 0 | <0> 0"];
root:2 -> 0:2 ;
1 [label="<2> 1 | <1> 1 | <0> 1"];
0:2 -> 1:2 ;
5 [label="<2> 5 | <1> 5 | <0> 5"];
1:2 -> 5:2 ;
5:2 -> NULL:2 ;
root:1 -> 0:1 ;
0:1 -> 1:1 ;
1:1 -> 5:1 ;
5:1 -> NULL:1 ;
root:0 -> 0:0 ;
0:0 -> 1:0 ;
4 [label="<0> 4"];
1:0 -> 4:0 ;
4:0 -> 5:0 ;
6 [label="<0> 6"];
5:0 -> 6:0 ;
13 [label="<0> 13"];
6:0 -> 13:0 ;
13:0 -> NULL:0 ;
}
77 changes: 77 additions & 0 deletions src/classes/list/skip_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#define SKIP_LIST_H

#ifdef __cplusplus
#include "../../visualization/list_visual/linked_list_visualization.h"
#include <iostream>
#include <memory>
#include <stdexcept>
#include <vector>
#include <unordered_set>
#endif

/**
Expand Down Expand Up @@ -170,6 +172,15 @@ template <typename T> class skip_list {
return false;
}

/**
* @brief visualize function
* returns a .dot file that can be previewd with graphviz plugin in vscode
*/
void visualize(){
std::string generated = this->generate();
linked_list_visualization::visualize(generated);
}

/**
*@brief operator << for skip_list<T> class.
*/
Expand Down Expand Up @@ -220,6 +231,72 @@ template <typename T> class skip_list {

int level{0};
std::shared_ptr<node> root;

std:: string generate_node(std::string node_val, int levs){
std::string gen;
gen += node_val;
gen += " [label=\"<";
gen += to_string(levs);
gen += "> ";
gen += node_val;
for(int i=levs-1;i>=0;i--){
gen += " | <";
gen += to_string(i);
gen += "> ";
gen += node_val;
}
gen += "\"];";
gen += '\n';
return gen;
}

std::string generate_edge(std::string prev_val, std::string curr_val, int lev){
std::string gen;
gen += prev_val;
gen += ':';
gen += to_string(lev);
gen += " -> ";
gen += curr_val;
gen += ':';
gen += to_string(lev);
gen += " ;\n";
return gen;
}

std::string generate() {
std::string gen;
gen += "rankdir=LR;";
gen += '\n';
gen += "node [shape=record;]";
gen += '\n';
unordered_set<std::string> S;
int m_level = min(level, (int)root->next.size()); // See if this is a parameter
gen += generate_node("root", m_level+1);
gen += generate_node("NULL", m_level+1);
gen += generate_edge("root", "NULL", m_level+1);
for(int i=m_level;i>=0;i--){
std::shared_ptr<node> head = root;
head = head->next[i];
std::string prev_val = "root";
std::string head_key;
while(head){
if(std::is_same_v<T, std::string>){
head_key = head->key;
} else {
head_key = to_string(head->key);
}
if(S.find(head_key) == S.end()){
S.insert(head_key);
gen += generate_node(head_key, i);
}
gen += generate_edge(prev_val, head_key, i);
prev_val = head_key;
head = head->next[i];
}
gen += generate_edge(prev_val, "NULL", i);
}
return gen;
}
};

/**
Expand Down

0 comments on commit 9ebc38c

Please sign in to comment.