Skip to content

Commit

Permalink
Merge branch 'master' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoding121 committed Jan 5, 2019
2 parents 6c74c1d + 3df0acc commit 891944c
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 22 deletions.
6 changes: 3 additions & 3 deletions docs/api/Advanced.Algorithms.DataStructures.AVLTree-1.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ <h4 id="Advanced_Algorithms_DataStructures_AVLTree_1__ctor_IEnumerable__0__Syste
<div class="markdown level1 conceptual"></div>
<h5 class="decalaration">Declaration</h5>
<div class="codewrapper">
<pre><code class="lang-csharp hljs">public AVLTree(IEnumerable&lt;T&gt; sortedKeys, bool enableNodeLookUp = false)</code></pre>
<pre><code class="lang-csharp hljs">public AVLTree(IEnumerable&lt;T&gt; sortedCollection, bool enableNodeLookUp = false)</code></pre>
</div>
<h5 class="parameters">Parameters</h5>
<table class="table table-bordered table-striped table-condensed">
Expand All @@ -143,8 +143,8 @@ <h5 class="parameters">Parameters</h5>
<tbody>
<tr>
<td><span class="xref">IEnumerable</span>&lt;T&gt;</td>
<td><span class="parametername">sortedKeys</span></td>
<td><p>The sorted keys.</p>
<td><span class="parametername">sortedCollection</span></td>
<td><p>The initial sorted collection.</p>
</td>
</tr>
<tr>
Expand Down
4 changes: 2 additions & 2 deletions docs/api/Advanced.Algorithms.DataStructures.BST-1.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ <h4 id="Advanced_Algorithms_DataStructures_BST_1__ctor_IEnumerable__0__" data-ui
<div class="markdown level1 conceptual"></div>
<h5 class="decalaration">Declaration</h5>
<div class="codewrapper">
<pre><code class="lang-csharp hljs">public BST(IEnumerable&lt;T&gt; sortedKeys)</code></pre>
<pre><code class="lang-csharp hljs">public BST(IEnumerable&lt;T&gt; sortedCollection)</code></pre>
</div>
<h5 class="parameters">Parameters</h5>
<table class="table table-bordered table-striped table-condensed">
Expand All @@ -153,7 +153,7 @@ <h5 class="parameters">Parameters</h5>
<tbody>
<tr>
<td><span class="xref">IEnumerable</span>&lt;T&gt;</td>
<td><span class="parametername">sortedKeys</span></td>
<td><span class="parametername">sortedCollection</span></td>
<td></td>
</tr>
</tbody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ <h4 id="Advanced_Algorithms_DataStructures_RedBlackTree_1__ctor_IEnumerable__0__
<div class="markdown level1 conceptual"></div>
<h5 class="decalaration">Declaration</h5>
<div class="codewrapper">
<pre><code class="lang-csharp hljs">public RedBlackTree(IEnumerable&lt;T&gt; sortedKeys = null)</code></pre>
<pre><code class="lang-csharp hljs">public RedBlackTree(IEnumerable&lt;T&gt; sortedCollection = null)</code></pre>
</div>
<h5 class="parameters">Parameters</h5>
<table class="table table-bordered table-striped table-condensed">
Expand All @@ -143,8 +143,9 @@ <h5 class="parameters">Parameters</h5>
<tbody>
<tr>
<td><span class="xref">IEnumerable</span>&lt;T&gt;</td>
<td><span class="parametername">sortedKeys</span></td>
<td></td>
<td><span class="parametername">sortedCollection</span></td>
<td><p>The sorted initial collection.</p>
</td>
</tr>
</tbody>
</table>
Expand Down
7 changes: 4 additions & 3 deletions docs/api/Advanced.Algorithms.DataStructures.SplayTree-1.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ <h4 id="Advanced_Algorithms_DataStructures_SplayTree_1__ctor_IEnumerable__0__" d
<div class="markdown level1 conceptual"></div>
<h5 class="decalaration">Declaration</h5>
<div class="codewrapper">
<pre><code class="lang-csharp hljs">public SplayTree(IEnumerable&lt;T&gt; collection)</code></pre>
<pre><code class="lang-csharp hljs">public SplayTree(IEnumerable&lt;T&gt; sortedCollection)</code></pre>
</div>
<h5 class="parameters">Parameters</h5>
<table class="table table-bordered table-striped table-condensed">
Expand All @@ -153,8 +153,9 @@ <h5 class="parameters">Parameters</h5>
<tbody>
<tr>
<td><span class="xref">IEnumerable</span>&lt;T&gt;</td>
<td><span class="parametername">collection</span></td>
<td></td>
<td><span class="parametername">sortedCollection</span></td>
<td><p>The sorted collection.</p>
</td>
</tr>
</tbody>
</table>
Expand Down
7 changes: 4 additions & 3 deletions docs/api/Advanced.Algorithms.DataStructures.TreapTree-1.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ <h4 id="Advanced_Algorithms_DataStructures_TreapTree_1__ctor_IEnumerable__0__" d
<div class="markdown level1 conceptual"></div>
<h5 class="decalaration">Declaration</h5>
<div class="codewrapper">
<pre><code class="lang-csharp hljs">public TreapTree(IEnumerable&lt;T&gt; collection)</code></pre>
<pre><code class="lang-csharp hljs">public TreapTree(IEnumerable&lt;T&gt; sortedCollection)</code></pre>
</div>
<h5 class="parameters">Parameters</h5>
<table class="table table-bordered table-striped table-condensed">
Expand All @@ -153,8 +153,9 @@ <h5 class="parameters">Parameters</h5>
<tbody>
<tr>
<td><span class="xref">IEnumerable</span>&lt;T&gt;</td>
<td><span class="parametername">collection</span></td>
<td></td>
<td><span class="parametername">sortedCollection</span></td>
<td><p>The initial sorted collection.</p>
</td>
</tr>
</tbody>
</table>
Expand Down
10 changes: 5 additions & 5 deletions docs/index.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion src/Advanced.Algorithms/DataStructures/Tree/AvlTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ public AVLTree(bool enableNodeLookUp = false)
/// <param name="enableNodeLookUp">Enabling lookup will fasten deletion/insertion/exists operations
/// at the cost of additional space.</param>
public AVLTree(IEnumerable<T> sortedCollection, bool enableNodeLookUp = false)
: this(enableNodeLookUp)
{
ValidateSortedCollection(sortedCollection);
var nodes = sortedCollection.Select(x => new AVLTreeNode<T>(null, x)).ToArray();
Root = (AVLTreeNode<T>)ToBST(nodes);
recomputeHeight(Root);
assignCount(Root);

if (enableNodeLookUp)
{
nodeLookUp = nodes.ToDictionary(x => x.Value, x => x as BSTNodeBase<T>);
}
}


Expand Down
5 changes: 4 additions & 1 deletion src/Advanced.Algorithms/DataStructures/Tree/RedBlackTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ internal bool Exists(T value)
{
if (nodeLookUp != null)
{
return (nodeLookUp[value] as RedBlackTreeNode<T>, Root.Position(value));
//since node look up is only used by Bentley-Ottmann algorithm internally
//and it does'nt need the position we can return defualt(int).
return (nodeLookUp[value] as RedBlackTreeNode<T>, default(int));
}

var result = Root.Find(value);
Expand Down Expand Up @@ -831,6 +833,7 @@ public T NextHigher(T value)
return next != null ? next.Value : default(T);
}

///Special (internal only) method for Bentley-Ottmann sweep line algorithm.
internal void Swap(T value1, T value2)
{
var node1 = find(value1).Item1;
Expand Down
108 changes: 107 additions & 1 deletion tests/Advanced.Algorithms.Tests/DataStructures/Tree/AVLTree_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,77 @@ public void AVLTree_Accuracy_Test()
{
var nodeCount = 1000;

var rnd = new Random();
var sorted = Enumerable.Range(1, nodeCount).ToList();
var randomNumbers = sorted
.OrderBy(x => rnd.Next())
.ToList();

var tree = new AVLTree<int>();

for (int i = 0; i < nodeCount; i++)
{
tree.Insert(randomNumbers[i]);

Assert.IsTrue(tree.HasItem(randomNumbers[i]));
Assert.IsTrue(tree.Root.IsBinarySearchTree(int.MinValue, int.MaxValue));
tree.Root.VerifyCount();

var actualHeight = tree.GetHeight();

//http://stackoverflow.com/questions/30769383/finding-the-minimum-and-maximum-height-in-a-avl-tree-given-a-number-of-nodes
var maxHeight = 1.44 * Math.Log(nodeCount + 2, 2) - 0.328;

Assert.IsTrue(actualHeight < maxHeight);
Assert.IsTrue(tree.Count == i + 1);
}

for (int i = 0; i < sorted.Count; i++)
{
Assert.AreEqual(sorted[i], tree.ElementAt(i));
Assert.AreEqual(i, tree.IndexOf(sorted[i]));
}

randomNumbers = Enumerable.Range(1, nodeCount)
.OrderBy(x => rnd.Next())
.ToList();

//IEnumerable test using linq
Assert.AreEqual(tree.Count, tree.Count());
Assert.AreEqual(tree.Count, tree.AsEnumerableDesc().Count());

for (int i = 0; i < nodeCount; i++)
{
if (rnd.NextDouble() >= 0.5)
{
tree.Delete(randomNumbers[i]);
}
else
{
var index = tree.IndexOf(randomNumbers[i]);
Assert.AreEqual(tree.ElementAt(index), randomNumbers[i]);
tree.RemoveAt(index);
}

Assert.IsTrue(tree.Root.IsBinarySearchTree(int.MinValue, int.MaxValue));
tree.Root.VerifyCount();

var actualHeight = tree.GetHeight();

//http://stackoverflow.com/questions/30769383/finding-the-minimum-and-maximum-height-in-a-avl-tree-given-a-number-of-nodes
var maxHeight = 1.44 * Math.Log(nodeCount + 2, 2) - 0.328;

Assert.IsTrue(actualHeight < maxHeight);
}

Assert.IsTrue(tree.Count == 0);
}

[TestMethod]
public void AVLTree_Accuracy_Test_With_Node_LookUp()
{
var nodeCount = 1000;

var rnd = new Random();
var sorted = Enumerable.Range(1, nodeCount).ToList();
var randomNumbers = sorted
Expand Down Expand Up @@ -165,7 +236,7 @@ public void AVLTree_Accuracy_Test()
}

[TestMethod]
public void AVLTree_BulkInit_Test()
public void AVLTree_BulkInit_Test_With_Node_LookUp()
{
var nodeCount = 1000;

Expand Down Expand Up @@ -199,6 +270,41 @@ public void AVLTree_BulkInit_Test()
Assert.IsTrue(tree.Count == 0);
}

[TestMethod]
public void AVLTree_BulkInit_Test()
{
var nodeCount = 1000;

var rnd = new Random();
var randomNumbers = Enumerable.Range(1, nodeCount).ToList();

var tree = new AVLTree<int>(randomNumbers, true);

Assert.IsTrue(tree.Root.IsBinarySearchTree(int.MinValue, int.MaxValue));
Assert.AreEqual(tree.Count, tree.Count());

tree.Root.VerifyCount();

for (int i = 0; i < nodeCount; i++)
{
tree.Delete(randomNumbers[i]);

tree.Root.VerifyCount();
Assert.IsTrue(tree.Root.IsBinarySearchTree(int.MinValue, int.MaxValue));

var actualHeight = tree.GetHeight();

//http://stackoverflow.com/questions/30769383/finding-the-minimum-and-maximum-height-in-a-avl-tree-given-a-number-of-nodes
var maxHeight = 1.44 * Math.Log(nodeCount + 2, 2) - 0.328;

Assert.IsTrue(actualHeight < maxHeight);

Assert.IsTrue(tree.Count == nodeCount - 1 - i);
}

Assert.IsTrue(tree.Count == 0);
}

[TestMethod]
public void AVLTree_Stress_Test()
{
Expand Down

0 comments on commit 891944c

Please sign in to comment.