Skip to content

Commit

Permalink
Merge branch 'tautcony-TreeDrawer_improve'
Browse files Browse the repository at this point in the history
  • Loading branch information
aalhour committed May 3, 2016
2 parents f868bf7 + 73ef726 commit 1074761
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 70 deletions.
6 changes: 1 addition & 5 deletions DataStructures/Common/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ public static string PadCenter(this string text, int newWidth, char fillerCharac
//add a space to the left if the string is an odd number
int padRight = charactersToPad / 2;

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

/// <summary>
Expand Down
47 changes: 19 additions & 28 deletions DataStructures/Trees/TreeDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public static string DrawTree<TKey, TValue>(this IBinarySearchTree<TKey, TValue>
/// <summary>
/// /// Recusively draws the tree starting from node.
/// To construct a full tree representation concatenate the returned list of strings by '\n'.
///
///
/// Example:
/// int position, width;
/// var fullTree = String.Join("\n", _recursivelyDrawTree(this.Root, out position, out width));
///
///
/// Algorithm developed by MIT OCW.
/// http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/readings/binary-search-trees/bstsize_r.py
/// </summary>
Expand All @@ -43,43 +43,35 @@ public static string DrawTree<TKey, TValue>(this IBinarySearchTree<TKey, TValue>
/// <param name="widthOutput"></param>
/// <returns>List of tree levels as strings.</returns>
private static List<string> _recursivelyDrawTree<T>
(BSTNode<T> node, out int positionOutput, out int widthOutput)
(BSTNode<T> node, out int positionOutput, out int widthOutput)
where T : IComparable<T>
{
widthOutput = 0;
positionOutput = 0;
List<string> listOfLines = new List<string>();

if (node == null)
{
return listOfLines;
return new List<string>();
}

//
// Variables
string nodeLabel;

List<string> leftLines, rightLines;
leftLines = rightLines = new List<string>();

int leftPosition = 0, rightPosition = 0;
int leftWidth = 0, rightWidth = 0;
int middle, position_out, width_out;
int leftPosition, rightPosition, leftWidth, rightWidth;

//
// Start drawing
nodeLabel = Convert.ToString(node.Value);
var nodeLabel = Convert.ToString(node.Value);

// Visit the left child
leftLines = _recursivelyDrawTree(node.LeftChild, out leftPosition, out leftWidth);
List<string> leftLines = _recursivelyDrawTree(node.LeftChild, out leftPosition, out leftWidth);

// Visit the right child
rightLines = _recursivelyDrawTree(node.RightChild, out rightPosition, out rightWidth);
List<string> rightLines = _recursivelyDrawTree(node.RightChild, out rightPosition, out rightWidth);

// Calculate pads
middle = Math.Max(Math.Max(2, nodeLabel.Length), (rightPosition + leftWidth - leftPosition + 1));
position_out = leftPosition + middle;
width_out = leftPosition + middle + rightWidth - rightPosition;
int middle = Math.Max(Math.Max(2, nodeLabel.Length), (rightPosition + leftWidth - leftPosition + 1));
int position_out = leftPosition + middle / 2;
int width_out = leftPosition + middle + rightWidth - rightPosition;

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

//
// Construct the list of lines.
listOfLines = new List<string>()
string leftBranch = node.HasLeftChild ? "/" : " ";
string rightBranch = node.HasRightChild ? "\\" : " ";

List<string> listOfLines = new List<string>()
{
// 0
(new String(' ', leftPosition )) + nodeLabel + (new String(' ', (rightWidth - rightPosition))),

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

//
// Add the right lines and left lines to the final list of lines.
listOfLines =
listOfLines.Concat(
leftLines.Zip(
rightLines, (left_line, right_line) =>
left_line + (new String(' ', (width_out - leftWidth - rightWidth))) + right_line)
).ToList();
listOfLines.AddRange(leftLines.Zip(rightLines, (leftLine, rightLine) =>
leftLine + (new String(' ', (width_out - leftWidth - rightWidth))) + rightLine));

//
// Return
Expand Down Expand Up @@ -210,7 +201,7 @@ private static List<string> _recursivelyDrawTree<TKey, TValue>
(new String(' ', leftPosition )) + nodeLabel + (new String(' ', (rightWidth - rightPosition))),

// 1
(new String(' ', leftPosition)) + "/" + (new String(' ', (middle - padValue))) + "\\" + (new String(' ', (rightWidth - rightPosition)))
(new String(' ', leftPosition)) + "/" + (new String(' ', (middle - padValue))) + "\\" + (new String(' ', (rightWidth - rightPosition)))
};

//
Expand Down
66 changes: 29 additions & 37 deletions MainProgram/DataStructuresTests/AVLTreeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static void DoTest()
** 4 5
** \ / \
** 5 ===> 4 7
** \
** 7
** \
** 7
**
***************************************
*/
Expand Down Expand Up @@ -65,7 +65,7 @@ public static void DoTest()
// DOUBLE *right* rotation for node 5.
//
// The double rotation is achieved by:
// 1> Simple *left* rotation for node 2, and then
// 1> Simple *left* rotation for node 2, and then
// 2> Simple *right* rotation for node 5
//
/*************************************
Expand All @@ -92,7 +92,7 @@ public static void DoTest()
// DOUBLE *right* rotation for node 5.
//
// The double rotation is achieved by:
// 1> Simple *right* rotation for node 7, and then
// 1> Simple *right* rotation for node 7, and then
// 2> Simple *left* rotation for node 5
//
/**************************************************************************
Expand All @@ -119,7 +119,7 @@ public static void DoTest()
//
/**************************************************************************
** UNBALANCED ===> TRANSITION (1st R) ===> BALANCED (2nd Rt)
** null .6..
** null .6..
** / \ / \
** 2 6 ===> ===> 2 7
** / \ / \ / \ /
Expand All @@ -132,7 +132,7 @@ public static void DoTest()
// ASSERT CASE 5
AssertCase_5(avlTree);


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

//
// Draw the tree to the console.
Console.WriteLine(String.Format("************\r\n** BST TREE:\r\n************\r\n"));
Console.WriteLine("************\r\n** BST TREE:\r\n************\r\n");
Console.WriteLine(bsTree.DrawTree());

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

Console.WriteLine(String.Format("************\r\n** AVL TREE:\r\n************\r\n"));
Console.WriteLine("************\r\n** AVL TREE:\r\n************\r\n");
Console.WriteLine(avlTree.DrawTree());

//
// OUTPUT OF AVL TREE DRAWER
/*****
** ************
** ** AVL TREE:
** ************
** ....15...
** / \
** ...9.. ..20.
** / \ / \
** ..5. 11 16 28
** / \ / \ / \ / \
** 1 7 9 12 19 25 30
** /\ / \ / \ / \ /\ /\ / \
** -1 7 8 10 13 39
** /\ /\ /\ /\ /\ /\
*
/**
** .....15....
** / \
** ...9... .20
** / \ / \
** .5 11 16 28
** / \ / \ \ / \
** 1 7 9 12 19 25 30
** / / \ \ \ \
** -1 7 8 10 13 39
*/


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

Console.WriteLine(String.Format("************\r\n** AVL TREE:\r\n************\r\n"));
Console.WriteLine("************\r\n** AVL TREE:\r\n************\r\n");
Console.WriteLine(avlTree.DrawTree());

//
// OUTPUT OF AVL TREE DRAWER
/*****
**
** .......9......
** / \
** .5 ....12...
** / \ / \
** 1 7 11 .25.
** /\ / \ /\ / \
** -1 8 10 15 30
** /\ /\ /\ /\ / \
** 13 28 39
** /\ /\ /\
**
/**
** ....9...
** / \
** 5 .12.
** / \ / \
** 1 7 11 25
** / \ / / \
** -1 8 10 15 30
** / / \
** 13 28 39
*/

Console.ReadLine();
Expand Down

0 comments on commit 1074761

Please sign in to comment.