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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
namespace InformationSystemKadri.Classes
{
/// <summary>
/// Generic class, which allow to work with information with help of binary tree.
/// </summary>
/// <typeparam name="T">Type of data, what conatains node of tree.</typeparam>
public class BinaryTree<T> where T : IComparable
{
/// <summary>
/// Root of tree.
/// </summary>
private BinaryTreeNode<T> _center;
/// <summary>
/// Property for access to root of tree.
/// </summary>
public BinaryTreeNode<T> Center
{
get { return _center; }
}
/// <summary>
/// Method for adding new node to the tree.
/// </summary>
/// <param name="data">New data.</param>
/// <returns>New node.</returns>
public BinaryTreeNode<T> Add(T data)
{
if (_center == null)
{
_center = new BinaryTreeNode<T>(data);
_center.Depth = 1;
_center.Offset = 0;
return _center;
}
else
{
BinaryTreeNode<T> current = _center;
BinaryTreeNode<T> next;
int nodeDepth = _center.Depth;
int nodeOffset = _center.Offset;
while (true)
{
nodeDepth++;
if (data.CompareTo(current.Data) <= 0)
{
nodeOffset--;
next = current.Left;
if (next == null)
{
current.Left = new BinaryTreeNode<T>(data);
current.Left.Parent = current;
if (nodeDepth == current.Depth)
{
nodeDepth++;
}
current.Left.Depth = nodeDepth;
current.Left.Offset = nodeOffset;
return current.Left;
}
}
else
{
nodeOffset++;
next = current.Right;
if (next == null)
{
current.Right = new BinaryTreeNode<T>(data);
current.Right.Parent = current;
if (nodeDepth == current.Depth)
{
nodeDepth++;
}
current.Right.Depth = nodeDepth;
current.Right.Offset = nodeOffset;
return current.Right;
}
}
current = next;
}
}
}
/// <summary>
/// Remove node from the tree.
/// </summary>
/// <param name="node"></param>
public void Remove(BinaryTreeNode<T> node)
{
// If node is null.
if (node == null) return;
// If children are null.
if (node.Right == null && node.Left == null)
{
if (node == node.Parent.Left) node.Parent.Left = null;
else node.Parent.Right = null;
}
else
{
if (node == node.Parent.Left)
{
node.Parent.Left = node.Left;
if (node.Left == null)
{
node.Parent.Left = node.Right;
SetDepth(node.Parent, node.Parent.Depth, node.Parent.Offset);
}
else
{
node.Parent.Left.Depth = node.Parent.Depth + 1;
BinaryTreeNode<T> rightBranch = MostRightNode(node.Parent);
rightBranch.Right = node.Right;
SetDepth(rightBranch.Right, rightBranch.Depth + 1, rightBranch.Offset - 1);
}
}
else
{
node.Parent.Right = node.Right;
if (node.Right == null)
{
node.Parent.Right = node.Left;
SetDepth(node.Parent, node.Parent.Depth, node.Parent.Offset);
}
else
{
node.Parent.Right.Depth = node.Parent.Depth + 1;
BinaryTreeNode<T> leftBranch = MostLeftNode(node.Parent);
leftBranch.Left = node.Left;
SetDepth(leftBranch.Left, leftBranch.Depth + 1, leftBranch.Offset + 1);
}
}
}
}
/// <summary>
/// Finds the most right node of the given node childs.
/// </summary>
/// <param name="node">Parent node.</param>
/// <returns>Most right node.</returns>
private BinaryTreeNode<T> MostRightNode(BinaryTreeNode<T> node)
{
if (node.Right != null)
return MostRightNode(node.Right);
else return node;
}
/// <summary>
/// Finds the most left node of the given node childs.
/// </summary>
/// <param name="node">Parent node.</param>
/// <returns>Most left node.</returns>
private BinaryTreeNode<T> MostLeftNode(BinaryTreeNode<T> node)
{
if (node.Left != null)
return MostLeftNode(node.Left);
else return node;
}
/// <summary>
/// Bypassing tree(recursion method).
/// </summary>
/// <param name="node">Parent node.</param>
/// <param name="data">Data, what needs to find.</param>
/// <returns>Finded node, or null.</returns>
public BinaryTreeNode<T> TreeBypass(BinaryTreeNode<T> node, T data)
{
// Compare two objects.
if (Treatment(node.Data, data) == true) return node;
// If left node is not null.
if (node.Left != null && data.CompareTo(node.Data) <= 0)
return TreeBypass(node.Left, data);
// If right node is not null.
if (node.Right != null && data.CompareTo(node.Data) > 0)
return TreeBypass(node.Right, data);
return null;
}
/// <summary>
/// Bypasing tree(recursion method), and format list of nodes.
/// </summary>
/// <param name="node">Parent node.</param>
/// <param name="nodes">List of nodes.</param>
public void TreeBypass(BinaryTreeNode<T> node, List<BinaryTreeNode<T>> nodes)
{
nodes.Add(node);
if (node.Left != null)
TreeBypass(node.Left, nodes);
if (node.Right != null)
TreeBypass(node.Right, nodes);
}
/// <summary>
/// Compare two data objects.
/// </summary>
/// <param name="data1">First data object.</param>
/// <param name="data2">Second data object.</param>
/// <returns>Returns true if data1 equals data2, otherwise - false.</returns>
private bool Treatment(T data1, T data2)
{
if (data1.Equals(data2))
return true;
else return false;
}
/// <summary>
/// Change data of node.
/// </summary>
/// <param name="node">Node, which data needs to change.</param>
/// <param name="data">New data.</param>
/// <param name="radical">If changes key - true, otherwise - false.</param>
public void ChangeData(BinaryTreeNode<T> node, T data, bool radical = true)
{
if (node != _center && radical == true)
{
Remove(node);
Add(data);
}
else
{
node.Data = data;
}
}
/// <summary>
/// Set depth, for graphical output of tree.
/// </summary>
/// <param name="node">Parent node.</param>
/// <param name="depth">Depth of node.</param>
/// <param name="offset">Offset of node.</param>
private void SetDepth(BinaryTreeNode<T> node, int depth, int offset)
{
if (node != null)
{
node.Depth = depth;
node.Offset = offset;
if (node.Left != null)
{
SetDepth(node.Left, depth + 1, offset - 1);
}
if (node.Right != null)
{
SetDepth(node.Right, depth + 1, offset + 1);
}
}
}
}
} |