-
Notifications
You must be signed in to change notification settings - Fork 1
/
IsBSTBalanced.cs
184 lines (165 loc) · 5.08 KB
/
IsBSTBalanced.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 Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
// Determine if a Binary Search Tree (BST) is balanced.
//
// Compiled: C# - Visual Studio 2013
namespace IsBSTBalanced
{
public class Node<T> where T : IComparable<T>
{
public Node<T> Left { get; set; }
public Node<T> Right { get; set; }
public T Value { get; private set ; }
public Node(T value)
{
this.Value = value;
}
}
/// <summary>
/// Partial BST implementation with no balancing logic.
/// </summary>
public class BSTree<T> where T : IComparable<T>
{
public Node<T> Root { get; private set; }
/// <summary>
/// Checks if a Binary Search Tree (BST) is balanced. A balanced tree is one in which
/// the heights of the two subtrees of any node never differ by more than one.
/// </summary>
public bool IsBalanced
{
get { return (GetHeight(Root) != -1); }
}
/// <summary>
/// Recursively check height
/// O(n) time and O(log N) space.
/// </summary>
public int GetHeight(Node<T> root)
{
if (root == null)
return 0;
// Check left
int leftSubtreeHeight = GetHeight(root.Left);
if (leftSubtreeHeight == -1)
return -1; // Left subtree is unbalanced
// Check right
int rightSubTreeHeight = GetHeight(root.Right);
if (rightSubTreeHeight == -1)
return -1; // Right subtree is unbalanced
// Check current
if (Math.Abs(leftSubtreeHeight - rightSubTreeHeight) > 1)
return -1; // Unbalanced
else
return Math.Max(leftSubtreeHeight, rightSubTreeHeight) + 1; // left + right + current
}
/// <summary>
/// Iterative insert
/// </summary>
public void Insert(T value)
{
var newNode = new Node<T>(value);
var curr = Root;
if (curr == null)
{
Root = newNode;
return;
}
while(true)
{
if (newNode.Value.CompareTo(curr.Value) < 0)
{
if (curr.Left != null) // Common case first
{
curr = curr.Left; // Search left
}
else
{
curr.Left = newNode; // Insert left
return;
}
}
else if (newNode.Value.CompareTo(curr.Value) > 0)
{
if (curr.Right != null)
{
curr = curr.Right;
}
else
{
curr.Right = newNode;
return;
}
}
else
{
throw new NotSupportedException("Duplicates not supported.");
}
}
}
}
[TestClass]
public class BSTreeTests
{
[TestMethod]
public void IsBalanced_WhenEmpty_ExpectBalanced()
{
var sut = new BSTree<int>();
Assert.IsTrue(sut.IsBalanced);
}
[TestMethod]
public void IsBalanced_WhenLeftHeightGreaterByOne_ExpectBalanced()
{
var sut = new BSTree<int>();
sut.Insert(10);
sut.Insert(5);
sut.Insert(3);
sut.Insert(15);
Assert.IsTrue(sut.IsBalanced);
}
[TestMethod]
public void IsBalanced_WhenRightHeightGreaterByOne_ExpectBalanced()
{
var sut = new BSTree<int>();
sut.Insert(10);
sut.Insert(5);
sut.Insert(15);
sut.Insert(13);
Assert.IsTrue(sut.IsBalanced);
}
[TestMethod]
public void IsBalanced_WhenLeftHeightGreaterByTwo_ExpectUnbalanced()
{
var sut = new BSTree<int>();
sut.Insert(10);
sut.Insert(5);
sut.Insert(3);
sut.Insert(2);
sut.Insert(15);
Assert.IsFalse(sut.IsBalanced);
}
[TestMethod]
public void IsBalanced_WhenRightHeightGreaterByTwo_ExpectUnbalanced()
{
var sut = new BSTree<int>();
sut.Insert(10);
sut.Insert(5);
sut.Insert(15);
sut.Insert(17);
sut.Insert(18);
Assert.IsFalse(sut.IsBalanced);
}
}
//public static class TestProgram
//{
// public static void Main()
// {
// var tree = new BTree<int>();
// tree.Insert(10);
// tree.Insert(5);
// Console.WriteLine(tree.IsBalanced());
// tree.Insert(3);
// Console.WriteLine(tree.IsBalanced());
// Console.WriteLine("Press [Enter Key] to exit.");
// Console.ReadLine();
// }
//}
}