Immutability in C# Part Six: A Simple Binary Treehttp://blogs.msdn.com/b/ericlippert/archive/2007/12/18/immutability-in-c-part-six-a-simple-binary-tree.aspxOK, we've gotten pretty good at this by now. A straightforward implementation of an immutable generic binary tree requires little comment on the basics. A binary tree is either empty, or a value, left tree and right tree:
public interface IBinaryTree&lt;V&gt;en-USTelligent Evolution Platform Developer Build (Build: 5.6.50428.7875)re: Immutability in C# Part Six: A Simple Binary Treehttp://blogs.msdn.com/b/ericlippert/archive/2007/12/18/immutability-in-c-part-six-a-simple-binary-tree.aspx#10055228Fri, 27 Aug 2010 22:23:40 GMT91d46819-8472-40ad-a661-2c78acb4018c:10055228Lars<p>@Eric, so you&#39;re saying that even though &quot;child&quot; is a single data structure in memory, multiple references to it in the tree data structure actually just mean &quot;two identical subtrees&quot;? Hmm, I guess if they&#39;re immutable, there&#39;s no way to tell the difference between that, and the two subtrees being the same subtree... Weird! So you really have to divorce the tree data structure from the abstract tree.</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=10055228" width="1" height="1">re: Immutability in C# Part Six: A Simple Binary Treehttp://blogs.msdn.com/b/ericlippert/archive/2007/12/18/immutability-in-c-part-six-a-simple-binary-tree.aspx#8269765Sun, 16 Mar 2008 20:47:59 GMT91d46819-8472-40ad-a661-2c78acb4018c:8269765MaybeIBeggin<p>Excuse my ignorance, but: Why it is immutable ? Because there is no insert ? I didnt catch.</p>
<p>Please.</p>
<p>MIB</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=8269765" width="1" height="1">re: Immutability in C# Part Six: A Simple Binary Treehttp://blogs.msdn.com/b/ericlippert/archive/2007/12/18/immutability-in-c-part-six-a-simple-binary-tree.aspx#7224974Thu, 24 Jan 2008 21:07:32 GMT91d46819-8472-40ad-a661-2c78acb4018c:7224974Eric Lippert<p>Um... why are either of those not binary trees? &nbsp;They certainly look like binary trees to me. They obey all of the rules of binaries trees. Just because we've been clever enough to compress two identical subtrees to have the same representation doesn't make this &quot;not a tree shape&quot;.</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=7224974" width="1" height="1">re: Immutability in C# Part Six: A Simple Binary Treehttp://blogs.msdn.com/b/ericlippert/archive/2007/12/18/immutability-in-c-part-six-a-simple-binary-tree.aspx#7224622Thu, 24 Jan 2008 20:38:45 GMT91d46819-8472-40ad-a661-2c78acb4018c:7224622David<p>It may not be possible to create a cycle in this implementation (notwithstanding Michael's posts; I haven't had time to go through them), but it is certainly possible to create an instance with a non-tree shape:</p>
<p>BinaryTree&lt;int&gt; child = new BinaryTree&lt;int&gt;(0, BinaryTree&lt;int&gt;.Empty, BinaryTree&lt;int&gt;.Empty);</p>
<p>BinaryTree&lt;int&gt; parent = new BinaryTree&lt;int&gt;(1, child, child);</p>
<p>or, slightly less trivial,</p>
<p>BinaryTree&lt;int&gt; child = new BinaryTree&lt;int&gt;(0, BinaryTree&lt;int&gt;.Empty, BinaryTree&lt;int&gt;.Empty);</p>
<p>BinaryTree&lt;int&gt; left = new BinaryTree&lt;int&gt;(1, BinaryTree&lt;int&gt;.Empty, child);</p>
<p>BinaryTree&lt;int&gt; right = new BinaryTree&lt;int&gt;(1, child, BinaryTree&lt;int&gt;.Empty);</p>
<p>BinaryTree&lt;int&gt; root = new BinaryTree&lt;int&gt;(2, left, right);</p>
<p>These examples also point out how cumbersome it is to refer to static fields in generic classes, and why you really should consider using null as a proxy for Empty during construction; the above code would be much easier to read. Also, a constructor for leaf nodes which only takes a value would reduce the burden somewhat.</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=7224622" width="1" height="1">Immutability in C#http://blogs.msdn.com/b/ericlippert/archive/2007/12/18/immutability-in-c-part-six-a-simple-binary-tree.aspx#7153118Sat, 19 Jan 2008 00:05:08 GMT91d46819-8472-40ad-a661-2c78acb4018c:7153118<a href="http://weblogs.asp.net/bleroy">Tales from the Evil Empire</a><p>For some reason, there's been a lot of buzz lately around immutability in C#. If you're interested in</p>
<img src="http://blogs.msdn.com/aggbug.aspx?PostID=7153118" width="1" height="1">re: Immutability in C# Part Six: A Simple Binary Treehttp://blogs.msdn.com/b/ericlippert/archive/2007/12/18/immutability-in-c-part-six-a-simple-binary-tree.aspx#6810188Thu, 20 Dec 2007 01:02:39 GMT91d46819-8472-40ad-a661-2c78acb4018c:6810188Michael Chermside<p>Okay, I started to reply to this thread, and it wound up too big for a comment so I wrote my own blog post instead. To summarize, I wanted to say that cycles CAN exist even using just this implementation, but only in the presence of unusual threading behavior. I don't know C# well enough to explain it there, so I write an explanation in Java (maybe someone here who understands C# better can comment on how well it applies).</p>
<p>I wrote &lt;a href=&quot;<a rel="nofollow" target="_new" href="http://mcherm.com/blog/permalinks/1/immutable-trees-and-threading-evil-part-1&quot;&gt;one">http://mcherm.com/blog/permalinks/1/immutable-trees-and-threading-evil-part-1&quot;&gt;one</a> blog entry&lt;/a&gt; just describing the same thing built in Java (and simplified somewhat) and &lt;a href=&quot;<a rel="nofollow" target="_new" href="http://mcherm.com/blog/permalinks/1/immutable-trees-and-threading-evil-part-2&quot;&gt;another">http://mcherm.com/blog/permalinks/1/immutable-trees-and-threading-evil-part-2&quot;&gt;another</a> blog entry&lt;/a&gt; describing how you can get a cycle. I also explained the special trick you can use to avoid the problem in Java -- there's a good chance that it applies to the Eric's C# code as well, but I'm not expert enough to be sure.</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=6810188" width="1" height="1">re: Immutability in C# Part Six: A Simple Binary Treehttp://blogs.msdn.com/b/ericlippert/archive/2007/12/18/immutability-in-c-part-six-a-simple-binary-tree.aspx#6808343Wed, 19 Dec 2007 20:54:49 GMT91d46819-8472-40ad-a661-2c78acb4018c:6808343Eric Lippert<p>Good point. &nbsp;I meant &quot;only using this implementation&quot;.</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=6808343" width="1" height="1">re: Immutability in C# Part Six: A Simple Binary Treehttp://blogs.msdn.com/b/ericlippert/archive/2007/12/18/immutability-in-c-part-six-a-simple-binary-tree.aspx#6807758Wed, 19 Dec 2007 19:33:32 GMT91d46819-8472-40ad-a661-2c78acb4018c:6807758Grant Husbands<p>This immutable tree implementation can trivially have cycles, because it accepts arbitrary IBinaryTree&lt;V&gt; instances and that's a public interface; I can provide my own (immutable) implementation of it that has cycles. One can't have cycles containing only BinaryTree instances, though. &nbsp;It's possible in functional languages, too, like Haskell: &quot;let ones = BinaryTree ones 1 ones&quot;</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=6807758" width="1" height="1">re: Immutability in C# Part Six: A Simple Binary Treehttp://blogs.msdn.com/b/ericlippert/archive/2007/12/18/immutability-in-c-part-six-a-simple-binary-tree.aspx#6802182Wed, 19 Dec 2007 07:03:44 GMT91d46819-8472-40ad-a661-2c78acb4018c:6802182Dean Harding<p>meisinger: Because you have to pass the children in as arguments to the constructor, it's physically impossible to pass a parent in as a child because the parent has not been constructed yet... if you get my meaning.</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=6802182" width="1" height="1">re: Immutability in C# Part Six: A Simple Binary Treehttp://blogs.msdn.com/b/ericlippert/archive/2007/12/18/immutability-in-c-part-six-a-simple-binary-tree.aspx#6802002Wed, 19 Dec 2007 06:27:41 GMT91d46819-8472-40ad-a661-2c78acb4018c:6802002meisinger<p>Ok.. first off... great series! Can't tell you how much I enjoy it...</p>
<p>I am, however, having a really hard time understanding why an immutable tree cannot have any cycles. I can see how it happens to a mutable tree but why or how is this prevented in an immutable one?</p>
<p>Man... I am lost... and I thought I was doing so well too...</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=6802002" width="1" height="1">re: Immutability in C# Part Six: A Simple Binary Treehttp://blogs.msdn.com/b/ericlippert/archive/2007/12/18/immutability-in-c-part-six-a-simple-binary-tree.aspx#6799515Tue, 18 Dec 2007 23:59:28 GMT91d46819-8472-40ad-a661-2c78acb4018c:6799515Eric Lippert<p>We'll be looking at ways to construct immutable trees _not_ from the leaves first in later posts.</p>
<p>Your intuition about InOrder is correct, as is your proposed sketch of a solution.</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=6799515" width="1" height="1">re: Immutability in C# Part Six: A Simple Binary Treehttp://blogs.msdn.com/b/ericlippert/archive/2007/12/18/immutability-in-c-part-six-a-simple-binary-tree.aspx#6799419Tue, 18 Dec 2007 23:46:42 GMT91d46819-8472-40ad-a661-2c78acb4018c:6799419WaterBreath<p>Thanks for looking at immutable trees, Eric.</p>
<p>My very first concern looking at the immutable tree class as you've implemented it is that it seems like it _must_ be built starting at the leaves and working back up to the root. &nbsp;It seems to me like this would be very inconvenient in some cases, and is exactly the reason I asked about immutable trees in the comments to your post on the immutable stack.</p>
<p>My feeling looking at InOrder is that the problem is if your tree has height h and nodes n, you're generating an iterator object for every node, and potentially will at some points have h &quot;nested&quot; iterators in memory at once. &nbsp;While the way you have it written here is very simple, code-wise, a better solution might be to not use recursion and instead use a stack or something to keep track of your traversal path into the tree.</p>
<div style="clear:both;"></div><img src="http://blogs.msdn.com/aggbug.aspx?PostID=6799419" width="1" height="1">Geek Lectures - Things geeks should know about &raquo; Blog Archive &raquo; Immutability in C# Part Six: A Simple Binary Treehttp://blogs.msdn.com/b/ericlippert/archive/2007/12/18/immutability-in-c-part-six-a-simple-binary-tree.aspx#6799274Tue, 18 Dec 2007 23:32:54 GMT91d46819-8472-40ad-a661-2c78acb4018c:6799274Geek Lectures - Things geeks should know about » Blog Archive » Immutability in C# Part Six: A Simple Binary Tree<p>PingBack from <a rel="nofollow" target="_new" href="http://geeklectures.info/2007/12/18/immutability-in-c-part-six-a-simple-binary-tree/">http://geeklectures.info/2007/12/18/immutability-in-c-part-six-a-simple-binary-tree/</a></p>
<img src="http://blogs.msdn.com/aggbug.aspx?PostID=6799274" width="1" height="1">