-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add trie data structure and tests (#30)
* Add trie data structure and tests * updating DIRECTORY.md * Remove mutable from some tests --------- Co-authored-by: mahdihasnat <[email protected]>
- Loading branch information
1 parent
d0c57ac
commit b478a6a
Showing
5 changed files
with
107 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters