I did not apply today for any reason other than fun, currently it supports (and) search () Remove () should also be applied but I think it is quite straight forward.
It is completely functional, but it also takes a little bit to fill the tray with the data. I'm using this list as a data source for a lot of my taste: (somewhere on SO And found) ~ 11s to load it was my initial implementation ~ 15s so I have already promoted this good performance, but I am still not satisfied :)
My question It is that I ) What to boost performance and what could? I am not bound by this design, a complete overhaul is acceptable.
class tri $ {personal $ trie; Public Function __ Composition (Trianode $ Tri = Faucet) {If ($ Tri! == empty) $ this-> Trie = $ trie; Else $ this-> Tri = new triode (); $ This- & gt; Counter = 0; } Add Public Functions ($ value, $ val = null) {$ str = ''; $ Trie_ref = $ this- & gt; Trio; Foreign currency (str_split ($ value) as $ $) {$ str = $ char; $ Trie_ref = $ trie_ref- & gt; Ad-node ($ str); } $ Trie_ref- & gt; Value = $ val; Back true; } Public function search ($ value, $ only_words = false) {If ($ value === '') $ $ - $ return return; $ Trie_ref = $ this- & gt; Trio; $ Str = ''; Foreign currency (str_split ($ value) as $ $) {$ str = $ char; If ($ trie_ref = $ trie_ref-> getNode ($ str)) {if ($ str === $ value) returns ($ only_words? $ This-> extractwords ($ trie_ref): new self ($ trie_ref )); to continue; } return false; } return false; } Extract public function word (trionode $ tray) {$ res = array (); Foreign currency ($ trie-> child;) as child $) {if ($ child-> value! == zero) $ res [] = $ child-> Values; If ($ child- & gt; children are child) $ res = array_merge ($ res, $ this-> extractwords ($ child)); } $ Res; }} Class TrieNode {public $ value; Protected $ children = array (); Public function addNode ($ index) {if (isset ($ this-> children [$ index])) $ $ - this-> children [$ index]; Return $ this-> children [$ index] = new self (); } Public event getNode ($ index) {return (isset ($ this-> child [$ index])? $ This- & gt; children [$ index]: incorrect); } Public function getChildren () {return $ this-> children; } Children at the public ceremony () {Count of return ($ this-> children) & gt; 0; }}
do not know php but,
the following methods :
add public function ($ value, $ val = null) {$ str = ''; $ Trie_ref = $ this- & gt; Trio; Foreign currency (str_split ($ value) as $ $) {$ str = $ char; $ Trie_ref = $ trie_ref- & gt; Ad-node ($ str); } $ Trie_ref- & gt; Value = $ val; Back true; } Public function search ($ value, $ only_words = false) {If ($ value === '') $ $ - $ return return; $ Trie_ref = $ this- & gt; Trio; $ Str = ''; Foreign currency (str_split ($ value) as $ $) {$ str = $ char; If ($ trie_ref = $ trie_ref-> getNode ($ str)) {if ($ str === $ value) returns ($ only_words? $ This-> extractwords ($ trie_ref): new self ($ trie_ref )); to continue; } return false; } return false; } Why do you $ str? = $ Char is required (which I think is attached)? It changes the omega (n ^ 2) (n length of $ value ) in the time of your O (n) instead of o (n) instead of o (n).
Try, you usually walk on the string when you walk on TRA, that means you get the next node based on the current character rather than the existing prefix.
Comments
Post a Comment