如何使用此代码为trie结构检索每个keyup函数中的值?

时间:2018-02-13 06:01:25

标签: javascript trie patricia-trie

如何使用此功能的keyup函数来检索值? 如果我按“m”,m的所有值都将显示,如果按s,则“s”的所有值将显示如下。感谢

<body>
<div style="text-align: center;" class="first_div" id="div_id">
    <div><p style="color:blue;"> Baby Names</p></div>
    <div><p style="color:blue;"> HOW UNIQUE IS YOUR CHILD NAMES IS ?</p>
</div>
    <label style="color: blue">Enter Full Name : </label>
    <input type="text" id="search_id" class="input_class">
    <input type="button" class="btn_class" id="btn_id" value="search">
    </br>
    <div style="color: #246eca;margin-left: 548px; font-weight: 5px; width: 300px; height: 450px; border: 1px solid" id="result"></div>
</div>
</body>
<script>
var nodes = ["maria", "mary", "marks", "michael", "seven", "sefon", "sky", "six", "tree", "ten", "team", "tee", "new", "next", "never", "neem"];
var tree = [];
function insertWord(tree, string) {
    var keys = Object.keys(tree),
        result = keys.some(function (k) {
            var i = 0;

            if (string.startsWith(k)) {
                string.slice(k.length);
                return true;
            }
            while (k[i] == string[i] && i < k.length) {
                i++;
            }

            if (i) {
                tree[k.slice(0, i)] = {[k.slice(i)]: tree[k], [string.slice(i)]: 0};
                delete tree[k];
                return true;
            }

        });

    if (!result) {
        tree[string] = 0;
    }
}
nodes.forEach(insertWord.bind(null, tree));
console.log(tree);
</script>

我必须使用keyup函数来完成此操作。之后我必须从数据库中检索值并保存在localstorage中以供进一步使用。

0 个答案:

没有答案