Skip to content

Commit 39385b6

Browse files
committed
Fix positon calculation in TreeDrawer, remove null node's branch display
1 parent f868bf7 commit 39385b6

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

DataStructures/Trees/TreeDrawer.cs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public static string DrawTree<TKey, TValue>(this IBinarySearchTree<TKey, TValue>
3030
/// <summary>
3131
/// /// Recusively draws the tree starting from node.
3232
/// To construct a full tree representation concatenate the returned list of strings by '\n'.
33-
///
33+
///
3434
/// Example:
3535
/// int position, width;
3636
/// var fullTree = String.Join("\n", _recursivelyDrawTree(this.Root, out position, out width));
37-
///
37+
///
3838
/// Algorithm developed by MIT OCW.
3939
/// http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/readings/binary-search-trees/bstsize_r.py
4040
/// </summary>
@@ -43,43 +43,35 @@ public static string DrawTree<TKey, TValue>(this IBinarySearchTree<TKey, TValue>
4343
/// <param name="widthOutput"></param>
4444
/// <returns>List of tree levels as strings.</returns>
4545
private static List<string> _recursivelyDrawTree<T>
46-
(BSTNode<T> node, out int positionOutput, out int widthOutput)
46+
(BSTNode<T> node, out int positionOutput, out int widthOutput)
4747
where T : IComparable<T>
4848
{
4949
widthOutput = 0;
5050
positionOutput = 0;
51-
List<string> listOfLines = new List<string>();
5251

5352
if (node == null)
5453
{
55-
return listOfLines;
54+
return new List<string>();
5655
}
5756

5857
//
5958
// Variables
60-
string nodeLabel;
61-
62-
List<string> leftLines, rightLines;
63-
leftLines = rightLines = new List<string>();
64-
65-
int leftPosition = 0, rightPosition = 0;
66-
int leftWidth = 0, rightWidth = 0;
67-
int middle, position_out, width_out;
59+
int leftPosition, rightPosition, leftWidth, rightWidth;
6860

6961
//
7062
// Start drawing
71-
nodeLabel = Convert.ToString(node.Value);
63+
var nodeLabel = Convert.ToString(node.Value);
7264

7365
// Visit the left child
74-
leftLines = _recursivelyDrawTree(node.LeftChild, out leftPosition, out leftWidth);
66+
List<string> leftLines = _recursivelyDrawTree(node.LeftChild, out leftPosition, out leftWidth);
7567

7668
// Visit the right child
77-
rightLines = _recursivelyDrawTree(node.RightChild, out rightPosition, out rightWidth);
69+
List<string> rightLines = _recursivelyDrawTree(node.RightChild, out rightPosition, out rightWidth);
7870

7971
// Calculate pads
80-
middle = Math.Max(Math.Max(2, nodeLabel.Length), (rightPosition + leftWidth - leftPosition + 1));
81-
position_out = leftPosition + middle;
82-
width_out = leftPosition + middle + rightWidth - rightPosition;
72+
int middle = Math.Max(Math.Max(2, nodeLabel.Length), (rightPosition + leftWidth - leftPosition + 1));
73+
int position_out = leftPosition + middle / 2;
74+
int width_out = leftPosition + middle + rightWidth - rightPosition;
8375

8476
while (leftLines.Count < rightLines.Count)
8577
leftLines.Add(new String(' ', leftWidth));
@@ -105,23 +97,22 @@ private static List<string> _recursivelyDrawTree<T>
10597

10698
//
10799
// Construct the list of lines.
108-
listOfLines = new List<string>()
100+
string leftBranch = node.HasLeftChild ? "/" : " ";
101+
string rightBranch = node.HasRightChild ? "\\" : " ";
102+
103+
List<string> listOfLines = new List<string>()
109104
{
110105
// 0
111106
(new String(' ', leftPosition )) + nodeLabel + (new String(' ', (rightWidth - rightPosition))),
112107

113108
// 1
114-
(new String(' ', leftPosition)) + "/" + (new String(' ', (middle - 2))) + "\\" + (new String(' ', (rightWidth - rightPosition)))
109+
(new String(' ', leftPosition)) + leftBranch + (new String(' ', (middle - 2))) + rightBranch + (new String(' ', (rightWidth - rightPosition)))
115110
};
116111

117112
//
118113
// Add the right lines and left lines to the final list of lines.
119-
listOfLines =
120-
listOfLines.Concat(
121-
leftLines.Zip(
122-
rightLines, (left_line, right_line) =>
123-
left_line + (new String(' ', (width_out - leftWidth - rightWidth))) + right_line)
124-
).ToList();
114+
listOfLines.AddRange(leftLines.Zip(rightLines, (leftLine, rightLine) =>
115+
leftLine + (new String(' ', (width_out - leftWidth - rightWidth))) + rightLine));
125116

126117
//
127118
// Return
@@ -210,7 +201,7 @@ private static List<string> _recursivelyDrawTree<TKey, TValue>
210201
(new String(' ', leftPosition )) + nodeLabel + (new String(' ', (rightWidth - rightPosition))),
211202

212203
// 1
213-
(new String(' ', leftPosition)) + "/" + (new String(' ', (middle - padValue))) + "\\" + (new String(' ', (rightWidth - rightPosition)))
204+
(new String(' ', leftPosition)) + "/" + (new String(' ', (middle - padValue))) + "\\" + (new String(' ', (rightWidth - rightPosition)))
214205
};
215206

216207
//

0 commit comments

Comments
 (0)