Skip to content

Commit 1074761

Browse files
committed
Merge branch 'tautcony-TreeDrawer_improve'
2 parents f868bf7 + 73ef726 commit 1074761

File tree

3 files changed

+49
-70
lines changed

3 files changed

+49
-70
lines changed

DataStructures/Common/Helpers.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ public static string PadCenter(this string text, int newWidth, char fillerCharac
4949
//add a space to the left if the string is an odd number
5050
int padRight = charactersToPad / 2;
5151

52-
StringBuilder resultBuilder = new StringBuilder(newWidth);
53-
for (int i = 0; i < padLeft; i++) resultBuilder.Insert(i, fillerCharacter);
54-
for (int i = 0; i < length; i++) resultBuilder.Insert(i + padLeft, text[i]);
55-
for (int i = newWidth - padRight; i < newWidth; i++) resultBuilder.Insert(i, fillerCharacter);
56-
return resultBuilder.ToString();
52+
return new String(fillerCharacter, padLeft) + text + new String(fillerCharacter, padRight);
5753
}
5854

5955
/// <summary>

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
//

MainProgram/DataStructuresTests/AVLTreeTest.cs

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public static void DoTest()
2222
** 4 5
2323
** \ / \
2424
** 5 ===> 4 7
25-
** \
26-
** 7
25+
** \
26+
** 7
2727
**
2828
***************************************
2929
*/
@@ -65,7 +65,7 @@ public static void DoTest()
6565
// DOUBLE *right* rotation for node 5.
6666
//
6767
// The double rotation is achieved by:
68-
// 1> Simple *left* rotation for node 2, and then
68+
// 1> Simple *left* rotation for node 2, and then
6969
// 2> Simple *right* rotation for node 5
7070
//
7171
/*************************************
@@ -92,7 +92,7 @@ public static void DoTest()
9292
// DOUBLE *right* rotation for node 5.
9393
//
9494
// The double rotation is achieved by:
95-
// 1> Simple *right* rotation for node 7, and then
95+
// 1> Simple *right* rotation for node 7, and then
9696
// 2> Simple *left* rotation for node 5
9797
//
9898
/**************************************************************************
@@ -119,7 +119,7 @@ public static void DoTest()
119119
//
120120
/**************************************************************************
121121
** UNBALANCED ===> TRANSITION (1st R) ===> BALANCED (2nd Rt)
122-
** null .6..
122+
** null .6..
123123
** / \ / \
124124
** 2 6 ===> ===> 2 7
125125
** / \ / \ / \ /
@@ -132,7 +132,7 @@ public static void DoTest()
132132
// ASSERT CASE 5
133133
AssertCase_5(avlTree);
134134

135-
135+
136136
//
137137
// CLEAR THE TREE AND START OVER
138138
// Compare two binary trees with each other (height-wise) using bulk-inserts
@@ -152,56 +152,48 @@ public static void DoTest()
152152

153153
//
154154
// Draw the tree to the console.
155-
Console.WriteLine(String.Format("************\r\n** BST TREE:\r\n************\r\n"));
155+
Console.WriteLine("************\r\n** BST TREE:\r\n************\r\n");
156156
Console.WriteLine(bsTree.DrawTree());
157157

158158
Console.WriteLine("\r\n\r\n");
159159

160-
Console.WriteLine(String.Format("************\r\n** AVL TREE:\r\n************\r\n"));
160+
Console.WriteLine("************\r\n** AVL TREE:\r\n************\r\n");
161161
Console.WriteLine(avlTree.DrawTree());
162162

163163
//
164164
// OUTPUT OF AVL TREE DRAWER
165-
/*****
166-
** ************
167-
** ** AVL TREE:
168-
** ************
169-
** ....15...
170-
** / \
171-
** ...9.. ..20.
172-
** / \ / \
173-
** ..5. 11 16 28
174-
** / \ / \ / \ / \
175-
** 1 7 9 12 19 25 30
176-
** /\ / \ / \ / \ /\ /\ / \
177-
** -1 7 8 10 13 39
178-
** /\ /\ /\ /\ /\ /\
179-
*
165+
/**
166+
** .....15....
167+
** / \
168+
** ...9... .20
169+
** / \ / \
170+
** .5 11 16 28
171+
** / \ / \ \ / \
172+
** 1 7 9 12 19 25 30
173+
** / / \ \ \ \
174+
** -1 7 8 10 13 39
180175
*/
181176

182177

183178
treeDataList = new List<int>() { 15, 25, 5, 12, 1, 9, 7, -1, 11, 30, 8, 10, 13, 28, 39 };
184179
avlTree.Clear();
185180
avlTree.Insert(treeDataList);
186181

187-
Console.WriteLine(String.Format("************\r\n** AVL TREE:\r\n************\r\n"));
182+
Console.WriteLine("************\r\n** AVL TREE:\r\n************\r\n");
188183
Console.WriteLine(avlTree.DrawTree());
189184

190185
//
191186
// OUTPUT OF AVL TREE DRAWER
192-
/*****
193-
**
194-
** .......9......
195-
** / \
196-
** .5 ....12...
197-
** / \ / \
198-
** 1 7 11 .25.
199-
** /\ / \ /\ / \
200-
** -1 8 10 15 30
201-
** /\ /\ /\ /\ / \
202-
** 13 28 39
203-
** /\ /\ /\
204-
**
187+
/**
188+
** ....9...
189+
** / \
190+
** 5 .12.
191+
** / \ / \
192+
** 1 7 11 25
193+
** / \ / / \
194+
** -1 8 10 15 30
195+
** / / \
196+
** 13 28 39
205197
*/
206198

207199
Console.ReadLine();

0 commit comments

Comments
 (0)