Skip to content

Commit

Permalink
Merge pull request #22 from jnyrup/MinMax
Browse files Browse the repository at this point in the history
Fix Min/Max copy-paste error
  • Loading branch information
apacha authored Apr 23, 2019
2 parents e0d9c60 + 7beb6c0 commit e2c5778
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
21 changes: 19 additions & 2 deletions RangeTree/RangeTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,27 @@ public class RangeTree<TKey, TValue> : IRangeTree<TKey, TValue>

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

public TKey Max => root.Max;
public TKey Max
{
get
{
if (!isInSync)
Rebuild();

public TKey Min => root.Min;
return root.Max;
}
}

public TKey Min
{
get
{
if (!isInSync)
Rebuild();

return root.Min;
}
}

public IEnumerable<TValue> Values => items.Select(i => i.Value);

Expand Down
4 changes: 2 additions & 2 deletions RangeTree/RangeTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ public TKey Min
get
{
if (leftNode != null)
return leftNode.Max;
return leftNode.Min;
else if (items != null)
return items.Max(i => i.From);
return items.Min(i => i.From);
else
return default(TKey);
}
Expand Down
71 changes: 71 additions & 0 deletions RangeTreeTests/RangeTreeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using NUnit.Framework;
using RangeTree;
using System;
using System.Collections.Generic;
using System.Text;

namespace RangeTreeTests
{
[TestFixture]
public class RangeTreeTests
{
[Test]
public void GettingMin_InnerItems()
{
var tree = new RangeTree<int, int>
{
{ 1, 5, -1 },
{ 2, 5, -1 },
{ 3, 5, -1 },
};

var min = tree.Min;

Assert.AreEqual(1, min);
}

[Test]
public void GettingMin_LeftRecurse()
{
var tree = new RangeTree<int, int>
{
{ 1, 2, -1 },
{ 3, 4, -1 }
};

var min = tree.Min;

Assert.AreEqual(1, min);
}

[Test]
public void GettingMax_InnerItems()
{
var tree = new RangeTree<int, int>
{
{ 1, 2, -1 },
{ 1, 3, -1 },
{ 1, 4, -1 },
};

var max = tree.Max;

Assert.AreEqual(4, max);
}

[Test]
public void GettingMax_RightRecurse()
{
var tree = new RangeTree<int, int>
{
{ 1, 2, -1 },
{ 3, 4, -1 },
{ 5, 6, -1 }
};

var max = tree.Max;

Assert.AreEqual(6, max);
}
}
}

0 comments on commit e2c5778

Please sign in to comment.