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

	}
}
相关推荐
nono牛1 分钟前
实战项目:设计一个智能温控服务
android·前端·网络·算法
H_BB4 分钟前
LRU缓存
数据结构·c++·算法·缓存
BF06246 分钟前
EPICS Archiver Appliance部署Quickstart方式
java·tomcat
JaguarJack7 分钟前
PHP True Async 最近进展以及背后的争议
后端·php
历程里程碑2 小时前
LeetCode热题11:盛水容器双指针妙解
c语言·数据结构·c++·经验分享·算法·leetcode·职场和发展
想不明白的过度思考者2 小时前
Spring Boot 配置文件深度解析
java·spring boot·后端
第二只羽毛2 小时前
Java图书管理系统的设计与实现
java·大数据·安全·系统安全
cike_y7 小时前
Mybatis之解析配置优化
java·开发语言·tomcat·mybatis·安全开发
是一个Bug8 小时前
Java基础50道经典面试题(四)
java·windows·python
Slow菜鸟9 小时前
Java基础架构设计(三)| 通用响应与异常处理(分布式应用通用方案)
java·开发语言