-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.cs
186 lines (155 loc) · 5.34 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using Microsoft.Win32.SafeHandles;
namespace GeneticProgrammingOptimizer
{
public class BinaryTree<T>
{
public int index;
public T value;
public BinaryTree<T> left;
public BinaryTree<T> right;
public BinaryTree(T[] values) : this(values, 0) { }
public BinaryTree()
{
}
public static int ReindexTree(ref BinaryTree<string> tree)
{
var stack = new Stack<BinaryTree<string>>();
stack.Push(tree);
var index = 0;
while (stack.Count > 0)
{
var elem = stack.Pop();
if (elem != null)
{
elem.index = index++;
}
if (elem?.left != null)
stack.Push(elem.left);
if(elem?.right != null)
stack.Push(elem.right);
}
return index-1;
}
public static void SwapTreeWithTarget(ref BinaryTree<string> tree, BinaryTree<string> target, int index)
{
var st = GetSubtreeByIndex(ref tree, index);
st.left = target.left;
st.right = target.right;
st.value = target.value;
}
public static BinaryTree<string>[] SwapSubtrees(
int index1,
int index2,
BinaryTree<string> bt1,
BinaryTree<string> bt2)
{
var st1 = GetSubtreeByIndex(ref bt1, index1);
var st2 = GetSubtreeByIndex(ref bt2, index2);
var tmpleft = st1.left;
var tmpright = st1.right;
var tmpval = st1.value;
st1.left = st2.left;
st1.right = st2.right;
st1.value = st2.value;
st2.left = tmpleft;
st2.right = tmpright;
st2.value = tmpval;
return new[] {bt1, bt2};
}
public static void SwapSubtrees(
int index1,
int index2,
ref BinaryTree<string> bt1,
ref BinaryTree<string> bt2)
{
var st1 = GetSubtreeByIndex( ref bt1, index1);
var st2 = GetSubtreeByIndex( ref bt2, index2);
var tmpleft = st1.left;
var tmpright = st1.right;
var tmpval = st1.value;
st1.left = st2.left;
st1.right = st2.right;
st1.value = st2.value;
st2.left = tmpleft;
st2.right = tmpright;
st2.value = tmpval;
}
public static ref BinaryTree<string> GetSubtreeByIndex(ref BinaryTree<string> tree, int index)
{
if (index == 0)
return ref tree;
Stack<BinaryTree<string>> stack = new Stack<BinaryTree<string>>();
stack.Push(tree);
while (stack.Count > 0)
{
var curr = stack.Pop();
if (curr?.left?.index == index)
{
return ref curr.left;
}
if (curr?.right?.index == index)
{
return ref curr.right;
}
if(curr?.left != null)
stack.Push(curr.left);
if(curr?.right != null)
stack.Push(curr.right);
}
throw new ApplicationException("Index not found");
}
public static int MaxDepth( BinaryTree<T> root)
{
if (root == null) return 0;
return Math.Max(MaxDepth(root.left), MaxDepth(root.right)) + 1;
}
BinaryTree(T[] values, int index)
{
Load(this, values, index);
}
void Load(BinaryTree<T> tree, T[] values, int index)
{
this.value = values[index];
if (index * 2 + 1 < values.Length)
{
this.left = new BinaryTree<T>(values, index * 2 + 1);
}
if (index * 2 + 2 < values.Length)
{
this.right = new BinaryTree<T>(values, index * 2 + 2);
}
}
public static string ToExpression(BinaryTree<T> tree)
{
var nulls = 0;
if (tree.left == null)
nulls++;
if (tree.right == null)
nulls++;
if (nulls >= 2)
return tree.value.ToString();
if (nulls == 1)
{
BinaryTree<T> child;
if (tree.left == null)
child = tree.right;
else child = tree.left;
return tree.value.ToString() + "( " + child.value.ToString() + " )";
}
return "( " + ToExpression(tree.left) + " )" + tree.value.ToString() + "( " + ToExpression(tree.right) +" )";
}
public BinaryTree<T> Clone()
{
BinaryTree<T> clone = (BinaryTree<T>)MemberwiseClone();
if (left != null)
clone.left = left.Clone();
if (right != null)
clone.right = right.Clone();
return clone;
}
}
}