title: Generate random tree using JavaScript
body: <p>I am working on a project to generate random data structures for testing solutions for DSA problems. I am trying to form an algorithm that generates a random tree data structure that takes in the input of <em>number of test cases</em> and <em>number of nodes</em>. Since I cannot use pointers and references, I'm having trouble figuring out how to do this in javaScript.</p>
  <p>So far I managed to get the basics down, however, I'm getting errors in my code</p>
  <h2><strong>CODE:</strong></h2>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const randnumgen = (min, max) =&gt; {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
  };
  
  function randtreegen(nodes) {
  
    var string = "";
  
    class Tree {
  
      constructor(nodes) {
        this.nodes = nodes;
        this.adj = [];
      }
      addEdge(n, w) {
        this.adj[n].push(w);
      }
      removeEdge(n, w) {
        this.adj[n].forEach((elem) =&gt; {
          if (elem === w) {
            var index = this.adj[n].indexOf(elem);
            if (index !== -1) this.adj[n].splice(index, 1);
          }
        });
      }
      isCyclicUtil(nodes, visited, recStack) {
        if (visited[nodes] === false) {
          visited[nodes] = true;
          recStack[nodes] = true;
  
          this.adj[n].forEach((elem) =&gt; {
            if (!visited[elem] &amp;&amp; this.isCyclicUtil(elem, visited, recStack))
              return true;
            else if (recStack[elem])
              return true;
          });
        }
        recStack[nodes] = false;
        return false;
      }
      isCyclic() {
  
        visited = new Array();
        recStack = new Array();
  
        for (var i = 0; i &lt; this.nodes; i++) {
          visited[i] = false;
          recStack[i] = false;
        }
  
        for (var j = 0; j &lt; this.nodes; i++) {
          if (this.isCyclicUtil(j, visited, recStack))
            return true;
        }
  
        return false;
      }
    }
  
    container = new Set();
    let t = new Tree(nodes);
    for (var i = 1; i &lt; nodes - 1; i++) {
      var a = randnumgen(1, nodes);
      var b = randnumgen(1, nodes);
      var p = [a, b];
  
      t.addEdge(p[0], p[1]);
  
      while (container.has(`${p[0]},${p[1]}`) || t.isCyclic() === true) {
        t.removeEdge(p[0], p[1]);
  
        var a = randnumgen(1, nodes);
        var b = randnumgen(1, nodes);
        var p = [a, b];
  
        t.addEdge(p[0], p[1]);
      }
      container.add(`${p[0]},${p[1]}`);
    }
  
    container.forEach((elem) =&gt; {
      string += elem + '\n'
    });
    return string;
  }
  
  
  function treeGen(test_case, tree_nodes) {
    var result = "";
    while (test_case-- &gt; 0) {
      result += randtreegen(tree_nodes) + '\n';
    }
    return result;
  }
  
  const ans = treeGen(1, 5);
  
  document.write(ans);</code></pre>
  </div>
  </div>
  </p>
  <hr />
  <h1>ERROR</h1>
  <pre><code>TypeError: Cannot read property 'push' of undefined at /home/cg/root/7217808/main.js:18
          this.adj[n].push(w);
  </code></pre>
  <h2>My question is:</h2>
  <ul>
  <li>Is the Logic correct?</li>
  <li>How to resolve the error to make it work?</li>
  </ul>
  <p>P.S: I referred to <a href="https://www.geeksforgeeks.org/test-case-generation-set-4-random-directed-undirected-weighted-and-unweighted-graphs/" rel="nofollow noreferrer" title="article">this</a> article on GeeksforGeeks.</p>