Skip to content

Commit 506a44a

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents a327cae + d1ca661 commit 506a44a

13 files changed

+207
-254
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,6 @@ _Pvt_Extensions/
190190
ModelManifest.xml
191191
.vs/
192192
.idea/
193+
194+
# Local test runner
195+
testrunner

.travis.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
language: csharp
2-
solution: C-Sharp-Algorithms.sln
3-
dotnet: 2.0.3
42
mono: none
5-
dist: trusty
3+
dotnet: 2.1.502
4+
solution: C-Sharp-Algorithms.sln
65
install:
76
- dotnet restore
87
script:
9-
- dotnet build
10-
- dotnet test UnitTest/UnitTest.csproj
8+
- dotnet msbuild /p:Configuration=Release C-Sharp-Algorithms.sln
9+
- dotnet restore
10+
- dotnet test UnitTest/UnitTest.csproj

Algorithms/Algorithms.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
<TargetFramework>netcoreapp2.0</TargetFramework>
44
</PropertyGroup>
55

6-
<ItemGroup>
7-
<PackageReference Include="xunit" Version="2.4.0" />
8-
</ItemGroup>
9-
106
<ItemGroup>
117
<ProjectReference Include="..\DataStructures\DataStructures.csproj" />
128
</ItemGroup>

Algorithms/Graphs/BellmanFordShortestPaths.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,23 @@ public class BellmanFordShortestPaths<TGraph, TVertex>
3535
/// </summary>
3636
public BellmanFordShortestPaths(TGraph Graph, TVertex Source)
3737
{
38-
if (Graph == null)
39-
throw new ArgumentNullException();
40-
if (!Graph.HasVertex(Source))
41-
throw new ArgumentException("The source vertex doesn't belong to graph.");
38+
if (Graph == null) {
39+
throw new ArgumentNullException ();
40+
} else {
41+
if (!Graph.HasVertex (Source))
42+
throw new ArgumentException ("The source vertex doesn't belong to graph.");
4243

43-
// Init
44-
_initializeDataMembers(Graph);
44+
// Init
45+
_initializeDataMembers (Graph);
4546

46-
// Traverse the graph
47-
var status = _bellmanFord(Graph, Source);
47+
// Traverse the graph
48+
var status = _bellmanFord (Graph, Source);
4849

49-
if (status == false)
50-
throw new Exception("Negative-weight cycle detected.");
50+
if (status == false)
51+
throw new Exception ("Negative-weight cycle detected.");
5152

52-
Debug.Assert(_checkOptimalityConditions(Graph, Source));
53+
Debug.Assert (_checkOptimalityConditions (Graph, Source));
54+
}
5355
}
5456

5557

DataStructures/DataStructures.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
<TargetFramework>netcoreapp2.0</TargetFramework>
44
</PropertyGroup>
55

6-
<ItemGroup>
7-
<PackageReference Include="xunit" Version="2.4.0" />
8-
</ItemGroup>
9-
106
<ItemGroup>
117
<Content Include="Data\PrimesDocument_10K.csv">
128
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace DataStructures.Trees
6+
{
7+
public class TernarySearchTree
8+
{
9+
public TernaryTreeNode Root { get; private set; }
10+
11+
public void Insert(string word)
12+
{
13+
if (string.IsNullOrWhiteSpace(word))
14+
throw new Exception("Inputted value is empty");
15+
16+
if (Root == null)
17+
Root = new TernaryTreeNode(null, word[0], word.Length == 1);
18+
19+
WordInsertion(word);
20+
}
21+
22+
public void Insert(string[] words)
23+
{
24+
foreach (var word in words)
25+
{
26+
Insert(word);
27+
}
28+
}
29+
30+
void WordInsertion(string word)
31+
{
32+
int index = 0;
33+
TernaryTreeNode currentNode = Root;
34+
35+
while (index < word.Length)
36+
currentNode = ChooseNode(currentNode, word, ref index);
37+
}
38+
39+
TernaryTreeNode ChooseNode(TernaryTreeNode currentNode, string word, ref int index)
40+
{
41+
//Center Branch
42+
if (word[index] == currentNode.Value)
43+
{
44+
index++;
45+
46+
if (currentNode.GetMiddleChild == null)
47+
InsertInTree(currentNode.AddMiddleChild(word[index], word.Length == index + 1), word, ref index);
48+
49+
50+
return currentNode.GetMiddleChild;
51+
}
52+
//Right Branch
53+
else if (word[index] > currentNode.Value)
54+
{
55+
if (currentNode.GetRightChild == null)
56+
InsertInTree(currentNode.AddRightChild(word[index], word.Length == index + 1), word, ref index);
57+
58+
return currentNode.GetRightChild;
59+
}
60+
//Left Branch
61+
else
62+
{
63+
if (currentNode.GetLeftChild == null)
64+
InsertInTree(currentNode.AddLeftChild(word[index], word.Length == index + 1), word, ref index);
65+
66+
return currentNode.GetLeftChild;
67+
}
68+
}
69+
70+
void InsertInTree(TernaryTreeNode currentNode, string word, ref int currentIndex)
71+
{
72+
int length = word.Length;
73+
74+
currentIndex++;
75+
var currNode = currentNode;
76+
for (int i = currentIndex; i < length; i++)
77+
currNode = currNode.AddMiddleChild(word[i], word.Length == currentIndex + 1);
78+
79+
currentIndex = length;
80+
}
81+
}
82+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace DataStructures.Trees
6+
{
7+
public class TernaryTreeNode
8+
{
9+
public virtual TernaryTreeNode Parent { get; private set; }
10+
protected virtual TernaryTreeNode[] childs { get; set; }
11+
12+
public virtual TernaryTreeNode GetLeftChild { get { return childs[0]; } }
13+
public virtual TernaryTreeNode GetMiddleChild { get { return childs[1]; } }
14+
public virtual TernaryTreeNode GetRightChild { get { return childs[2]; } }
15+
16+
public virtual bool FinalLetter { get; set; }
17+
public virtual char Value { get; set; }
18+
19+
public TernaryTreeNode(TernaryTreeNode parent)
20+
{
21+
this.Parent = parent;
22+
childs = new TernaryTreeNode[3];
23+
}
24+
25+
public TernaryTreeNode(TernaryTreeNode parent, char value, bool isFinal)
26+
{
27+
this.Parent = parent;
28+
this.Value = value;
29+
this.FinalLetter = isFinal;
30+
childs = new TernaryTreeNode[3];
31+
}
32+
33+
public virtual TernaryTreeNode AddLeftChild(char value, bool isFinal)
34+
{
35+
childs[0] = new TernaryTreeNode(this, value, isFinal);
36+
return childs[0];
37+
}
38+
public virtual TernaryTreeNode AddRightChild(char value, bool isFinal)
39+
{
40+
childs[2] = new TernaryTreeNode(this, value, isFinal);
41+
return childs[2];
42+
}
43+
public virtual TernaryTreeNode AddMiddleChild(char value, bool isFinal)
44+
{
45+
childs[1] = new TernaryTreeNode(this, value, isFinal);
46+
return childs[1];
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)