File tree 2 files changed +39
-4
lines changed
Algorithms/DataStructures/SearchTries
AlgorithmsUnitTest/DataStrutures/SearchTries
2 files changed +39
-4
lines changed Original file line number Diff line number Diff line change 1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using Algorithms . DataStructures . Queue ;
3
4
using Algorithms . Utils ;
4
5
5
6
namespace Algorithms . DataStructures . SearchTries
@@ -112,10 +113,43 @@ public bool ContainsKey(string key)
112
113
return false ;
113
114
}
114
115
115
- public IEnumerable < string > Keys { get ; }
116
+ public IEnumerable < string > Keys
117
+ {
118
+ get
119
+ {
120
+ var queue = new QueueLinkedList < string > ( ) ;
121
+
122
+ Collect ( root , "" , queue ) ;
123
+
124
+ return queue ;
125
+ }
126
+ }
127
+
128
+ private void Collect ( Node x , string prefix , IQueue < string > queue )
129
+ {
130
+ if ( x == null )
131
+ {
132
+ return ;
133
+ }
134
+ if ( ! ObjectUtil . IsNullOrDefault ( x . value ) )
135
+ {
136
+ queue . Enqueue ( prefix ) ;
137
+ }
138
+ Collect ( x . left , prefix , queue ) ;
139
+ Collect ( x . mid , prefix + x . key , queue ) ;
140
+ Collect ( x . right , prefix , queue ) ;
141
+ }
142
+
116
143
public IEnumerable < string > KeysWithPrefix ( string prefix )
117
144
{
118
- throw new System . NotImplementedException ( ) ;
145
+ IQueue < string > queue = new QueueLinkedList < string > ( ) ;
146
+
147
+ Node x = Get ( root , prefix , 0 ) ;
148
+ if ( x != null )
149
+ {
150
+ Collect ( x , prefix , queue ) ;
151
+ }
152
+ return queue ;
119
153
}
120
154
}
121
155
}
Original file line number Diff line number Diff line change @@ -34,13 +34,14 @@ public void TestSearchTries()
34
34
Assert . False ( map . ContainsKey ( "two" ) ) ;
35
35
Assert . False ( map . IsEmpty ) ;
36
36
37
- /*
37
+
38
38
39
39
foreach ( var key in map . Keys )
40
40
{
41
41
console . WriteLine ( "{0}" , key ) ;
42
42
}
43
43
44
+
44
45
map [ "test" ] = 3 ;
45
46
map [ "te" ] = 4 ;
46
47
map [ "team" ] = 4 ;
@@ -52,7 +53,7 @@ public void TestSearchTries()
52
53
count ++ ;
53
54
console . WriteLine ( "{0}" , key ) ;
54
55
}
55
- Assert.Equal(3, count);*/
56
+ Assert . Equal ( 3 , count ) ;
56
57
}
57
58
}
58
59
}
You can’t perform that action at this time.
0 commit comments