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

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

描述:

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){

	}
}
相关推荐
snowfoootball12 分钟前
最短路问题
数据结构·算法
匆匆整棹还42 分钟前
关于tomcat乱码和idea中控制台乱码的问题
java·tomcat·intellij-idea
有你的冬天1981 小时前
数据结构(一)
数据结构·算法
何似在人间5751 小时前
SpringAI+DeepSeek大模型应用开发——1 AI概述
java·人工智能·spring·springai
满怀10151 小时前
【Python进阶】列表:全面解析与实战指南
python·算法
匹马夕阳2 小时前
Java开发中的设计模式之观察者模式详细讲解
java·观察者模式·设计模式
风铃儿~2 小时前
Java微服务注册中心深度解析:环境隔离、分级模型与Eureka/Nacos对比
java·分布式·微服务·面试
爱学习的uu2 小时前
决策树:ID3,C4.5,CART树总结
算法·决策树·机器学习
赤橙红的黄2 小时前
Spring Boot中接入DeepSeek的流式输出
java·服务器·javascript
小样vvv2 小时前
【AI】IDEA 集成 AI 工具的背景与意义
java·人工智能·intellij-idea