当前位置: 首页> 文旅> 酒店 > 60、PHP 实现 单词查找树算法

60、PHP 实现 单词查找树算法

时间:2025/7/13 9:55:21来源:https://blog.csdn.net/weixin_44010641/article/details/140944321 浏览次数:0次

题目: PHP 实现 单词查找树算法

描述:

class TrieST
{private $_root=null;/***查找单词$key* */public function search(string $key){$node=$this->_search($this->_root,$key,0);if(is_null($node)){return null;}return $node->getVal();}private function _search($node,string $key,int $keyIndex){if(is_null($node)){return null;}if(strlen($key)==$keyIndex){return $node;}$nextNodePos=$key[$keyIndex];return $this->_search($node->getNextNode($nextNodePos),$key,++$keyIndex);}/*** 添加单词* @param string key 要添加的单词*/public function put(string $key,$value){if(is_null($this->_root)){$this->_root=$this->_put($this->_root,$key,$value,0);}$this->_put($this->_root,$key,$value,0);}private function _put($node,string $key,$value,int $keyIndex){if(is_null($node)){$node=new Node();}if(strlen($key)==$keyIndex){$node->setVal($value);return $node;}$nextNodePos=$key[$keyIndex];$nextNode=$this->_put($node->getNextNode($nextNodePos),$key,$value,++$keyIndex);$node->setNextNode($nextNodePos,$nextNode);return $node;}public function keyWithPrefix(string $pre){}
}
关键字:60、PHP 实现 单词查找树算法

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: