@@ -72,6 +72,7 @@ public bool Contains(string element)
7272 {
7373 continue ;
7474 }
75+
7576 return false ;
7677 }
7778
@@ -107,18 +108,9 @@ public int HowManyStartWithPrefix(string prefix)
107108 }
108109
109110 // Function for clearing dictionaries
110- static private void ClearDictionaryAndNode ( string element , int index , Node ? node )
111+ static private void ClearDictionaryAndNode ( Node node )
111112 {
112- if ( index + 1 < element . Length - 1 && node != null )
113- {
114- node = node . Nodes [ element [ index + 1 ] ] ;
115- ClearDictionaryAndNode ( element , index + 1 , node ) ;
116- }
117-
118- if ( node != null )
119- {
120- node . Nodes . Clear ( ) ;
121- }
113+ node . Nodes . Clear ( ) ;
122114 }
123115
124116 /// <summary>
@@ -134,22 +126,44 @@ public bool Remove(string element)
134126 }
135127
136128 int index = 0 ;
137- Node ? node = root ;
129+ Node node = root ;
130+ Node ? anotherNode = null ;
138131 for ( int i = 0 ; i < element . Length ; i ++ )
139132 {
140133 if ( node != null )
141134 {
142135 node = node . Nodes [ element [ i ] ] ;
136+
137+ if ( node . Nodes . Count == 0 && i == element . Length - 1 )
138+ {
139+ if ( anotherNode != null )
140+ {
141+ ClearDictionaryAndNode ( anotherNode ) ;
142+ Size -= i - index ;
143+ return true ;
144+ }
145+ else
146+ {
147+ ClearDictionaryAndNode ( root ) ;
148+ Size = 0 ;
149+ return true ;
150+ }
151+ }
152+
153+ if ( node . Nodes . Count != 0 && i == element . Length - 1 )
154+ {
155+ node . IsTerminal = false ;
156+ return true ;
157+ }
143158 }
144159
145- string subString = element [ 0 ..( i + 1 ) ] ;
146- if ( HowManyStartWithPrefix ( subString ) < 2 )
160+ if ( node != null && node . IsTerminal )
147161 {
148162 index = i ;
149- break ;
163+ anotherNode = node ;
150164 }
165+
151166 }
152- ClearDictionaryAndNode ( element , index , node ) ;
153167 return true ;
154168 }
155169}
0 commit comments