-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeTypesExcecise.cs
More file actions
200 lines (160 loc) Β· 5.69 KB
/
TreeTypesExcecise.cs
File metadata and controls
200 lines (160 loc) Β· 5.69 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
using System;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Unit4.BinTreeUtilsLib;
using Unit4.CollectionsLib;
using Unit4.BinTreeCanvasLib;
namespace ConsoleApp32
{
internal class Program
{
public static void Main(string[] args)
{
}
}
class BinaryTreeUtils
{
public static BinNode<int[]> CreateRandomBinaryTree(int depth)
{
Random rnd = new Random();
if (depth == 0) return null;
int size = rnd.Next(10, 31);
int[] values = new int[size];
for (int i = 0; i < size; i++)
values[i] = rnd.Next(100, 5001);
BinNode<int[]> node = new BinNode<int[]>(values);
node.SetLeft(CreateRandomBinaryTree(depth - 1));
node.SetRight(CreateRandomBinaryTree(depth - 1));
return node;
}
public static int FindLevelWithMaxSum(BinNode<int[]> root)
{
if (root == null) return -1;
Queue<BinNode<int[]>> queue = new Queue<BinNode<int[]>>();
queue.Insert(root);
int level = 0;
int maxSumLevel = 0;
int maxSum = int.MinValue;
while (!queue.IsEmpty())
{
int levelSum = 0;
Queue<BinNode<int[]>> tempQueue = new Queue<BinNode<int[]>>();
while (!queue.IsEmpty())
{
BinNode<int[]> currentNode = queue.Remove();
levelSum += currentNode.GetValue().Sum();
if (currentNode.HasLeft()) tempQueue.Insert(currentNode.GetLeft());
if (currentNode.HasRight()) tempQueue.Insert(currentNode.GetRight());
}
while (!tempQueue.IsEmpty())
{
queue.Insert(tempQueue.Remove());
}
if (levelSum > maxSum)
{
maxSum = levelSum;
maxSumLevel = level;
}
level++;
}
return maxSumLevel;
}
public static Queue<double> AvgEachLevel(BinNode<int[]> root)
{
if (root == null) return null;
Queue<BinNode<int[]>> queue = new Queue<BinNode<int[]>>();
queue.Insert(root);
Queue<double> AvgVal = new Queue<double>();
while (!queue.IsEmpty())
{
int levelSum = 0;
double countlevelnodes = 0;
Queue<BinNode<int[]>> tempQueue = new Queue<BinNode<int[]>>();
while (!queue.IsEmpty())
{
BinNode<int[]> currentNode = queue.Remove();
levelSum += currentNode.GetValue().Sum();
if (currentNode.HasLeft()) tempQueue.Insert(currentNode.GetLeft());
if (currentNode.HasRight()) tempQueue.Insert(currentNode.GetRight());
countlevelnodes++;
}
while (!tempQueue.IsEmpty())
{
queue.Insert(tempQueue.Remove());
}
AvgVal.Insert(levelSum/countlevelnodes);
}
return AvgVal;
}
public static bool SameStartEnd(BinNode<string> root)
{
if (root == null) return false;
if (root.GetValue()[0] == root.GetValue()[root.GetValue().Length -1]) return true;
return SameStartEnd(root.GetLeft()) || SameStartEnd(root.GetRight());
}
public static string FindLongestNode(BinNode<string> root)
{
if (root == null) return "";
string longest = root.GetValue();
string leftLongest = FindLongestNode(root.GetLeft());
string rightLongest = FindLongestNode(root.GetRight());
if (leftLongest.Length > longest.Length)
longest = leftLongest;
if (rightLongest.Length > longest.Length)
longest = rightLongest;
return longest;
}
public static Queue<char> FindQueueWithSameStartEnd(BinNode<Queue<char>> root)
{
if (root == null) return null;
if (IsSameStartEnd(root.GetValue()))
{
return root.GetValue();
}
if (root.HasLeft())
{
Queue<char> leftResult = FindQueueWithSameStartEnd(root.GetLeft());
return leftResult;
}
if (root.HasRight())
{
Queue<char> rightResult = FindQueueWithSameStartEnd(root.GetRight());
return rightResult;
}
return null;
}
public static bool IsSameStartEnd(Queue<char> queue)
{
if (queue.IsEmpty()) return false;
Queue<char> copy = copyqueue(queue);
char first = copy.Remove();
if (copy.IsEmpty()) return true;
char last = '\0';
while (!copy.IsEmpty())
{
last = copy.Remove();
}
return first == last;
}
public static Queue<T> copyqueue<T>(Queue<T> queue)
{
Queue<T> temp = new Queue<T>();
Queue<T> temp2 = new Queue<T>();
while (!queue.IsEmpty())
{
temp.Insert(queue.Head());
temp2.Insert(queue.Head());
queue.Remove();
}
while (!temp2.IsEmpty())
{
queue.Insert(temp2.Head());
temp2.Remove();
}
return temp;
}
}
}