<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Every Tree There Is</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/04/22/every-tree-there-is.aspx</link><description>[This is part of a series on generating every string in a language. The previous part is here . The next part is here .] Last time we talked about how the number of binary trees with n nodes is C(n), where C(n) is the nth Catalan number. I asked if there</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Every Tree There Is</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/04/22/every-tree-there-is.aspx#10007833</link><pubDate>Wed, 05 May 2010 15:43:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10007833</guid><dc:creator>jsrfc58</dc:creator><description>&lt;p&gt;Oops...I meant to write &amp;quot;ternary tree&amp;quot; not &amp;quot;tertiary tree&amp;quot;. And...a four-node tree would be a quadtree, etc.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10007833" width="1" height="1"&gt;</description></item><item><title>re: Every Tree There Is</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/04/22/every-tree-there-is.aspx#10007825</link><pubDate>Wed, 05 May 2010 15:33:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10007825</guid><dc:creator>jsrfc58</dc:creator><description>&lt;p&gt;Eric wrote: &amp;quot;Binary trees have more structure than arbitrary trees! There are two binary trees with two nodes: one with the child on the right of the root, one with the child on the left of the root. There is only one arbitrary tree with two nodes; there is no difference between the &amp;quot;left&amp;quot; and &amp;quot;right&amp;quot; child.&amp;quot;&lt;/p&gt;
&lt;p&gt;I think this argument depends on what function you are using to generate new nodes in the tree.&lt;/p&gt;
&lt;p&gt;For example, if you have a binary tree, and you are adding a new value, you could create a new node on the left if the incoming value is less than the current node value. That rule is arbitrary, however, because you could reverse that and instead create a new node on the right. Now does that constitute one binary tree, or two?&lt;/p&gt;
&lt;p&gt;Also, with arbitrary trees, it depends on how you define them. To say there is no difference between the left and the right child really depends on what type of arbitrary tree you are describing. If you have some type of function which determines how new nodes are created, they may in fact have more structure than a binary tree.&lt;/p&gt;
&lt;p&gt;For instance, if an incoming value is compared to an existing node, you could have it create a left node if the value is less, a right node if the value is greater, and a middle node if the value is equal. (would that make it a tertiary tree?) &lt;/p&gt;
&lt;p&gt;Anyway, you could scale this &amp;quot;gateway&amp;quot; function so that four, five, or six (or more) nodes are created. I would argue that a six-node tree would have more structure than a binary tree in this case. I haven't done the math on it, but would it possibly have 6! different configurations?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10007825" width="1" height="1"&gt;</description></item><item><title>re: Every Tree There Is</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/04/22/every-tree-there-is.aspx#10002768</link><pubDate>Mon, 26 Apr 2010 20:48:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10002768</guid><dc:creator>hp</dc:creator><description>&lt;p&gt;Generating all possible pictures:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://aboutintelligence.blogspot.com/2008/05/generating-all-possible-pictures.html"&gt;http://aboutintelligence.blogspot.com/2008/05/generating-all-possible-pictures.html&lt;/a&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10002768" width="1" height="1"&gt;</description></item><item><title>re: Every Tree There Is</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/04/22/every-tree-there-is.aspx#10001843</link><pubDate>Sat, 24 Apr 2010 02:22:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10001843</guid><dc:creator>Pop Catalin</dc:creator><description>&lt;p&gt;It's interesting that for binary trees the following are considered distinct configurations&lt;/p&gt;
&lt;p&gt; &amp;nbsp; A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; A&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; \ &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; / &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \&lt;/p&gt;
&lt;p&gt; &amp;nbsp; C &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; C&lt;/p&gt;
&lt;p&gt;but if they are viewed as general trees they are not distinct, it's the same configuration. &lt;/p&gt;
&lt;p&gt;There's a distinction in the definitions given for the binary and arbitrary trees, the binary tree definition contains node position information (which also implies ordering), but the definition of general trees only contains ordering information. (without position information)&lt;/p&gt;
&lt;p&gt;If we were to define the the binary trees without any position information, something like : a binary tree is a tree where every node has a maximum of two child nodes (maybe as a linked list) then the number of possible configurations &amp;nbsp;is reduced by a large factor. (Intuitively I think 2n!, but I need to verify)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10001843" width="1" height="1"&gt;</description></item><item><title>re: Every Tree There Is</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/04/22/every-tree-there-is.aspx#10001818</link><pubDate>Sat, 24 Apr 2010 00:53:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10001818</guid><dc:creator>Mark Knell</dc:creator><description>&lt;p&gt;&amp;gt; No, that is exactly the opposite of what I need. If A is {} and B is {{}} then I want {{}{{}}} and {{{}}{}} to be different trees. An ordered list gives me exactly the ordering I need.&lt;/p&gt;
&lt;p&gt;My version does distinguish {{}{{}}} from {{{}}{}}, fwiw, so I don't think it's the opposite of what you need. &amp;nbsp;It would produce the exact results you require. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;But, it is unnecessarily complex. I think I've both introduced a wrinkle and compensated for it, when I should have let well enough alone.&lt;/p&gt;
&lt;p&gt;Consider two graphs: &lt;/p&gt;
&lt;p&gt; &amp;nbsp;A&lt;/p&gt;
&lt;p&gt;/ | \ &lt;/p&gt;
&lt;p&gt;B C D&lt;/p&gt;
&lt;p&gt;|&lt;/p&gt;
&lt;p&gt;E&lt;/p&gt;
&lt;p&gt;And&lt;/p&gt;
&lt;p&gt; &amp;nbsp;A&lt;/p&gt;
&lt;p&gt;/ | \ &lt;/p&gt;
&lt;p&gt;B D C&lt;/p&gt;
&lt;p&gt;|&lt;/p&gt;
&lt;p&gt;E&lt;/p&gt;
&lt;p&gt;I thought a plausible reading of your original definition would say that in the first case, A's list of children is {B, C, D}, in the second it is {B, D, C}. &amp;nbsp;These are different lists, but you wish them to describe the same tree. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;However, this confers more differentiable identity to the nodes than I should. You've shown trees with labels, but the labels have been based on positions, i.e., labeled after order is fixed. &amp;nbsp;My instinct is to quibble with this, saying, &amp;quot;Yeah, but what if we reverse the order in which we visit the leafs under A; it's the same 'shape' but with different 'order'.&amp;quot; &amp;nbsp;Upon reflection, I think that's begging the question. &amp;nbsp;It already assumes that node identity is meaningful apart from position. &amp;nbsp;That's something I cooked up; you don't distinguish nodes in any way other than their subtrees and their order relative to their siblings. &amp;nbsp;It's reminiscent of the story about the three baseball umpires (which you've featured). &amp;nbsp;To paraphrase the oldest umpire, your nodes ain't nothing until you've ordered them. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;I've probably spent too much time serializing things lately.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10001818" width="1" height="1"&gt;</description></item><item><title>re: Every Tree There Is</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/04/22/every-tree-there-is.aspx#10001793</link><pubDate>Fri, 23 Apr 2010 22:44:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10001793</guid><dc:creator>Mark Knell</dc:creator><description>&lt;P&gt;&amp;gt; Let's say that an arbitrary tree is defined as either (1) the empty arbitrary tree, or (2) a list (possibly empty) of *non-empty* arbitrary trees called the children. &lt;/P&gt;
&lt;P&gt;If you're using the same notion of ordering as you did last post, you probably want a bit more detail on how the children are ordered. &amp;nbsp;Maybe something like: If two permutations of the children only transpose children that have similar subtrees, the permutations are considered equivalent. &lt;/P&gt;
&lt;P&gt;In other words, if "list" entails being well ordered, then that's too strict an ordering for your needs. &amp;nbsp;You need a collection that allows a root with children {A, B} to be equated with a root with children {B, A}.&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;No, that is exactly the opposite of what I need. If A is {} and B is {{}} then I want {{}{{}}} and {{{}}{}} to be different trees. An ordered&amp;nbsp;list gives me exactly the ordering I need.&lt;/P&gt;
&lt;P&gt;With these definitions it becomes plausible that binary trees and arbitrary trees are isomorphic, since both of them are &lt;EM&gt;recursively defined collections of pairs&lt;/EM&gt;. A binary tree is a collection of left/right pairs, and an arbitrary tree is a collection of head/tail pairs.&lt;/P&gt;&lt;/DIV&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10001793" width="1" height="1"&gt;</description></item><item><title>re: Every Tree There Is</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/04/22/every-tree-there-is.aspx#10001781</link><pubDate>Fri, 23 Apr 2010 21:51:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10001781</guid><dc:creator>John Kallen</dc:creator><description>&lt;P&gt;"Next time: what else can we generate all of?"&lt;/P&gt;
&lt;P&gt;Eric: how about generalizing from binary trees to arbitrary directed graphs (including cycles). That way you could generate what amounts to control flow graphs of basic blocks. You might think of it as useful for generating control flow graphs for the testing of the back end of a compiler (or decompiler, in my case). However, the number of graphs is likely to grow very fast with the number of nodes. And attempting to filter out the many isomorphic graphs doesn't work as graph isomorphism is hard (not P, but apparently not NP-complete either).&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;Interesting idea. You're right, it's the isomorphic graphs that are hard to identify. Simply generating all the directed graphs with n nodes is easy; there are 2^(n^2) of them. Imagine an n by n matrix of Booleans that says whether there is an edge from x to y; there is one graph for every possible combination of Booleans in that matrix. Easily enumerated: just make an n x n bit integer and count from zero until you're done. -- Eric &lt;/P&gt;&lt;/DIV&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10001781" width="1" height="1"&gt;</description></item><item><title>re: Every Tree There Is</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/04/22/every-tree-there-is.aspx#10001711</link><pubDate>Fri, 23 Apr 2010 19:39:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10001711</guid><dc:creator>Philip</dc:creator><description>&lt;p&gt;Just a note of pedantry;&lt;/p&gt;
&lt;p&gt;If there are two trees of size 2 (binary), &amp;nbsp;A in root with B left, A in root with B right...&lt;/p&gt;
&lt;p&gt;The you could also say that there are four for a structured tree of size 4: &amp;nbsp;A in root with B in child position 0 (nodes 1-3 empty), &amp;nbsp;... A in root with B in child position 3 (nodes 0-2 empty).&lt;/p&gt;
&lt;p&gt;When reading your question, it was not clear to me if &amp;quot;any arbitrary tree&amp;quot; meant &amp;quot;a tree with an arbitrary structure&amp;quot; or &amp;quot;any structured tree picked arbitrarily&amp;quot;. &amp;nbsp; So I answered &amp;quot;it depends&amp;quot; :)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10001711" width="1" height="1"&gt;</description></item><item><title>re: Every Tree There Is</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/04/22/every-tree-there-is.aspx#10001653</link><pubDate>Fri, 23 Apr 2010 17:56:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10001653</guid><dc:creator>Mark Knell</dc:creator><description>&lt;p&gt;&amp;gt; It's amazing how many people get hopelessly tangled up&lt;/p&gt;
&lt;p&gt;This is completely off Eric's topic, but it's interesting to me how common this view of technical difficulty is. &amp;nbsp;Namely, that a problem that's only mildly complex in general should still only be mildly complex to solve in an interview. &amp;nbsp;I used to think that, too.&lt;/p&gt;
&lt;p&gt;Over the years, though, I've had a couple of interviews where I drew blanks on (or got &amp;quot;hopelessly tangled up&amp;quot; in) things I not only should have known, but did know cold in my everyday routine. &amp;nbsp;And certainly I've been on the interviewing side of of plenty of trainwreck responses to even trivial problems, such as &amp;quot;in the language of your choice, write code to traverse a binary tree&amp;quot;. &amp;nbsp;The Coding Horror blog has recurring posts about even simpler sorts of problems causing difficult. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Two things changed my mind. &amp;nbsp;One, I've heard that the most widely-held fear in the US is public speaking--outranking spiders, needles, planes, internet outtages, etc. &amp;nbsp;For many, relatedly I think, the interview is NOT a representative sample of their technical skill. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;And two, consider the last time you made an important technical presentation (say, to a client or a boss). &amp;nbsp;Did you choose to do it without having prepared or rehearsed? &amp;nbsp;If you rehearsed, why? For many, we rehearse because thinking things through in advance, where you can be meticulous at your own speed, is different from trying to produce meticulous results rapidly, for an audience. &amp;nbsp;And that's when you know what the questions are likely to be in advance!&lt;/p&gt;
&lt;p&gt;I'm not saying this sort of question is an unfair test of a candidate, or that speaking and thinking on your feet is not an important skill, even for a &amp;quot;heads down&amp;quot; developer. &amp;nbsp;But I do think that most interviewers underestimate how technical degree-of-difficulty amplifies when introduced in an interview. &amp;nbsp;They think it says something about the population, and maybe it does--but I think a significance fraction of the result can be explained by sheer anxiety.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10001653" width="1" height="1"&gt;</description></item><item><title>re: Every Tree There Is</title><link>http://blogs.msdn.com/b/ericlippert/archive/2010/04/22/every-tree-there-is.aspx#10001591</link><pubDate>Fri, 23 Apr 2010 16:10:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10001591</guid><dc:creator>Tony Cox [MSFT]</dc:creator><description>&lt;p&gt;On the theme of &amp;quot;generating all of...&amp;quot;: printing all anagrams of a given input word is a question I sometimes pose to candidates in interviews. It's amazing how many people get hopelessly tangled up by it.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10001591" width="1" height="1"&gt;</description></item></channel></rss>