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

	}
}
相关推荐
我送炭你添花21 小时前
Pelco KBD300A 模拟器:05.校验算法终极对比 + 完整 100+ 指令封装 + KBD300A 所有隐藏功能函数化
python·算法·自动化·运维开发
计算机学姐21 小时前
基于SSM的网上花店销售系统【2026最新】
java·vue.js·mysql·java-ee·tomcat·intellij-idea·mybatis
DuHz21 小时前
汽车FMCW雷达互扰下的快速目标检测:谱峰累积法与泊松CFAR精读与推导
论文阅读·算法·目标检测·汽车·信息与通信·信号处理
2401_8370885021 小时前
算法边界情况处理套路总结
算法
.墨迹.21 小时前
汇总笔试题
java
悟空码字21 小时前
Java短信验证码保卫战,当羊毛党遇上“铁公鸡”
java·后端
爱吃KFC的大肥羊21 小时前
Redis 基础完全指南:从全局命令到五大数据结构
java·开发语言·数据库·c++·redis·后端
用户21903265273521 小时前
Spring Boot4.0整合RabbitMQ死信队列详解
java·后端
uup21 小时前
Redis 缓存雪崩
java·redis
独自归家的兔21 小时前
大模型通义千问3-VL-Plus - QVQ 视觉推理模型
java·人工智能·intellij-idea