-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
349 lines (306 loc) · 9.26 KB
/
Program.cs
File metadata and controls
349 lines (306 loc) · 9.26 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
using System;
using System.Diagnostics;
namespace ConsoleApp1
{
class BST
{
private class Node
{
private Node left;
private Node right;
private int data;
private int height;
public int Height
{
get { return height; }
set { height = value; }
}
public int Data
{
get { return data; }
set { data = value; }
}
public Node Left
{
get { return left; }
set { left = value; }
}
public Node Right
{
get { return right; }
set { right = value; }
}
public Node(int n_data)
{
data = n_data;
left = null;
right = null;
}
}
private Node root;
public BST()
{
root = null;
}
private int update_height(Node branch)
{
if (branch == null) return -1;
int lh = 1 + update_height(branch.Left);
int rh = 1 + update_height(branch.Right);
branch.Height = (lh > rh ? lh : rh);
return branch.Height;
}
private bool Insert(int x, Node branch)
{
if (root == null)
{
root = new Node(x);
return true;
}
else if (branch.Left == null && branch.Data > x)
{
branch.Left = new Node(x);
return true;
}
else if (branch.Right == null && branch.Data < x)
{
branch.Right = new Node(x);
return true;
}
else if (branch.Data < x)
{
if(Insert(x, branch.Right))
{
update_height(branch);
balance(branch);
return true;
}
return false;
}
else if (branch.Data > x)
{
if(Insert(x, branch.Left))
{
update_height(branch);
balance(branch);
return true;
}
return false;
}
return false;
}
public void Insert(int x)
{
if (Insert(x, root))
{
System.Console.WriteLine("Inserted: " + x);
}
else
{
System.Console.WriteLine("Failed to insert: " + x);
}
}
public void print()
{
update_height(root);
print(root);
}
public void print_root()
{
Console.WriteLine("Root is: " + root.Data + "|| Height: " + root.Height);
}
private void print(Node branch)
{
if (branch != null)
{
print(branch.Left);
System.Console.Write("<" + branch.Data + " : H " + branch.Height + ">" + " ");
print(branch.Right);
}
}
private int get_balance(Node branch)
{
if(branch == null)
{
return -1;
}
int lh = (branch.Left == null ? -1 : branch.Left.Height);
int rh = (branch.Right == null ? -1 : branch.Right.Height);
return rh - lh;
}
private bool at_root(Node branch)
{
return branch.Data == root.Data;
}
private bool at_root(int data)
{
return data == root.Data;
}
private Node get_parent(int data)
{
if(root == null || at_root(data))
{
return null;
}
return helper_get_parent(data, root);
}
private Node helper_get_parent(int data, Node branch)
{
if(branch == null)
{
return null;
}
else if(branch.Left.Data == data || branch.Right.Data == data)
{
return branch;
}
else if(branch.Data > data)
{
return helper_get_parent(data, branch.Left);
}
else
{
return helper_get_parent(data, branch.Right);
}
}
private void balance(Node branch)
{
int balance_val = get_balance(branch);
if(balance_val > 1)
{
Node parent = get_parent(branch.Data);
Node temp = branch.Right;
if(get_balance(branch.Right) == -1) // right left
{
branch.Right = temp.Left;
temp.Left = branch.Right.Right;
branch.Right.Right = temp;
}
// right right
branch.Right = temp.Left;
bool isroot = at_root(branch.Data);
temp.Left = branch;
if(isroot)
{
root = temp;
}
else
{
if (parent.Left == branch)
parent.Left = temp;
else
parent.Right = temp;
}
}
else if(balance_val < -1)
{
Node parent = get_parent(branch.Data);
Node temp = branch.Left;
if(get_balance(branch.Left) == 1) // left right
{
branch.Left = temp.Right;
temp.Right = branch.Left.Left;
branch.Left.Left = temp;
}
// left left
branch.Left = temp.Right;
temp.Right = branch;
bool isroot = at_root(branch.Data);
if(isroot)
{
root = temp;
}
else
{
if (parent.Left == branch)
parent.Left = temp;
else
parent.Right = temp;
}
}
}
private void print_at_height(Node branch, int height)
{
if (branch != null)
{
print_at_height(branch.Left, height);
if(height == branch.Height)
System.Console.Write("<" + branch.Data + " : H " + branch.Height + ">" + " ");
print_at_height(branch.Right, height);
}
}
public void print_at_height(int height)
{
print_at_height(root,height);
}
public void testInsert()
{
Insert(3);
Insert(1);
Insert(2);
Insert(6);
Insert(4);
Insert(7);
Debug.Assert(root.Data == 3);
Debug.Assert(root.Left.Data == 1);
Debug.Assert(root.Left.Right.Data == 2);
Debug.Assert(root.Right.Data == 6);
Debug.Assert(root.Right.Right.Data == 7);
Debug.Assert(root.Right.Left.Data == 4);
}
// to be tested with using a separate object since it it directly
// modifying root
public void testUpdateHeight()
{
Insert(3);
Insert(1);
Insert(2);
Insert(6);
Insert(4);
Insert(7);
update_height(root);
Debug.Assert(root.Height == 2);
Debug.Assert(root.Left.Height == 1);
Debug.Assert(root.Left.Right.Height == 0);
Debug.Assert(root.Right.Height == 1);
Debug.Assert(root.Right.Right.Height == 0);
Debug.Assert(root.Right.Left.Height == 0);
}
// can be used to check if the tree's height is correct
public void testIntegrity()
{
testIntegrity(root);
}
private void testIntegrity(Node branch)
{
if (branch == null)
return;
int lh = (branch.Left == null ? -1 : branch.Left.Data);
int rh = (branch.Right == null ? -1 : branch.Right.Data);
Debug.Assert((rh - lh <= 1) && ( rh - lh >= -1));
}
}
class Program
{
static void Main(string[] args)
{
BST obj = new BST();
for (int i = 0; i < 100; ++i)
{
obj.Insert(i);
Console.WriteLine();
Console.WriteLine();
obj.print_root();
}
obj.print();
obj.testIntegrity();
for(int i = 0; i < 7; ++i)
{
obj.print_at_height(i);
Console.WriteLine();
Console.WriteLine();
}
Console.ReadLine();
}
}
}