Skip to content

Commit 9cce7ae

Browse files
committed
docs & range tree duplicate point support
1 parent dc1bb08 commit 9cce7ae

File tree

3 files changed

+50
-18
lines changed

3 files changed

+50
-18
lines changed

Advanced.Algorithms.Tests/DataStructures/Tree/RangeTreeTests.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,21 @@ public void RangeTree1D_Smoke_Test()
1919
tree.Insert(new int[] { 2 });
2020
tree.Insert(new int[] { 3 });
2121
tree.Insert(new int[] { 4 });
22+
tree.Insert(new int[] { 4 });
2223
tree.Insert(new int[] { 5 });
2324
tree.Insert(new int[] { 6 });
2425
tree.Insert(new int[] { 7 });
2526

2627
var rangeResult = tree.GetInRange(new int[] { 2 }, new int[] { 6 });
27-
Assert.IsTrue(rangeResult.Count == 5);
28+
Assert.IsTrue(rangeResult.Count == 6);
2829

2930
tree.Delete(new int[] { 4 });
3031
rangeResult = tree.GetInRange(new int[] { 2 }, new int[] { 6 });
31-
Assert.IsTrue(rangeResult.Count == 4);
32+
Assert.IsTrue(rangeResult.Count == 5);
3233

34+
tree.Delete(new int[] { 4 });
35+
rangeResult = tree.GetInRange(new int[] { -1 }, new int[] { 6 });
36+
Assert.IsTrue(rangeResult.Count == 6);
3337

3438
tree.Delete(new int[] { 0 });
3539
tree.Delete(new int[] { 1 });
@@ -51,6 +55,7 @@ public void RangeTree2D_Smoke_Test()
5155
tree.Insert(new int[] { 2, 5 });
5256
tree.Insert(new int[] { 3, 6 });
5357
tree.Insert(new int[] { 4, 5 });
58+
tree.Insert(new int[] { 4, 7 });
5459
tree.Insert(new int[] { 5, 8 });
5560
tree.Insert(new int[] { 6, 9 });
5661
tree.Insert(new int[] { 7, 10 });
@@ -69,9 +74,12 @@ public void RangeTree2D_Smoke_Test()
6974
tree.Delete(new int[] { 0, 1 });
7075
tree.Delete(new int[] { 1, 1 });
7176
tree.Delete(new int[] { 4, 5 });
77+
tree.Delete(new int[] { 4, 7 });
7278
tree.Delete(new int[] { 5, 8 });
7379
tree.Delete(new int[] { 6, 9 });
7480
tree.Delete(new int[] { 7, 10 });
81+
82+
7583
}
7684
}
77-
}
85+
}

Advanced.Algorithms/Advanced.Algorithms.Docs.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<Compile Include="DataStructures\Dictionary\Dictionary.cs" />
6565
<Compile Include="DataStructures\Dictionary\OpenAddressDictionary.cs" />
6666
<Compile Include="DataStructures\Dictionary\SeparateChainingDictionary.cs" />
67-
<Compile Include="DataStructures\Dictionary\TreeDictionary.cs" />
67+
<Compile Include="DataStructures\Dictionary\SortedDictionary.cs" />
6868
<Compile Include="DataStructures\Graph\AdjacencyList\DiGraph.cs" />
6969
<Compile Include="DataStructures\Graph\AdjacencyList\Graph.cs" />
7070
<Compile Include="DataStructures\Graph\AdjacencyList\WeightedDiGraph.cs" />
@@ -76,7 +76,7 @@
7676
<Compile Include="DataStructures\HashSet\HashSet.cs" />
7777
<Compile Include="DataStructures\HashSet\OpenAddressHashSet.cs" />
7878
<Compile Include="DataStructures\HashSet\SeparateChainingHashSet.cs" />
79-
<Compile Include="DataStructures\HashSet\TreeHashSet.cs" />
79+
<Compile Include="DataStructures\HashSet\SortedHashSet.cs" />
8080
<Compile Include="DataStructures\Heap\BinomialHeapNode.cs" />
8181
<Compile Include="DataStructures\Heap\FibornacciHeapNode.cs" />
8282
<Compile Include="DataStructures\Heap\Max\BinomialMaxHeap.cs" />

Advanced.Algorithms/DataStructures/Tree/RangeTree.cs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Collections.Generic;
34

45
namespace Advanced.Algorithms.DataStructures
@@ -9,18 +10,20 @@ namespace Advanced.Algorithms.DataStructures
910
/// <typeparam name="T"></typeparam>
1011
internal class RangeTreeNode<T> : IComparable where T : IComparable
1112
{
12-
internal T Data { get; set; }
13+
internal T Value => Values[0];
14+
15+
internal List<T> Values { get; set; }
1316

1417
internal RangeTree<T> tree { get; set; }
1518

1619
public int CompareTo(object obj)
1720
{
18-
return Data.CompareTo(((RangeTreeNode<T>)obj).Data);
21+
return Value.CompareTo(((RangeTreeNode<T>)obj).Value);
1922
}
2023

2124
public RangeTreeNode(T value)
2225
{
23-
Data = value;
26+
Values = new List<T>(new T[] { value });
2427
tree = new RangeTree<T>();
2528
}
2629
}
@@ -142,10 +145,10 @@ private List<T[]> getInRange(
142145
{
143146
var result = new List<T[]>();
144147

145-
foreach (var node in nodes)
148+
foreach (var value in nodes.SelectMany(x => x.Values))
146149
{
147150
var thisDimResult = new T[dimensions];
148-
thisDimResult[dimension] = node.Data;
151+
thisDimResult[dimension] = value;
149152
result.Add(thisDimResult);
150153
}
151154

@@ -159,10 +162,13 @@ private List<T[]> getInRange(
159162
{
160163
var nextDimResult = getInRange(node.tree, start, end, dimension + 1);
161164

162-
foreach (var nextResult in nextDimResult)
165+
foreach (var value in node.Values)
163166
{
164-
nextResult[dimension] = node.Data;
165-
result.Add(nextResult);
167+
foreach (var nextResult in nextDimResult)
168+
{
169+
nextResult[dimension] = value;
170+
result.Add(nextResult);
171+
}
166172
}
167173
}
168174

@@ -215,13 +221,31 @@ public RangeTreeNode<T> Find(T value)
215221
internal RangeTreeNode<T> Insert(T value)
216222
{
217223
var newNode = new RangeTreeNode<T>(value);
224+
225+
var existing = tree.FindNode(newNode);
226+
if (existing != null)
227+
{
228+
existing.Value.Values.Add(value);
229+
return existing.Value;
230+
}
231+
218232
tree.Insert(newNode);
219233
return newNode;
220234
}
221235

222236
internal void Delete(T value)
223237
{
224-
tree.Delete(new RangeTreeNode<T>(value));
238+
var existing = tree.FindNode(new RangeTreeNode<T>(value));
239+
240+
if (existing.Value.Values.Count == 1)
241+
{
242+
tree.Delete(new RangeTreeNode<T>(value));
243+
return;
244+
}
245+
246+
//remove last
247+
existing.Value.Values.RemoveAt(existing.Value.Values.Count - 1);
248+
225249
}
226250

227251
internal List<RangeTreeNode<T>> GetInRange(T start, T end)
@@ -251,7 +275,7 @@ private List<RangeTreeNode<T>> getInRange(List<RangeTreeNode<T>> result, System.
251275
//move left
252276
else
253277
{
254-
if (start.CompareTo(currentNode.Value.Data) <= 0)
278+
if (start.CompareTo(currentNode.Value.Value) <= 0)
255279
{
256280
if (currentNode.Left != null)
257281
{
@@ -269,7 +293,7 @@ private List<RangeTreeNode<T>> getInRange(List<RangeTreeNode<T>> result, System.
269293
//if start is greater than current
270294
//and end is greater than current
271295
//move right
272-
if (end.CompareTo(currentNode.Value.Data) < 0)
296+
if (end.CompareTo(currentNode.Value.Value) < 0)
273297
{
274298
return result;
275299
}
@@ -304,8 +328,8 @@ private List<RangeTreeNode<T>> getInRange(List<RangeTreeNode<T>> result, System.
304328
private bool inRange(RedBlackTreeNode<RangeTreeNode<T>> currentNode, T start, T end)
305329
{
306330
//start is less than current & end is greater than current
307-
return start.CompareTo(currentNode.Value.Data) <= 0
308-
&& end.CompareTo(currentNode.Value.Data) >= 0;
331+
return start.CompareTo(currentNode.Value.Value) <= 0
332+
&& end.CompareTo(currentNode.Value.Value) >= 0;
309333
}
310334
}
311335
}

0 commit comments

Comments
 (0)