代码语言:javascript复制
function Node(options) {
options = options || {};
this.val = options.val;
this.isCapital = null;
this.position = options.position;
}
function position(str) {
var code = str.charCodeAt(0);
if (code >= 65 && code <= 97) {
return {
isCapital: false,
position: code
};
} else if (code >= 97 && code <= 122) {
return {
isCapital: true,
position: code
};
}
}
function makeTree(arr) {
var i = 0;
var j = 0;
var root = new Node();
var currentNode = root;
while(i < arr.length) {
var str = arr[i];
while(j < str.length) {
var info = position(str[j]);
info.val = str[j];
if (!currentNode[info.position]) {
var node = new Node(info);
currentNode[info.position] = node;
}
currentNode = currentNode[info.position];
j ;
}
currentNode.isEnd= true;
currentNode = root;
i ;
j = 0;
}
return root;
}
var str = ['hello', 'hel'];
function start() {
root = makeTree(str);
}
start()