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

	}
}
相关推荐
沐浴露z1 分钟前
详解Java ArrayList
java·开发语言·哈希算法
蒲公英源码1 分钟前
基于Linux+Nginx+PHP+MySQL的命理测算系统
linux·nginx·php
Juan_20122 分钟前
P2865 [USACO06NOV] Roadblocks G 题解
c++·算法·图论·题解
第二只羽毛3 分钟前
单例模式的初识
java·大数据·数据仓库·单例模式
4***g8944 分钟前
Java进阶-SpringCloud设计模式-工厂模式的设计与详解
java·spring cloud·设计模式
__万波__4 分钟前
二十三种设计模式(五)--建造者模式
java·设计模式·建造者模式
北郭guo5 分钟前
Java设计模式 【理论+代码实现】 让你从小白到大佬的蜕变
java·开发语言·设计模式
计算机徐师兄5 分钟前
Java基于微信小程序的贝壳活动助手【附源码、文档说明】
java·微信小程序·贝壳活动助手·贝壳活动助手小程序·贝壳活动助手微信小程序·java贝壳活动助手小程序·java贝壳活动助手微信小程序
Gavin在路上7 分钟前
架构设计之COLA架构
java·数据库·架构
MediaTea9 分钟前
Python 库手册:gc 垃圾回收
java·开发语言·jvm·python·算法