-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeVisualizer.cs
168 lines (151 loc) · 5.4 KB
/
TreeVisualizer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
*
* Create by Alimsah YILDIRIM on 3/5/2019
*
* @file TreeVisualizer.cs
* @url github.com/alimsahy/BinaryTreeVisualizer
*
*
*/
namespace RedBlackTree
{
using System;
using System.Collections.Generic;
public static class TreeVisualizer
{
/// <summary>
/// The default background.
/// </summary>
private static readonly ConsoleColor DEFAULT_BACKGROUND = Console.BackgroundColor;
/// <summary>
/// Print the specified root, topMargin and leftMargin.
/// </summary>
/// <param name="root">Root.</param>
/// <param name="topMargin">Top margin.</param>
/// <param name="leftMargin">Left margin.</param>
public static void Print(this TreeNode root, int topMargin = 2, int leftMargin = 2)
{
if (root == null) return;
int rootTop = Console.CursorTop + topMargin;
List<VisualTreeNode> last = new List<VisualTreeNode>();
TreeNode next = root;
for (int level = 0; next != null; level++)
{
VisualTreeNode item = new VisualTreeNode()
{
Node = next,
Text = next.Item.ToString(" 0 ")
};
if (level < last.Count)
{
item.StartPos = last[level].EndPos + 1;
last[level] = item;
}
else
{
item.StartPos = leftMargin;
last.Add(item);
}
if (level > 0)
{
item.Parent = last[level - 1];
if (next == item.Parent.Node.Left)
{
item.Parent.Left = item;
item.EndPos = Math.Max(item.EndPos, item.Parent.StartPos);
}
else
{
item.Parent.Right = item;
item.StartPos = Math.Max(item.StartPos, item.Parent.EndPos);
}
}
next = next.Left ?? next.Right;
for (; next == null; item = item.Parent)
{
Print(item, rootTop + 2 * level);
if (--level < 0) break;
if (item == item.Parent.Left)
{
item.Parent.StartPos = item.EndPos;
next = item.Parent.Node.Right;
}
else
{
if (item.Parent.Left == null)
{
item.Parent.EndPos = item.StartPos;
}
else
{
item.Parent.StartPos += (item.StartPos - item.Parent.EndPos) / 2;
}
}
}
Console.SetCursorPosition(0, rootTop + 2 * last.Count - 1);
}
}
/// <summary>
/// Print the specified item and top.
/// </summary>
/// <param name="item">Item.</param>
/// <param name="top">Top.</param>
private static void Print(VisualTreeNode item, int top)
{
SwapColors();
if (item.Node.Color == TreeNodeColor.Black) Console.BackgroundColor = ConsoleColor.DarkGray;
if (item.Node.Color == TreeNodeColor.Red) Console.BackgroundColor = ConsoleColor.Red;
Print(item.Text, top, item.StartPos);
SwapColors();
if (item.Left != null)
{
PrintLink(top + 1, "┌", "┘", item.Left.StartPos + item.Left.Size / 2, item.StartPos);
}
if (item.Right != null)
{
PrintLink(top + 1, "└", "┐", item.EndPos - 1, item.Right.StartPos + item.Right.Size / 2);
}
}
/// <summary>
/// Print the specified text, top, left and right.
/// </summary>
/// <param name="text">Text.</param>
/// <param name="top">Top.</param>
/// <param name="left">Left.</param>
/// <param name="right">Right.</param>
private static void Print(string text, int top, int left, int right = -1)
{
Console.SetCursorPosition(left, top);
if (right < 0)
{
right = left + text.Length;
}
while (Console.CursorLeft < right)
{
Console.Write(text);
}
}
/// <summary>
/// P rints the link.
/// </summary>
/// <param name="top">Top.</param>
/// <param name="start">Start.</param>
/// <param name="end">End.</param>
/// <param name="startPos">Start position.</param>
/// <param name="endPos">End position.</param>
private static void PrintLink(int top, string start, string end, int startPos, int endPos)
{
Print(start, top, startPos);
Print("-", top, startPos + 1, endPos);
Print(end, top, endPos);
}
/// <summary>
/// Swaps the colors.
/// </summary>
private static void SwapColors()
{
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = DEFAULT_BACKGROUND;
}
}
}