{"id":18,"date":"2011-05-17T22:24:26","date_gmt":"2011-05-18T03:24:26","guid":{"rendered":"http:\/\/www.playwithlua.com\/?p=18"},"modified":"2020-05-22T13:20:46","modified_gmt":"2020-05-22T18:20:46","slug":"enumerating-all-trees","status":"publish","type":"post","link":"http:\/\/www.playwithlua.com\/?p=18","title":{"rendered":"Enumerating All Trees"},"content":{"rendered":"<p>This is a problem in <em>The Art of Computer Programming<\/em> volume 4A, which only came out a few months ago. My solution isn&#8217;t Knuth&#8217;s solution, mostly because I don&#8217;t <em>understand<\/em> Knuth&#8217;s solution, but it works and it&#8217;s fairly easy to explain.<\/p>\n<p>The problem is this: given a number of nodes (like, say, 4), generate every possible arrangement of those nodes into a tree. So this is one:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lisp\" style=\"font-family:monospace;\"><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<p><img src=\"http:\/\/www.playwithlua.com\/wp-content\/tfo-graphviz\/021502f34df3ef2937b7a9c2b40c7ac4.png\" class=\"graphviz\" \/><br \/>\nThis is another one:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lisp\" style=\"font-family:monospace;\"><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<p><img src=\"http:\/\/www.playwithlua.com\/wp-content\/tfo-graphviz\/cf0f46f55fbff849e1d16525354c9b87.png\" class=\"graphviz\" \/><br \/>\nAnd so on. For n nodes there are 2<sup>n-1<\/sup> possible trees. And it&#8217;s not immediately obvious how to generate them.<br \/>\n<!--more--><br \/>\nKnuth&#8217;s solution builds the strings directly, through some magic I don&#8217;t quite understand. I <em>think<\/em> he&#8217;s doing the same general thing I am, but he&#8217;s got a way to do it directly, whereas I&#8217;m going to build the trees in memory first, out of Lua tables, and then print strings from them.<\/p>\n<p>So, first I want a simple way to print the strings. This function takes a table and returns the parentheses that represent it:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\"><span style=\"color: #aa9900; font-weight: bold;\">function<\/span> tree_str<span style=\"color: #66cc66;\">&#40;<\/span>tree<span style=\"color: #66cc66;\">&#41;<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">if<\/span> <span style=\"color: #66cc66;\">#<\/span>tree <span style=\"color: #66cc66;\">==<\/span> <span style=\"color: #cc66cc;\">0<\/span> <span style=\"color: #aa9900; font-weight: bold;\">then<\/span> <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> <span style=\"color: #ff6666;\">&quot;&quot;<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">else<\/span>\n\t  <span style=\"color: #aa9900; font-weight: bold;\">local<\/span> subtrees <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #66cc66;\">&#123;<\/span><span style=\"color: #66cc66;\">&#125;<\/span>\n&nbsp;\n\t  <span style=\"color: #aa9900; font-weight: bold;\">for<\/span> _<span style=\"color: #66cc66;\">,<\/span> subtree <span style=\"color: #aa9900; font-weight: bold;\">in<\/span> <span style=\"color: #0000aa;\">ipairs<\/span><span style=\"color: #66cc66;\">&#40;<\/span>tree<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #aa9900; font-weight: bold;\">do<\/span>\n\t\t <span style=\"color: #0000aa;\">table.insert<\/span><span style=\"color: #66cc66;\">&#40;<\/span>subtrees<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff6666;\">&quot;(&quot;<\/span> <span style=\"color: #66cc66;\">..<\/span> tree_str<span style=\"color: #66cc66;\">&#40;<\/span>subtree<span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">..<\/span> <span style=\"color: #ff6666;\">&quot;)&quot;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n\t  <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n&nbsp;\n\t  <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> <span style=\"color: #0000aa;\">table.concat<\/span><span style=\"color: #66cc66;\">&#40;<\/span>subtrees<span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #ff6666;\">&quot; &quot;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n<span style=\"color: #aa9900; font-weight: bold;\">end<\/span><\/pre><\/div><\/div><\/div>\n\n<p>We can test it like this:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\">tree_str<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#123;<\/span> <span style=\"color: #66cc66;\">&#123;<\/span><span style=\"color: #66cc66;\">&#125;<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #66cc66;\">&#123;<\/span><span style=\"color: #66cc66;\">&#125;<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #66cc66;\">&#123;<\/span><span style=\"color: #66cc66;\">&#123;<\/span><span style=\"color: #66cc66;\">&#125;<\/span><span style=\"color: #66cc66;\">,<\/span> <span style=\"color: #66cc66;\">&#123;<\/span><span style=\"color: #66cc66;\">&#125;<\/span><span style=\"color: #66cc66;\">&#125;<\/span><span style=\"color: #66cc66;\">&#125;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #808080; font-style: italic;\">--&amp;gt; &quot;() () (() ())&quot;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>Simple enough. Now that that&#8217;s out of the way, how do we actually do this? Well, let&#8217;s first try to figure it out inductively. If we have the set of trees for n-1 nodes, and we add an nth node, we can figure it will double the number of trees: each tree of n-1 can either be a descendent of the new node or a sibling of it.<\/p>\n<p>Since there is only one tree for the first node, and each one doubles the count, there are\u00a02<sup>n-1<\/sup> trees. If we can figure out some 1-to-1 correlation between bit fields and trees, we can just count from 1 to 2<sup>n-1<\/sup>-1 and generate the tree for each one. So, how to do that?<\/p>\n<p>The way I figured out is, each node&#8217;s bit determines whether it&#8217;s a sibling or a child of the previous node. We keep a <code>current_node<\/code> reference, and if the bit is zero, we push a new table into it; if it&#8217;s 1, we find <code>current_node<\/code>&#8216;s last child, make that the new <code>current_node<\/code>, and then add a child to that.<\/p>\n<p>Right away we run into a problem, because Lua doesn&#8217;t have any bitwise operators. So let&#8217;s make a quick function to dump out all the bits in a number:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\"><span style=\"color: #aa9900; font-weight: bold;\">function<\/span> bits<span style=\"color: #66cc66;\">&#40;<\/span>n<span style=\"color: #66cc66;\">&#41;<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">local<\/span> b <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #66cc66;\">&#123;<\/span><span style=\"color: #66cc66;\">&#125;<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">while<\/span> n &amp;gt<span style=\"color: #66cc66;\">;<\/span> <span style=\"color: #cc66cc;\">0<\/span> <span style=\"color: #aa9900; font-weight: bold;\">do<\/span>\n\t  b<span style=\"color: #66cc66;\">&#91;<\/span><span style=\"color: #66cc66;\">#<\/span>b<span style=\"color: #66cc66;\">+<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #66cc66;\">&#93;<\/span> <span style=\"color: #66cc66;\">=<\/span> n<span style=\"color: #66cc66;\">%<\/span><span style=\"color: #cc66cc;\">2<\/span>\n\t  n <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #66cc66;\">&#40;<\/span>n <span style=\"color: #66cc66;\">-<\/span> n<span style=\"color: #66cc66;\">%<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">\/<\/span> <span style=\"color: #cc66cc;\">2<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> b\n<span style=\"color: #aa9900; font-weight: bold;\">end<\/span><\/pre><\/div><\/div><\/div>\n\n<p>We don&#8217;t care what the indices are, so we may as well have them be 1-indexed so that we can use <code>ipairs<\/code>. We can use this function to generate all the trees, like this:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\"><span style=\"color: #aa9900; font-weight: bold;\">function<\/span> make_tree<span style=\"color: #66cc66;\">&#40;<\/span>node_count<span style=\"color: #66cc66;\">,<\/span> n<span style=\"color: #66cc66;\">&#41;<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">local<\/span> b <span style=\"color: #66cc66;\">=<\/span> bits<span style=\"color: #66cc66;\">&#40;<\/span>n<span style=\"color: #66cc66;\">&#41;<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">local<\/span> nodes <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #66cc66;\">&#123;<\/span> <span style=\"color: #66cc66;\">&#123;<\/span><span style=\"color: #66cc66;\">&#125;<\/span> <span style=\"color: #66cc66;\">&#125;<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">local<\/span> current_node <span style=\"color: #66cc66;\">=<\/span> nodes\n&nbsp;\n   <span style=\"color: #aa9900; font-weight: bold;\">while<\/span> node_count &amp;gt<span style=\"color: #66cc66;\">;<\/span> <span style=\"color: #cc66cc;\">1<\/span> <span style=\"color: #aa9900; font-weight: bold;\">do<\/span>\n\t  node_count <span style=\"color: #66cc66;\">=<\/span> node_count <span style=\"color: #66cc66;\">-<\/span> <span style=\"color: #cc66cc;\">1<\/span>\n\t  <span style=\"color: #aa9900; font-weight: bold;\">if<\/span> b<span style=\"color: #66cc66;\">&#91;<\/span>node_count<span style=\"color: #66cc66;\">&#93;<\/span> <span style=\"color: #66cc66;\">==<\/span> <span style=\"color: #cc66cc;\">1<\/span> <span style=\"color: #aa9900; font-weight: bold;\">then<\/span>\n\t\t current_node <span style=\"color: #66cc66;\">=<\/span> current_node<span style=\"color: #66cc66;\">&#91;<\/span><span style=\"color: #66cc66;\">#<\/span>current_node<span style=\"color: #66cc66;\">&#93;<\/span>\n\t  <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n\t  current_node<span style=\"color: #66cc66;\">&#91;<\/span><span style=\"color: #66cc66;\">#<\/span>current_node <span style=\"color: #66cc66;\">+<\/span> <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #66cc66;\">&#93;<\/span> <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #66cc66;\">&#123;<\/span><span style=\"color: #66cc66;\">&#125;<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">end<\/span>\n&nbsp;\n   <span style=\"color: #aa9900; font-weight: bold;\">return<\/span> nodes\n<span style=\"color: #aa9900; font-weight: bold;\">end<\/span><\/pre><\/div><\/div><\/div>\n\n<p>The loop is structured a little weirdly because we want to skip the first node; it can&#8217;t possibly be a child of the previous node even if it&#8217;s a 1.<\/p>\n<p>So now, we can generate a single tree, so let&#8217;s loop through to make all the trees:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lua\" style=\"font-family:monospace;\"><span style=\"color: #aa9900; font-weight: bold;\">for<\/span> n <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #66cc66;\">,<\/span><span style=\"color: #cc66cc;\">7<\/span> <span style=\"color: #aa9900; font-weight: bold;\">do<\/span>\n   <span style=\"color: #aa9900; font-weight: bold;\">local<\/span> str <span style=\"color: #66cc66;\">=<\/span> tree_str<span style=\"color: #66cc66;\">&#40;<\/span>make_tree<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #cc66cc;\">4<\/span><span style=\"color: #66cc66;\">,<\/span> n<span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n   <span style=\"color: #0000aa;\">print<\/span><span style=\"color: #66cc66;\">&#40;<\/span>str<span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #aa9900; font-weight: bold;\">end<\/span><\/pre><\/div><\/div><\/div>\n\n<p>And we see:<\/p>\n\n<div class=\"my_syntax_box\"><div class=\"my_syntax\"><div class=\"code\"><pre class=\"lisp\" style=\"font-family:monospace;\"><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span> <span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span>\n<span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#40;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><span style=\"color: #66cc66;\">&#41;<\/span><\/pre><\/div><\/div><\/div>\n\n<p>And there, we&#8217;ve enumerated all the possible trees with four nodes! <a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/gist.github.com\/977940');\"  href=\"http:\/\/gist.github.com\/977940\">Here&#8217;s the code.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a problem in The Art of Computer Programming volume 4A, which only came out a few months ago. My solution isn&#8217;t Knuth&#8217;s solution, mostly because I don&#8217;t understand Knuth&#8217;s solution, but it works and it&#8217;s fairly easy to explain. The problem is this: given a number of nodes (like, say, 4), generate every [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/posts\/18"}],"collection":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18"}],"version-history":[{"count":0,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=\/wp\/v2\/posts\/18\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.playwithlua.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}