-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
201 lines (187 loc) · 7 KB
/
Program.cs
File metadata and controls
201 lines (187 loc) · 7 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BinaryTreeDisplay
{
class Program
{
static void Main(string[] args)
{
StreamReader input = new StreamReader("Trees1.txt"); // "Trees.txt", "Trees1.txt" and "Trees2.txt" exist
string trees = "";
while (!input.EndOfStream)
{
trees += input.ReadLine() + ",";
}
trees = trees.Substring(0, trees.Length - 1);
string[] TreeList = trees.Split(',');
BTNode[] RootList = new BTNode[TreeList.Length];
for (int x = 0; x < TreeList.Length; x++)
{
RootList[x] = BuildTree(TreeList[x]);
}
Console.WriteLine("Do you want to maximize the display before displaying the trees (matters in Win 10)? If so, do so now. Press any key to continue...");
Console.ReadKey(true);
for (int x = 0; x < TreeList.Length; x++)
{
Console.WriteLine("TREE {0}:", x + 1);
Console.WriteLine();
WriteHeading("My display method - Raw output:");
displayTree(RootList[x], false, false);
Console.WriteLine();
WriteHeading("My display method - Trimmed output:");
displayTree(RootList[x]);
Console.WriteLine();
WriteHeading("Existing display method:");
displayTreeStructure(RootList[x]);
Console.WriteLine();
WriteHeading("Breadth first order:");
DisplayBreadthFirst(RootList[x]);
Console.WriteLine();
Console.WriteLine("Press any key to continue to process next Tree.");
Console.WriteLine("=======================================================================================");
Console.ReadKey(true);
}
Console.WriteLine("=========================================");
Console.WriteLine("END OF FILE REACHED - press Enter to exit");
Console.WriteLine("=========================================");
Console.ReadLine();
}
// Call my display method
static void displayTree(BTNode startPoint, bool shrink = true, bool hide = true)
{
if (startPoint != null)
{
startPoint.displayTreeVisual(shrink, hide);
}
}
// CURRENT DEFAULT DISPLAY
static void displayTreeStructure(BTNode startPoint)
// with thanks to Minnaar Fullard (WRA202 Class of 2014)
// adapted from Will's answer at http://stackoverflow.com/questions/1649027/how-do-i-print-out-a-tree-structure
{
if (startPoint != null)
PrintPretty(startPoint, "", false);
}
static void PrintPretty(BTNode node, String indent, bool rightChild)
{
String output = Convert.ToString(((string)node.value().ToString()));
if (!(node.parent() == null)) // node is not root
{
if (node.parent().left() == node) // node is left child
output += " ~l";
else
output += " ~r";
}
Console.Write(indent);
if (rightChild)
{
Console.Write(" /--");
indent += " | ";
}
else
if (!(node.parent() == null))
{
Console.Write(" \\--");
indent += " ";
}
Console.WriteLine("{0}", output);
if (node.left() != null && node.right() != null)
{
PrintPretty(node.right(), indent, true);
PrintPretty(node.left(), indent, false);
}
else if (node.left() != null)
{
PrintPretty(node.left(), indent, true);
}
else if (node.right() != null)
{
PrintPretty(node.right(), indent, true);
}
}
static BTNode BuildTree(string notation)
{
if (notation.Length == 0 || notation == "()")
return null;
BTNode root = null;
BTNode cur = null;
bool addLeft = true;
for (int x = 0; x < notation.Length; x++)
{
char curChar = notation[x];
switch (curChar)
{
case '(':
if (cur == null)
{
cur = new BTNode();
root = cur;
}
else
{
if (notation[x + 1] != ')')
{
BTNode newNode = new BTNode(null, null, null, cur);
if (cur.left() == null && addLeft)
{
cur.setLeft(newNode);
}
else
{
cur.setRight(newNode);
addLeft = true;
}
cur = newNode;
}
else
{
addLeft = false;
x++;
}
}
break;
case ')':
cur = cur.parent();
break;
default:
if (cur.value() != null)
cur.setValue(cur.value().ToString() + notation[x].ToString());
else
cur.setValue(notation[x]);
break;
}
}
return root;
}
static void DisplayBreadthFirst(BTNode node)
{
Queue temp = new Queue();
if (node != null)
temp.Enqueue(node);
while (temp.Count > 0)
{
BTNode cur = (BTNode)temp.Dequeue();
Console.Write(cur.value() + " ");
if (cur.left() != null)
temp.Enqueue(cur.left());
if (cur.right() != null)
temp.Enqueue(cur.right());
}
Console.WriteLine();
}
static void WriteHeading(string text, bool allCaps = true, char underline = '-')
{
Console.WriteLine((allCaps) ? text.ToUpper() : text);
for (int x = 0; x < text.Length; x++)
{
Console.Write(underline);
}
Console.WriteLine();
}
}
}