Index: branches/HeuristicLab.ReingoldTilfordTreeLayout/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.cs
===================================================================
--- branches/HeuristicLab.ReingoldTilfordTreeLayout/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.cs	(revision 10485)
+++ branches/HeuristicLab.ReingoldTilfordTreeLayout/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.cs	(revision 10487)
@@ -266,45 +266,55 @@
 
     #region methods for painting the symbolic expression tree
-
-    private void DrawFunctionTree(ISymbolicExpressionTree tree, Graphics graphics, int preferredWidth, int preferredHeight, int minHDistance, int minVDistance) {
-      var layoutNodes = layoutAdapter.Convert(tree).ToList();
+    private void DrawFunctionTree(ISymbolicExpressionTree symbolicExpressionTree, Graphics graphics, int preferredWidth, int preferredHeight, int minHDistance, int minVDistance) {
+      var layoutNodes = layoutAdapter.Convert(symbolicExpressionTree).ToList();
       layoutEngine.Reset();
       layoutEngine.Root = layoutNodes[0];
       layoutEngine.AddNodes(layoutNodes);
-      layoutEngine.MinHorizontalSpacing = (preferredNodeWidth + minHDistance);
-      layoutEngine.MinVerticalSpacing = (preferredNodeHeight + minVDistance);
+      layoutEngine.MinHorizontalSpacing = (preferredWidth + minHDistance);
+      layoutEngine.MinVerticalSpacing = (preferredHeight + minVDistance);
       layoutEngine.CalculateLayout();
       var bounds = layoutEngine.Bounds();
-      double sx = this.Width / (bounds.Width + preferredWidth);
-      if (sx > 1) sx = 1;
-      double sy = this.Height / (bounds.Height + preferredHeight);
-      if (sy > 1) sy = 1;
-      double dx = (this.Width - bounds.Width + preferredWidth) / 2;
-      if (dx < 0) dx = 0;
-      double dy = (this.Height - bounds.Height + preferredHeight) / 2;
-      if (dy < 0) dy = 0;
-
-      var levels = layoutNodes.GroupBy(n => n.Level, n => n);
-
-      foreach (var level in levels) {
-        var nodes = level.ToList();
-        double min = 0;
-        for (int i = 0; i < nodes.Count - 1; ++i) {
-          var w = (nodes[i + 1].X - nodes[i].X) * sx - preferredWidth;
-          if (w < min) min = w;
-        }
-        if (min > 0) min = 0;
-
-        foreach (var layoutNode in level) {
+
+      double verticalScalingFactor = 1.0;
+      double layoutHeight = (bounds.Height + preferredHeight);
+      if (this.Height < layoutHeight)
+        verticalScalingFactor = this.Height / layoutHeight;
+
+      double horizontalScalingFactor = 1.0;
+      double layoutWidth = (bounds.Width + preferredWidth);
+      if (this.Width < layoutWidth)
+        horizontalScalingFactor = this.Width / layoutWidth;
+
+      double horizontalOffset;
+      if (this.Width > layoutWidth)
+        horizontalOffset = (this.Width - layoutWidth) / 2.0;
+      else
+        horizontalOffset = preferredWidth / 2.0;
+
+      var levels = layoutNodes.GroupBy(n => n.Level, n => n).ToList();
+      for (int i = levels.Count - 1; i >= 0; --i) {
+        var nodes = levels[i].ToList();
+
+        double minSpacing = double.MaxValue;
+        if (nodes.Count > 1) {
+          for (int j = 1; j < nodes.Count() - 1; ++j) {
+            var distance = nodes[j].X - nodes[j - 1].X; // guaranteed to be > 0
+            if (minSpacing > distance) minSpacing = distance;
+          }
+        }
+        minSpacing *= horizontalScalingFactor;
+
+        int minWidth = (int)Math.Round(preferredWidth * horizontalScalingFactor);
+        int width = (int)Math.Min(minSpacing, preferredWidth) - 2; // leave some pixels so that node margins don't overlap
+
+        foreach (var layoutNode in nodes) {
           var visualNode = visualTreeNodes[layoutNode.Content];
-          visualNode.Width = (int)Math.Round(preferredWidth + min) - 2; // -2 to allow enough padding (1px on each side) for the node contour to be drawn
-          visualNode.Height = (int)Math.Round(preferredHeight * sy);
-          visualNode.X = (int)(Math.Round(layoutNode.X * sx + dx));
-          visualNode.Y = (int)(Math.Round(layoutNode.Y * sy + dy));
+          visualNode.Width = width;
+          visualNode.Height = (int)Math.Round(preferredHeight * verticalScalingFactor);
+          visualNode.X = (int)Math.Round((layoutNode.X + horizontalOffset) * horizontalScalingFactor + (minWidth - width) / 2.0);
+          visualNode.Y = (int)Math.Round(layoutNode.Y * verticalScalingFactor);
           DrawTreeNode(graphics, visualNode);
         }
       }
-
-      graphics.ResetClip(); // reset clip region
       // draw node connections
       foreach (var visualNode in visualTreeNodes.Values) {
@@ -315,4 +325,5 @@
           var origin = new Point(visualNode.X + visualNode.Width / 2, visualNode.Y + visualNode.Height);
           var target = new Point(visualSubtree.X + visualSubtree.Width / 2, visualSubtree.Y);
+          graphics.Clip = new Region(new Rectangle(Math.Min(origin.X, target.X), origin.Y, Math.Max(origin.X, target.X), target.Y));
           using (var linePen = new Pen(visualLine.LineColor)) {
             linePen.DashStyle = visualLine.DashStyle;
@@ -366,6 +377,5 @@
       Image image = new Bitmap(Width, Height);
       using (Graphics g = Graphics.FromImage(image)) {
-        int height = this.Height / tree.Depth;
-        DrawFunctionTree(tree, g, 0, 0, Width, height);
+        DrawFunctionTree(tree, g, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance);
       }
       image.Save(filename);
@@ -377,6 +387,5 @@
         using (Metafile file = new Metafile(filename, g.GetHdc())) {
           using (Graphics emfFile = Graphics.FromImage(file)) {
-            int height = this.Height / tree.Depth;
-            DrawFunctionTree(tree, emfFile, 0, 0, Width, height);
+            DrawFunctionTree(tree, emfFile, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance);
           }
         }
Index: branches/HeuristicLab.ReingoldTilfordTreeLayout/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/LayoutEngines/SymbolicExpressionTreeLayoutAdapter.cs
===================================================================
--- branches/HeuristicLab.ReingoldTilfordTreeLayout/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/LayoutEngines/SymbolicExpressionTreeLayoutAdapter.cs	(revision 10485)
+++ branches/HeuristicLab.ReingoldTilfordTreeLayout/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/LayoutEngines/SymbolicExpressionTreeLayoutAdapter.cs	(revision 10487)
@@ -50,8 +50,5 @@
       int i = 0;
       while (i < list.Count) {
-        if (list[i].Children != null)
-          list.AddRange(list[i].Children);
-        //          foreach (LayoutNode<ISymbolicExpressionTreeNode> child in list[i].Children)
-        //            list.Add(child);
+        if (list[i].Children != null) list.AddRange(list[i].Children);
         ++i;
       }
