My favorites | Sign in
Logo
                
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
/*
Copyright 2007-2009 The NGenerics Team
(http://code.google.com/p/ngenerics/wiki/Team)

This program is licensed under the GNU Lesser General Public License (LGPL). You should
have received a copy of the license along with the source code. If not, an online copy
of the license can be found at http://www.gnu.org/copyleft/lesser.html.
*/


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace NGenerics.DataStructures.Trees
{
/// <summary>
/// An implementation of a Binary Search Tree data structure.
/// </summary>
/// <typeparam name="TKey">The type of the keys in the tree.</typeparam>
/// <typeparam name="TValue">The type of the values in the tree.</typeparam>
[SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
#if (!SILVERLIGHT)
[Serializable]
#endif
public class BinarySearchTree<TKey, TValue> : BinarySearchTreeBase<TKey, TValue>
{
#region Construction

/// <inheritdoc />
public BinarySearchTree()
{
}


/// <inheritdoc />
public BinarySearchTree(IComparer<TKey> comparer)
: base(comparer)
{
}

/// <inheritdoc />
public BinarySearchTree(Comparison<TKey> comparison)
: base(comparison)
{
}

#endregion

#region Public Members

#endregion

#region Private Members

/// <summary>
/// Finds the node containing the specified data item.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="parent">The parent node of the item found.</param>
/// <returns>The node in the tree with the specified key if found, otherwise null.</returns>
private BinaryTree<KeyValuePair<TKey, TValue>> FindNode(TKey key, out BinaryTree<KeyValuePair<TKey, TValue>> parent)
{
var currentNode = Tree;
parent = null;
var searchItem = new KeyValuePair<TKey, TValue>(key, default(TValue));

while (currentNode != null)
{
var nodeResult = Comparer.Compare(searchItem, currentNode.Data);

if (nodeResult == 0)
{
return currentNode;
}

if (nodeResult < 0)
{
parent = currentNode;
currentNode = currentNode.Left;
}
else
{
parent = currentNode;
currentNode = currentNode.Right;
}
}

return null;
}


/// <summary>
/// Finds the maximum node.
/// </summary>
/// <param name="startNode">The start node.</param>
/// <param name="parent">The parent of the node found.</param>
/// <returns>The maximum node underneath the node specified. If the node specified is a leaf node, it is returned.</returns>
private static BinaryTree<KeyValuePair<TKey, TValue>> FindMaximumNode(BinaryTree<KeyValuePair<TKey, TValue>> startNode, out BinaryTree<KeyValuePair<TKey, TValue>> parent)
{
#region Asserts

Debug.Assert(startNode != null);

#endregion

var searchNode = startNode;
parent = null;

while (searchNode.Right != null)
{
parent = searchNode;
searchNode = searchNode.Right;
}

return searchNode;
}

#endregion

#region IDictionary<TKey,TValue> Members

/// <inheritdoc />
protected override void AddItem(KeyValuePair<TKey, TValue> item)
{
if (Tree == null)
{
Tree = new BinaryTree<KeyValuePair<TKey, TValue>>(item);
}
else
{
var currentNode = Tree;

// Find the place to insert the item.
// - If the item is found, throw an exception - no duplicate items allowed.
// - If a leaf is reached, insert the item in the correct place.
// - Else, traverse the tree further
while (true)
{
var nodeResult = Comparer.Compare(item, currentNode.Data);

if (nodeResult == 0)
{
throw new ArgumentException(alreadyContainedInTheTree, "item");
}

if (nodeResult < 0)
{
if (currentNode.Left == null)
{
currentNode.Left = new BinaryTree<KeyValuePair<TKey, TValue>>(item);
break;
}

currentNode = currentNode.Left;
}
else
{
if (currentNode.Right == null)
{
currentNode.Right = new BinaryTree<KeyValuePair<TKey, TValue>>(item);
break;
}

currentNode = currentNode.Right;
}
}
}
}

/// <inheritdoc />
protected override bool RemoveItem(KeyValuePair<TKey, TValue> item)
{
BinaryTree<KeyValuePair<TKey, TValue>> parentNode;
var currentNode = FindNode(item.Key, out parentNode);

// The node wasn't found in the tree.
if (currentNode == null)
{
return false;
}

if (currentNode.Degree == 2)
{
// Find the element with the largest key in the left sub-tree
BinaryTree<KeyValuePair<TKey, TValue>> searchParent;
var searchNode = FindMaximumNode(currentNode.Left, out searchParent);

parentNode = searchParent ?? currentNode;

currentNode.Data = searchNode.Data;
currentNode = searchNode;
}

var leftOverChild = currentNode.Left ?? currentNode.Right;

if (currentNode == Tree)
{
Tree = leftOverChild;
}
else
{
if (currentNode == parentNode.Left)
{
parentNode.Left = leftOverChild;
}
else
{
parentNode.Right = leftOverChild;
}
}

return true;
}

#endregion
}
}
Show details Hide details

Change log

r8 by rhanekom on Aug 08, 2009   Diff
- Added ignores for bin and obj
- Added License file and lgpl copy.
- License header updated in all source
files.
Go to: 
Sign in to write a code review

Older revisions

r2 by Simon.Cropp on Aug 07, 2009   Diff
initial port of code from codeplex
All revisions of this file

File info

Size: 6916 bytes, 219 lines
Hosted by Google Code