This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.cs
296 lines (278 loc) · 9.7 KB
/
BinaryTree.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace SolucionAlumno
{
class BinaryTree<T> : IOrderSerchStruct<T> where T : IComparable {
private BinaryTreeNode<T> raiz;
//private Hashtable hashTable = new Hashtable();
private Comparison<IComparable> comparer = CompareElements;
private int size;
public BinaryTree()
{
raiz = null;
size = 0;
}
public int Size
{
get { return size; }
}
public BinaryTreeNode<T> Raiz
{
get { return raiz; }
set { raiz = value; }
}
public static int CompareElements(IComparable valor1, IComparable valor2) {
return valor1.CompareTo(valor2);
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
public void Add(T value) {
BinaryTreeNode<T> node = new BinaryTreeNode<T>(value);
this.Add(node);
//hashTable.Add(value.GetHashCode(), node);
}
/// <summary>
/// Si el arbol no tiene nodos, coloca el nodo como raiz, sino, si el nodo es de valor
/// menor a la raiz, lo coloca a la izquierda, sino a la derecha.
/// </summary>
/// <param name="node"></param>
public void Add(BinaryTreeNode<T> node) {
if (this.raiz == null) {
this.raiz = node;
size++;
} else {
if (node.Parent == null) {
node.Parent = raiz;
}
bool insertLeft = comparer((IComparable)node.Value, (IComparable)node.Parent.Value) <= 0;
if (insertLeft) {
if (node.Parent.LeftChild == null) {
node.Parent.LeftChild = node;
size++;
} else {
node.Parent = node.Parent.LeftChild;
this.Add(node);
}
} else {
if (node.Parent.RightChild == null) {
node.Parent.RightChild = node;
size++;
} else {
node.Parent = node.Parent.RightChild;
this.Add(node);
}
}
}
}
//TODO juli - Vieja este metodo tambien va a sacar el nodo?
//Te acordas que deciamos que lo obtiene y lo saca del arbol.
public T getMinValue() {
if (this.raiz != null) {
BinaryTreeNode<T> node = this.raiz;
while (node.HasLeftChild) {
node = node.LeftChild;
}
return node.Value;
}
//return null;
return default(T); // no tengo idea que hace esto.
//SI NO TENES IDEA COMO LLEGO ACA???? JAJAJA COPY PASTE????
}
/**
* Obtiene el minimo y ademas lo remueve de la lista.
*/
public T getMinimoAndRemove()
{
if (this.raiz != null) {
BinaryTreeNode<T> node = this.raiz;
while (node.HasLeftChild) {
node = node.LeftChild;
}
this.Remove(node);
return node.Value;
}
return default(T); // no tengo idea que hace esto.
//SI NO TENES IDEA COMO LLEGO ACA???? JAJAJA COPY PASTE????
}
public T FindInStruct(T value)
{
return Find(value).Value;
}
/// <summary>
/// Busca un nodo.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public BinaryTreeNode<T> Find(T value) {
/*
BinaryTreeNode<T> node = this.raiz;
while (node != null)
{
String values = node.Value + " - " + value;
if (node.Value.Equals(value))
{
return node;
}
else
{
bool searchLeft = comparer((IComparable)value, (IComparable)node.Value) < 0;
if (searchLeft)
{
node = node.LeftChild;
}
else
{
node = node.RightChild;
}
}
}
return null;
*/
//return hashTable[value.GetHashCode()] as BinaryTreeNode<T>;
return this.Find(value, this.raiz);
}
/// <summary>
/// Busqueda secuencial sobre todo el arbol!!!!
/// //TODO PERFORMAR
/// </summary>
/// <param name="value"></param>
/// <param name="node"></param>
/// <returns></returns>
public BinaryTreeNode<T> Find(T value, BinaryTreeNode<T> node)
{
if (node != null)
{
if (node.Value.Equals(value))
{
return node;
}
BinaryTreeNode<T> nodeLeftChilds = this.Find(value, node.LeftChild);
if (nodeLeftChilds != null)
{
return nodeLeftChilds;
}
BinaryTreeNode<T> nodeRigthChilds = this.Find(value, node.RightChild);
if (nodeRigthChilds != null)
{
return nodeRigthChilds;
}
}
return null;
}
public bool Contains(T value) {
//return hashTable.ContainsKey(value.GetHashCode());
return (this.Find(value) != null);
}
public bool Remove(T value) {
BinaryTreeNode<T> node = Find(value);
return (this.Remove(node) != null);
}
/// <summary>
/// Remueve un nodo.
/// </summary>
/// <param name="removeNode">Nodo a remover.</param>
/// <returns>referencia al nodo que cambio.</returns>
public BinaryTreeNode<T> Remove(BinaryTreeNode<T> removeNode)
{
if (removeNode == null) {
return null;
}
bool esRaiz = (removeNode == raiz);
if (this.Size == 1)
{
this.raiz = null;
//hashTable.Remove(removeNode.Value.GetHashCode());
size--;
}
//Remueve para el caso que no tenga hijos
else if (removeNode.IsHoja)
{
if (removeNode.IsLeftChild)
{
removeNode.Parent.LeftChild = null;
}
else {
removeNode.Parent.RightChild = null;
}
removeNode.Parent = null;
//hashTable.Remove(removeNode.Value.GetHashCode());
size--;
}
//Remueve para el caso que tenga un hijo
else if (removeNode.CantHijos == 1)
{
if (removeNode.HasLeftChild)
{
removeNode.LeftChild.Parent = removeNode.Parent;
if (esRaiz)
{
this.raiz = removeNode.LeftChild;
}
else
{
if (removeNode.IsLeftChild)
{
removeNode.Parent.LeftChild = removeNode.LeftChild;
}
else
{
removeNode.Parent.RightChild = removeNode.LeftChild;
}
}
}
else
{
removeNode.RightChild.Parent = removeNode.Parent;
if (esRaiz)
{
this.raiz = removeNode.RightChild;
}
else
{
if (removeNode.IsLeftChild)
{
removeNode.Parent.LeftChild = removeNode.RightChild;
}
else
{
removeNode.Parent.RightChild = removeNode.RightChild;
}
}
}
removeNode.Parent = null;
removeNode.LeftChild = null;
removeNode.RightChild = null;
//hashTable.Remove(removeNode.Value.GetHashCode());
size--;
}
//Remueve para el caso que tenga 2 hijos
else
{
BinaryTreeNode<T> successorNode = removeNode.RightChild;
while (successorNode.LeftChild != null)
{
successorNode = successorNode.LeftChild;
}
T auxValue = successorNode.Value;
this.Remove(successorNode);
removeNode.Value = auxValue;
return removeNode;
//successorNode.replaceNodeReferences(removeNode);
}
return new BinaryTreeNode<T>();
}
public void Clear() {
if (this.raiz != null)
{
this.raiz.LeftChild = null;
this.raiz.RightChild = null;
}
this.raiz = null;
this.size = 0;
}
}
}