0

I have written a code for a coding question on the leetcode. Question is that we are given a vector and we have to find all the permutation of that vector. I have tried this question using bitmasking.

My code:

#include<bits/stdc++.h>
using namespace std;

vector<vector<int>> vec;
vector<int> v1;
vector<int> nums{ 1, 2, 3 };
void fun(int mask, vector<int> nums)
{
    int n = nums.size();
    int j;
    if (mask == 0)
    {
        vec.push_back(v1);
        for(int i=0;i<v1.size();i++)
        {
            cout << v1[i] << " ";
        }
        cout << endl;
        return;
    }
    for (j = 0; j < n; j++)
    {
        if (mask & (1 << j))
        {
            v1.push_back(nums[j]);
            fun(mask ^ (1 << j), nums);
            v1.pop_back();
        }
    }
    return;
}

int main()
{
    int n = 3;
    int mask = (1 << (n)) - 1;

    fun(mask, nums);
}

Somehow,i am getting all the permutation of the given permutation. My only question is that I am not able to draw the recursive tree for this question. Is there any website or app available online, in which I can insert my above-mentioned code and that website or app will produce the recursive tree online???

Can anyone please help me in finding any such websites to draw and visualize recursive tree online?

Ranoiaetep
  • 5,872
  • 1
  • 14
  • 39
  • Maybe, [Graphviz Online](https://dreampuf.github.io/GraphvizOnline/). Though, I'm not sure how to open the website (with the resp. graph) from another online compiler... – Scheff's Cat Nov 12 '20 at 06:17
  • @Scheff can i insert my above c++ code in that? –  Nov 12 '20 at 06:19
  • No. You can add the output of your graph in that graphviz language to your application. Then you might copy the output into the web site or even append the output (as fragment) to the URI or the website. (Have a look into the address field of your browser after clicking the above link to see what I mean.) – Scheff's Cat Nov 12 '20 at 06:21
  • @Scheff i am asking for that website or app which can take c++ code and frame a recursive tree as per the code. –  Nov 12 '20 at 06:23
  • Yeah - understood. I'm not quite sure whether you will find something special (because I doubt that this exists). I would like to find an online compiler for Qt GUI applications but I accepted that this will probably be hard to find as well. Sorry, for bothering you... ;-) – Scheff's Cat Nov 12 '20 at 06:25
  • I would doubt that there's any website or app that follow exactly what you want. Non-related, you can use [`std::next_permutation`](https://en.cppreference.com/w/cpp/algorithm/next_permutation) to get all permutations – Ranoiaetep Nov 12 '20 at 06:26
  • In case, you have to fall back to my stupid idea: [Visualize C++ Data Structures using Graphviz and the DOT language](https://nirav.com.np/2019/12/08/visualize-c-data-structures-using-graphviz-and-dot-language.html)... – Scheff's Cat Nov 12 '20 at 06:29

0 Answers0