Skip to content

Commit

Permalink
Add trie data structure and tests (#30)
Browse files Browse the repository at this point in the history
* Add trie data structure and tests

* updating DIRECTORY.md

* Remove mutable from some tests

---------

Co-authored-by: mahdihasnat <[email protected]>
  • Loading branch information
mahdihasnat and mahdihasnat authored Nov 25, 2024
1 parent d0c57ac commit b478a6a
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 12 deletions.
8 changes: 2 additions & 6 deletions Algorithms.Tests/Algorithms.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>latest</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>

<ItemGroup>
<Compile Include="Math/*.fs" />
<Compile Include="Search/*.fs" />
<Compile Include="Sort/*.fs" />
<Compile Include="Strings/*.fs" />
<Compile Include="DataStructures/*.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
Expand All @@ -25,9 +23,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Algorithms\Algorithms.fsproj" />
</ItemGroup>

</Project>
</Project>
62 changes: 62 additions & 0 deletions Algorithms.Tests/DataStructures/Trie.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
namespace Algorithms.Tests.DataStructures

open Microsoft.VisualStudio.TestTools.UnitTesting
open Algorithms.DataStructures.Trie

[<TestClass>]
type TrieTests () =

[<TestMethod>]
member this.``Test insertion and retrieval with strings``() =
let mutable trie = empty

trie <- insert "foo" trie
Assert.IsTrue(search "foo" trie)

trie <- insert "foobar" trie
Assert.IsTrue(search "foobar" trie)
Assert.IsTrue(search "foo" trie)

trie <- insert "bar" trie
Assert.IsTrue(search "bar" trie)
Assert.IsFalse(search "baz" trie)
Assert.IsFalse(search "foobarbaz" trie)

[<TestMethod>]
member this.``Test empty trie``() =
let trie = empty
Assert.IsFalse(search "foo" trie)
Assert.IsFalse(search "" trie)

[<TestMethod>]
member this.``Test insert empty key``() =
let trie =
empty
|> insert ""

Assert.IsTrue(search "" trie)
Assert.IsFalse(search "foo" trie)

[<TestMethod>]
member this.``Test overlapping keys``() =
let trie =
empty
|> insert "car"
|> insert "cart"
|> insert "carter"

Assert.IsTrue(search "car" trie)
Assert.IsTrue(search "cart" trie)
Assert.IsTrue(search "carter" trie)
Assert.IsFalse(search "care" trie)

[<TestMethod>]
member this.``Test partial match``() =
let trie =
empty
|> insert "apple"

Assert.IsFalse(search "app" trie)
Assert.IsFalse(search "appl" trie)
Assert.IsTrue(search "apple" trie)
Assert.IsFalse(search "applepie" trie)
8 changes: 2 additions & 6 deletions Algorithms/Algorithms.fsproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>latest</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Include="Math/*.fs" />
<Compile Include="Search/*.fs" />
<Compile Include="Sort/*.fs" />
<Compile Include="Strings/*.fs" />
<Compile Include="DataStructures/*.fs" />
</ItemGroup>

</Project>
</Project>
37 changes: 37 additions & 0 deletions Algorithms/DataStructures/Trie.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace Algorithms.DataStructures

module Trie =

type Trie = {
IsWord : bool
Children : Map<char, Trie>
}

let empty : Trie = { IsWord = false; Children = Map.empty }

let insert (word: string) (trie: Trie) : Trie =
let rec insertImpl (chars: char list) (trie: Trie) : Trie =
match chars with
| [] ->
{ trie with IsWord = true }
| c :: rest ->
match trie.Children.TryFind c with
| Some child ->
let child = insertImpl rest child
{ trie with Children = trie.Children.Add(c, child) }
| None ->
let child = insertImpl rest empty
{ trie with Children = trie.Children.Add(c, child) }

insertImpl (word |> Seq.toList) trie

let search (word: string) (trie: Trie) : bool =
let rec searchImpl (chars: char list) (trie: Trie) : bool =
match chars with
| [] -> trie.IsWord
| c :: rest ->
match trie.Children.TryFind c with
| Some child -> searchImpl rest child
| None -> false
searchImpl (word |> Seq.toList) trie

4 changes: 4 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# List of all files

## Algorithms.Tests
* Datastructures
* [Trie](https://github.com/TheAlgorithms/F-Sharp/blob/main/Algorithms.Tests/DataStructures/Trie.fs)
* Math
* [Absmaxtests](https://github.com/TheAlgorithms/F-Sharp/blob/main/Algorithms.Tests/Math/AbsMaxTests.fs)
* [Absmintests](https://github.com/TheAlgorithms/F-Sharp/blob/main/Algorithms.Tests/Math/AbsMinTests.fs)
Expand Down Expand Up @@ -37,6 +39,8 @@
* [Zfunctiontests](https://github.com/TheAlgorithms/F-Sharp/blob/main/Algorithms.Tests/Strings/ZFunctionTests.fs)

## Algorithms
* Datastructures
* [Trie](https://github.com/TheAlgorithms/F-Sharp/blob/main/Algorithms/DataStructures/Trie.fs)
* Math
* [Abs](https://github.com/TheAlgorithms/F-Sharp/blob/main/Algorithms/Math/Abs.fs)
* [Absmax](https://github.com/TheAlgorithms/F-Sharp/blob/main/Algorithms/Math/AbsMax.fs)
Expand Down

0 comments on commit b478a6a

Please sign in to comment.