//字典树和链表的区别,是node.next指向一个node 或 node[]的区别
function Node(){
this.next=[];
this.value= null;
}
function TrieST(){
this.root = new Node();
this.put = (key,value)=>{
_put(this.root, key ,value,0);
}
this.get = (key)=>{
let res = _get(this.root, key, 0);
if(res === null || res === undefined){
return null;}
return res.value;
}
this.contains = (key)=>{
let res = this.get(key);
if(res !== null && res !== undefined ){
return true;
}
return false;
}
function _put (x,key,val,i) {
//如果不存在,初始化
if(x === null || x === undefined){
x = new Node();
}
//如果找到了,无论之前存在与否,更新它储存的值
if(i== key.length){
x.value = val;
return x;
}
//当前字符
let c = key.charAt(i);
//下一个字符
//设定在X.next中,key[i]的值
x.next[c] =_put(x.next[c],key,val,i+1);
//返回key[i]对应的Node到树中
return x;
}
function _get (x,key,i) {
if(x=== null || x === undefined){
return null;
}
//如果当前值是要的值,返回
if(i == key.length){
return x;
}
let c = key.charAt(i);
return _get(x.next[c],key,i+1);
}
}
let tries= new TrieST();
tries.put("heheh",10);
console.log(tries)
//...