@@ -30,11 +30,11 @@ public static string DrawTree<TKey, TValue>(this IBinarySearchTree<TKey, TValue>
30
30
/// <summary>
31
31
/// /// Recusively draws the tree starting from node.
32
32
/// To construct a full tree representation concatenate the returned list of strings by '\n'.
33
- ///
33
+ ///
34
34
/// Example:
35
35
/// int position, width;
36
36
/// var fullTree = String.Join("\n", _recursivelyDrawTree(this.Root, out position, out width));
37
- ///
37
+ ///
38
38
/// Algorithm developed by MIT OCW.
39
39
/// http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/readings/binary-search-trees/bstsize_r.py
40
40
/// </summary>
@@ -43,43 +43,35 @@ public static string DrawTree<TKey, TValue>(this IBinarySearchTree<TKey, TValue>
43
43
/// <param name="widthOutput"></param>
44
44
/// <returns>List of tree levels as strings.</returns>
45
45
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 )
47
47
where T : IComparable < T >
48
48
{
49
49
widthOutput = 0 ;
50
50
positionOutput = 0 ;
51
- List < string > listOfLines = new List < string > ( ) ;
52
51
53
52
if ( node == null )
54
53
{
55
- return listOfLines ;
54
+ return new List < string > ( ) ;
56
55
}
57
56
58
57
//
59
58
// 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 ;
68
60
69
61
//
70
62
// Start drawing
71
- nodeLabel = Convert . ToString ( node . Value ) ;
63
+ var nodeLabel = Convert . ToString ( node . Value ) ;
72
64
73
65
// Visit the left child
74
- leftLines = _recursivelyDrawTree ( node . LeftChild , out leftPosition , out leftWidth ) ;
66
+ List < string > leftLines = _recursivelyDrawTree ( node . LeftChild , out leftPosition , out leftWidth ) ;
75
67
76
68
// Visit the right child
77
- rightLines = _recursivelyDrawTree ( node . RightChild , out rightPosition , out rightWidth ) ;
69
+ List < string > rightLines = _recursivelyDrawTree ( node . RightChild , out rightPosition , out rightWidth ) ;
78
70
79
71
// 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 ;
83
75
84
76
while ( leftLines . Count < rightLines . Count )
85
77
leftLines . Add ( new String ( ' ' , leftWidth ) ) ;
@@ -105,23 +97,22 @@ private static List<string> _recursivelyDrawTree<T>
105
97
106
98
//
107
99
// 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 > ( )
109
104
{
110
105
// 0
111
106
( new String ( ' ' , leftPosition ) ) + nodeLabel + ( new String ( ' ' , ( rightWidth - rightPosition ) ) ) ,
112
107
113
108
// 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 ) ) )
115
110
} ;
116
111
117
112
//
118
113
// 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 ) ) ;
125
116
126
117
//
127
118
// Return
@@ -210,7 +201,7 @@ private static List<string> _recursivelyDrawTree<TKey, TValue>
210
201
( new String ( ' ' , leftPosition ) ) + nodeLabel + ( new String ( ' ' , ( rightWidth - rightPosition ) ) ) ,
211
202
212
203
// 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 ) ) )
214
205
} ;
215
206
216
207
//
0 commit comments