<?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>Immutability in C# Part Nine: Academic? Plus my AVL tree implementation</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/01/21/immutability-in-c-part-nine-academic-plus-my-avl-tree-implementation.aspx</link><description>Good Monday morning all. I have received a lot of good comments on this series so far that I thought I would speak to a bit. 
 Academic? 
 First and foremost, a number of people have asked questions which could be summed up as "isn't this just an academic</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Immutability in C# Part Nine: Academic? Plus my AVL tree implementation</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/01/21/immutability-in-c-part-nine-academic-plus-my-avl-tree-implementation.aspx#10325874</link><pubDate>Sun, 01 Jul 2012 15:16:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10325874</guid><dc:creator>Luca Minudel</dc:creator><description>&lt;p&gt;can you tell more about how immutable objects encourage non-nullable programming more then mutable objects ?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10325874" width="1" height="1"&gt;</description></item><item><title>re: Immutability in C# Part Nine: Academic? Plus my AVL tree implementation</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/01/21/immutability-in-c-part-nine-academic-plus-my-avl-tree-implementation.aspx#10066614</link><pubDate>Thu, 23 Sep 2010 09:08:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10066614</guid><dc:creator>Stefan Monov</dc:creator><description>&lt;p&gt;I agree with Donnie Hale. Your immutable dictionary code looks good, but I don&amp;#39;t see myself using it, as I wouldn&amp;#39;t know how to make it fit into my main mutating-style code.&lt;/p&gt;
&lt;p&gt;For example, you mention a queue, two threads that push to it and one that pops from it. You say it&amp;#39;s difficult to write correctly (or even think about), and I agree. Then you introduce the alternative - an immutable queue. Ok, say thread1 wants to push stuff to the queue. So it calls queue.Add a couple of times and gets a larger queue back. Thread2 does the same. So far so good. Now what do those two threads do with their own, larger queues?&lt;/p&gt;
&lt;p&gt;1. Do they merge them, (by iterating and skipping identical sequences of members)?&lt;/p&gt;
&lt;p&gt;2. Do they just provide an externally queriable &amp;quot;StuffThatMustBePushed&amp;quot; member, and leave it to the reader thread to query that and add it to its own queue?&lt;/p&gt;
&lt;p&gt;What would help is some sample (pseudo)code that demonstrates the entire situation you&amp;#39;re envisioning with the three threads and the queue.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10066614" width="1" height="1"&gt;</description></item><item><title>re: Immutability in C# Part Nine: Academic? Plus my AVL tree implementation</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/01/21/immutability-in-c-part-nine-academic-plus-my-avl-tree-implementation.aspx#8664315</link><pubDate>Sat, 28 Jun 2008 21:41:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8664315</guid><dc:creator>Daniel</dc:creator><description>&lt;p&gt;Hi, Eric. What do you think abount immutable variable length list? Is it possible to implement it?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8664315" width="1" height="1"&gt;</description></item><item><title>re: Immutability in C# Part Nine: Academic? Plus my AVL tree implementation</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/01/21/immutability-in-c-part-nine-academic-plus-my-avl-tree-implementation.aspx#7936290</link><pubDate>Thu, 28 Feb 2008 19:37:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7936290</guid><dc:creator>Ted</dc:creator><description>&lt;p&gt;Consider approaching you problem as a set of static functions first and then immutable objects next. &amp;nbsp;Static functions force one to write code that a) does not depend on a context being setup b) do not affect things outside of their parameters (if written properly) and c) can easily be moved around in your code.&lt;/p&gt;
&lt;p&gt;The non-static pattern of&lt;/p&gt;
&lt;p&gt;int functionABC()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;X a = new object X&lt;/p&gt;
&lt;p&gt;a.property_D = 2&lt;/p&gt;
&lt;p&gt;a.property_E = 5&lt;/p&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;p&gt;a.property_G = 6&lt;/p&gt;
&lt;p&gt;int r = a.method_call()&lt;/p&gt;
&lt;p&gt;return (r)&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;becomes a single line of code, functionABC is not needed and can be removed.&lt;/p&gt;
&lt;p&gt;X.method_call(2, 5, 6)&lt;/p&gt;
&lt;p&gt;This is is a common code defect since many class objects are designed for reuse in multiple parts of the application but end up being mostly made up of methods that could easily be made static.&lt;/p&gt;
&lt;p&gt;Static methods avoid the spaghetti code of needing 3 or more calls to an object (construct, set properties, call method) &amp;nbsp;when the functionality desired is a simple function call without side effects.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7936290" width="1" height="1"&gt;</description></item><item><title>re: Immutability in C# Part Nine: Academic? Plus my AVL tree implementation</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/01/21/immutability-in-c-part-nine-academic-plus-my-avl-tree-implementation.aspx#7600170</link><pubDate>Mon, 11 Feb 2008 07:13:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7600170</guid><dc:creator>DRBlaise</dc:creator><description>&lt;p&gt;I created an Immutible AVL Tree implementation that stores the balance information in different classes so has no need to store or calculate height. &amp;nbsp;It did not lead to simpler code but I think it is more memory and speed effecient. &amp;nbsp;First I created an enum and an additional interface with Balance and IsSingleLeaf properties.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private enum AVLTreeBalance&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;LeftHeavy = -1,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Balanced = 0,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;RightHeavy = 1&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private interface IAVLTree : IBinarySearchTree&amp;lt;K, V&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AVLTreeBalance Balance { get; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;bool IsSingleLeaf { get; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new IAVLTree Add(K key, V value);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new IAVLTree Remove(K key);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new IAVLTree Left { get; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new IAVLTree Right { get; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;Then I created 7 classes to represent different shapes and balance:&lt;/p&gt;
&lt;p&gt;private sealed class EmptyAVLTree : IBinarySearchTree&amp;lt;K, V&amp;gt;&lt;/p&gt;
&lt;p&gt;private sealed class SingleLeafAVLTree : IAVLTree &lt;/p&gt;
&lt;p&gt;private sealed class LeftLeafAVLTree : IAVLTree&lt;/p&gt;
&lt;p&gt;private sealed class RightLeafAVLTree : IAVLTree&lt;/p&gt;
&lt;p&gt;private sealed class BalancedAVLTree : IAVLTree&lt;/p&gt;
&lt;p&gt;private sealed class LeftHeavyAVLTree : IAVLTree&lt;/p&gt;
&lt;p&gt;private sealed class RightHeavyAVLTree : IAVLTree&lt;/p&gt;
&lt;p&gt;The code was pretty long but I thought I would share the Add for a RightHeavyAVLTree:&lt;/p&gt;
&lt;p&gt;public IAVLTree Add(K key, V value)&lt;/p&gt;
&lt;p&gt;{ &amp;nbsp; // Add to RightHeavy Tree&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;int compare = key.CompareTo(Key);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;if (compare &amp;gt; 0)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{ &amp;nbsp; // greater than add to right&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IAVLTree newRight = Right.Add(key, value);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (newRight.Balance == Right.Balance || newRight.Balance == AVLTreeBalance.Balanced)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// shape stays the same if right tree keeps same shape&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// or if the right tree goes from from unbalanced to balanced (same height)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new RightHeavyAVLTree(Key, Value, Left, newRight);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else //tree has gained a level on right making it 2 levels greater on right&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return RotateLeft(this, Left, newRight);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;if (compare &amp;lt; 0)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{ &amp;nbsp; // less than add to left&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IAVLTree newLeft = Left.Add(key, value);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (newLeft.Balance == Left.Balance || newLeft.Balance == AVLTreeBalance.Balanced)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// shape stays the same if left tree keeps same shape&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// or if the left tree goes from from unbalanced to balanced (same height)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new RightHeavyAVLTree(Key, Value, newLeft, Right);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else //tree has gained a level on left making it balanced&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new BalancedAVLTree(Key, Value, newLeft, Right);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;throw new Exception(&amp;quot;key already exists&amp;quot;);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Here is code for RotateLeft and DoubleLeft:&lt;/p&gt;
&lt;p&gt;private static IAVLTree RotateLeft(IAVLTree top, IAVLTree left, IAVLTree right)&lt;/p&gt;
&lt;p&gt;{ &amp;nbsp; // assumes top has key and value, right's height is 2 greater than left's&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;if (right.Balance == AVLTreeBalance.RightHeavy)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Single Left Rotation and shape becomes balanced &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new BalancedAVLTree(right.Key, right.Value,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new BalancedAVLTree(top.Key, top.Value, left, right.Left),&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;right.Right);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;else if (right.Balance == AVLTreeBalance.Balanced)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Single Left Rotation and shape becomes Left balanced&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new LeftHeavyAVLTree(right.Key, right.Value,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new RightHeavyAVLTree(top.Key, top.Value, left, right.Left),&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;right.Right);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;else //(right.Balance == AVLTreeBalance.LeftHeavy)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Double Left Rotation and shape becomes balanced&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return DoubleLeft(top, left, right);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;private static IAVLTree DoubleLeft(IAVLTree top, IAVLTree left, IAVLTree right)&lt;/p&gt;
&lt;p&gt;{ &amp;nbsp; // assumes top has key and value, right's height is 2 greater than left's&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;// assumes right tree is left heavy&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;IAVLTree newTop = right.Left; // right.Left tree becomes new top and controls shape&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;if (newTop.Balance == AVLTreeBalance.RightHeavy)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Left is LeftHeavy, Right is Balanced&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (newTop.Right.IsSingleLeaf)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Left is LeftLeaf&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new BalancedAVLTree(newTop.Key, newTop.Value,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new LeftLeafAVLTree(top.Key, top.Value, left),&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new BalancedAVLTree(right.Key, right.Value, newTop.Right, right.Right));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Left is LeftHeavy&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new BalancedAVLTree(newTop.Key, newTop.Value,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new LeftHeavyAVLTree(top.Key, top.Value, left, newTop.Left),&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new BalancedAVLTree(right.Key, right.Value, newTop.Right, right.Right));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;else if (newTop.Balance == AVLTreeBalance.Balanced)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Both children Balanced&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new BalancedAVLTree(newTop.Key, newTop.Value,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new BalancedAVLTree(top.Key, top.Value, left, newTop.Left),&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new BalancedAVLTree(right.Key, right.Value, newTop.Right, right.Right));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;else // (newTop.Balance == AVLTreeBalance.LeftHeavy)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Left is Balanced, Right is RightHeavy&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (newTop.Left.IsSingleLeaf)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Right is RightLeaf&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new BalancedAVLTree(newTop.Key, newTop.Value,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new BalancedAVLTree(top.Key, top.Value, left, newTop.Left),&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new RightLeafAVLTree(right.Key, right.Value, right.Right));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Right is RightHeavy&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new BalancedAVLTree(newTop.Key, newTop.Value,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new BalancedAVLTree(top.Key, top.Value, left, newTop.Left),&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new RightHeavyAVLTree(right.Key, right.Value, newTop.Right, right.Right));&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7600170" width="1" height="1"&gt;</description></item><item><title>re: Immutability in C# Part Nine: Academic? Plus my AVL tree implementation</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/01/21/immutability-in-c-part-nine-academic-plus-my-avl-tree-implementation.aspx#7577854</link><pubDate>Sun, 10 Feb 2008 09:40:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7577854</guid><dc:creator>Codrus</dc:creator><description>&lt;p&gt;I think there are a couple of errors in the implementation -- or at least it doesn't match the wikipedia article you linked to.&lt;/p&gt;
&lt;p&gt;If I use the following code, it should hit the Right Left Case (in your code called a DoubleLeft).&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IBinarySearchTree&amp;lt;int, string&amp;gt; tree = AVLTree&amp;lt;int, string&amp;gt;.Empty;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;tree = tree.Add(5, &amp;quot;five&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;tree = tree.Add(7, &amp;quot;seven&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;tree = tree.Add(6, &amp;quot;six&amp;quot;);&lt;/p&gt;
&lt;p&gt;What I actually get is a single left, same as if I had run it as:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IBinarySearchTree&amp;lt;int, string&amp;gt; tree = AVLTree&amp;lt;int, string&amp;gt;.Empty;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;tree = tree.Add(5, &amp;quot;five&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;tree = tree.Add(6, &amp;quot;six&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;tree = tree.Add(7, &amp;quot;seven&amp;quot;);&lt;/p&gt;
&lt;p&gt;The main reason for this is that the inner test shouldn't be looking for -2 or +2; -1 or +1 is sufficient.&lt;/p&gt;
&lt;p&gt;Changing the tests in MakeBalanced to match the Wikipedia algorithm makes it go down the correct rotation paths, but there's a second bug in RotateRight that causes an exception -- it should be grabbing tree.left.right, not tree.right.left.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7577854" width="1" height="1"&gt;</description></item><item><title>re: Immutability in C# Part Nine: Academic? Plus my AVL tree implementation</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/01/21/immutability-in-c-part-nine-academic-plus-my-avl-tree-implementation.aspx#7477860</link><pubDate>Wed, 06 Feb 2008 02:08:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7477860</guid><dc:creator>Donnie Hale</dc:creator><description>&lt;p&gt;Eric, you said: &amp;quot;When I compare the practical, solving-real-problems code I write using immutable or (&amp;quot;mostly&amp;quot; immutable objects) to that which uses highly mutable objects, I find that the solutions which use immutable objects are easier to reason about.&amp;quot;&lt;/p&gt;
&lt;p&gt;I don't that that's the case, but so far you're only showing immutable constructs that you can use in those practical situations. I personally would find it helpful if you applied these constructs to real practical situations, perhaps compared to using mutable constructs, with explanation as to why it's easier to reason about, simpler to code, etc.&lt;/p&gt;
&lt;p&gt;Thanks, and we appreciate the series.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7477860" width="1" height="1"&gt;</description></item><item><title>re: Immutability in C# Part Nine: Academic? Plus my AVL tree implementation</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/01/21/immutability-in-c-part-nine-academic-plus-my-avl-tree-implementation.aspx#7395864</link><pubDate>Sat, 02 Feb 2008 20:28:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7395864</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;That makes perfect sense! Write it up and let us see the code. I'll happily post it here, or a link.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7395864" width="1" height="1"&gt;</description></item><item><title>re: Immutability in C# Part Nine: Academic? Plus my AVL tree implementation</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/01/21/immutability-in-c-part-nine-academic-plus-my-avl-tree-implementation.aspx#7394171</link><pubDate>Sat, 02 Feb 2008 18:56:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7394171</guid><dc:creator>DRBlaise</dc:creator><description>&lt;p&gt;I have been fascinated by this discussion on Immutible data structures and have been doing some of my own research and investigation. &amp;nbsp;It seems that this example of an Immutible AVL tree is missing a couple important optimizations that Immutible data structures lend themselves to.&lt;/p&gt;
&lt;p&gt;First, memory can be reduced by creating more classes that represent different shapes of a tree. &amp;nbsp;There is no reason to store empty trees in the left and right variables. &amp;nbsp;SingleLeaf, LeftLeaf and RightLeaf classes can be created similar to the Empty class that do not allocate left and right variables.&lt;/p&gt;
&lt;p&gt;Second, even more memory reduction can be created if even more classes are created that represent the different states of Tree Balance. &amp;nbsp;3 classes can be created that represent non-leaf trees that are Balanced, LeftHeavy, and RightHeavy. &amp;nbsp;This should total get rid of the need to store Height because the class the tree is built from holds information about the balance of the tree similar to the way the IsEmpty property holds information about the shape of the tree. &amp;nbsp;New properties such as IsLeaf, IsBalanced, IsLeftHeavy, and IsRightHeavy can be created for each class. &amp;nbsp;This can also lead to simpler and faster code because each class of tree only needs to deal with its special shape.&lt;/p&gt;
&lt;p&gt;Does this make sense or am I all wet?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7394171" width="1" height="1"&gt;</description></item><item><title>Community Convergence XL</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/01/21/immutability-in-c-part-nine-academic-plus-my-avl-tree-implementation.aspx#7314592</link><pubDate>Wed, 30 Jan 2008 02:24:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7314592</guid><dc:creator>Noticias externas</dc:creator><description>&lt;p&gt;Welcome to the fortieth issue of Community Convergence. This week we have two new releases of note: We&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7314592" width="1" height="1"&gt;</description></item></channel></rss>