-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeNode.cs
184 lines (156 loc) · 4.05 KB
/
TreeNode.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
using System;
namespace DataStructures
{
class TreeNode
{
public int Data;
public TreeNode Left;
public TreeNode Right;
public TreeNode Parent;
public TreeNode(int number)
{
Data = number;
}
public void SetLeftChild(TreeNode left)
{
Left = left;
if (left != null)
{
left.Parent = this;
}
}
public void SetRightChild(TreeNode right)
{
Right = right;
if (right != null)
{
right.Parent = this;
}
}
public void InsertInOrder(int number)
{
if (number <= Data)
{
if (Left == null)
{
SetLeftChild(new TreeNode(number));
}
else
{
Left.InsertInOrder(number);
}
}
else
{
if (Right == null)
{
SetRightChild(new TreeNode(number));
}
else
{
Right.InsertInOrder(number);
}
}
}
public bool IsBst()
{
if (Left != null)
{
if (Data < Left.Data || !Left.IsBst())
{
return false;
}
}
if (Right != null)
{
if (Data >= Right.Data || !Right.IsBst())
{
return false;
}
}
return true;
}
public int Height()
{
var leftHeight = Left != null ? Left.Height() : 0;
var rightHeight = Right != null ? Right.Height() : 0;
return 1 + Math.Max(leftHeight, rightHeight);
}
public TreeNode Find(int number)
{
if (number == Data)
{
return this;
}
else if (number <= Data)
{
return Left != null ? Left.Find(number) : null;
}
else if (number > Data)
{
return Right != null ? Right.Find(number) : null;
}
return null;
}
private static TreeNode AddToTree(int[] array, int start, int end)
{
if (end < start)
{
return null;
}
var mid = (start + end) / 2;
var node = new TreeNode(array[mid]);
node.SetLeftChild(AddToTree(array, start, mid - 1));
node.SetRightChild(AddToTree(array, mid + 1, end));
return node;
}
public static TreeNode CreateMinimalBst(int[] array)
{
return AddToTree(array, 0, array.Length - 1);
}
/*
* Example code for Main method:
* Console.WriteLine(DateTime.Now.Millisecond);
Console.WriteLine();
// Create a balanced tree
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
var root = TreeNode.CreateMinimalBst(array);
Console.WriteLine("Root = " + root.Data);
Console.WriteLine("Balanced = " + IsBalanced(root));
// Create an unbalanced tree
var unbalanced = new TreeNode(10);
var random = new Random();
for (var loopCounter = 0; loopCounter < 10; loopCounter++)
{
unbalanced.InsertInOrder(random.Next(0, 100));
}
Console.WriteLine("Root = " + unbalanced.Data);
Console.WriteLine("Balanced = " + IsBalanced(unbalanced));
Console.WriteLine();
Console.WriteLine(DateTime.Now.Millisecond);
var dummy = Console.ReadLine();
*
* more helper methods:
*public static int MaxDepth(TreeNode root)
{
if (root == null)
{
return 0;
}
return 1 + Math.Max(MaxDepth(root.Left), MaxDepth(root.Right));
}
public static int MinDepth(TreeNode root)
{
if (root == null)
{
return 0;
}
return 1 + Math.Min(MinDepth(root.Left), MinDepth(root.Right));
}
public static bool IsBalanced(TreeNode root)
{
return (MaxDepth(root) - MinDepth(root) <= 1);
}
*/
}
}