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

	}
}
相关推荐
你怎么知道我是队长1 分钟前
C语言---排序算法9---堆排序法
c语言·算法·排序算法
金銀銅鐵1 分钟前
浅解 Junit 4 第四篇:类上的 @Ignore 注解
java·junit·单元测试
若水不如远方2 分钟前
分布式一致性原理(四):工程化共识 —— Raft 算法
分布式·后端·算法
小亮✿2 分钟前
算法—并查集
数据结构·c++·算法
西门吹雪分身4 分钟前
K8S之Pod生命周期
java·kubernetes·k8s
hrhcode5 分钟前
【Netty】一.Netty架构设计与Reactor线程模型深度解析
java·spring boot·后端·spring·netty
流云鹤10 分钟前
2026牛客寒假算法基础集训营2(A B I F E H)
算法
亓才孓12 分钟前
[Spring MVC]BindingResult
java·spring·mvc
Lun3866buzha14 分钟前
紧固件智能检测与分类_ATSS_R101_FPN_1x_COCO算法解析与Pytorch实现
pytorch·算法·分类
MSTcheng.16 分钟前
【Leetcode二分查找】『在排序数组中查找元素的第一个和最后一个位置&搜索插入位置』
算法·leetcode·职场和发展