[英]what is the easy way to make suffix from this js code?
Note the code below shows the array in the console, not in the snippet output 请注意,下面的代码在控制台中而不是在代码段输出中显示了数组
var nodes = ["maria", "mary", "marks", "michael"]; function insert_word(split_nodes) { var rest = []; for (var i = 0; i < split_nodes.length; i++) { //console.log(current); var word = split_nodes[i]; var letters = word.split(""); var current = rest; console.log(current); for (var j = 0; j < letters.length; j++) { var character = letters[j]; var position = current[character]; if (position == null) { current = current[character] = j == letters.length - 1 ? 0 : {}; } else { current = current[character]; } } } } insert_word(nodes);
Above outputs this 以上输出此
M :{a : {r :{i :{a :0},
k :0,
y :
},
},
i :{c :{h :{a :{e :{l :0}}}}}}}
but I want to output this : 但我想输出这个:
M :{ar:{ia:0,
k :0,
y :0
},
ichael :0
}
can anyone help me to find out this output from my code? 谁能帮助我从我的代码中找到此输出? how could i make suffeix from this code?
我怎样才能从此代码后缀?
This solution take a sightly changed object structure for the end indicator with a property isWord
, because the original structure does not reflect entries like 'marc'
and 'marcus'
, because if only 'marc'
is uses, a zero at the end of the tree denotes the end of the word, but it does not allowes to add a substring, because the property is a primitive and not an object. 该解决方案为带有属性
isWord
的结束指示符采用了isWord
改变的对象结构,因为原始结构不反映诸如'marc'
和'marcus'
类'marc'
条目,因为如果仅使用'marc'
,则末尾为零。 tree表示单词的结尾,但不允许添加子字符串,因为该属性是原始类型,而不是对象。
Basically this solution creates first a comlete tree with single letters and then joins all properties which have only one children object. 基本上,此解决方案首先创建一个具有单个字母的comlete树,然后将仅具有一个子对象的所有属性连接起来。
function join(tree) { Object.keys(tree).forEach(key => { var object = tree[key], subKeys = Object.keys(object), joinedKey = key, found = false; if (key === 'isWord') { return; } while (subKeys.length === 1 && subKeys[0] !== 'isWord') { joinedKey += subKeys[0]; object = object[subKeys[0]]; subKeys = Object.keys(object); found = true; } if (found) { delete tree[key]; tree[joinedKey] = object; } join(tree[joinedKey]); }); } var node = ["maria", "mary", "marks", "michael"], tree = {}; node.forEach(string => [...string].reduce((t, c) => t[c] = t[c] || {}, tree).isWord = true); console.log(tree); join(tree); console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }
A recursive single pass approach with a function for inserting a word into a tree which updates the nodes. 一种具有将单词插入树中以更新节点的功能的递归单遍方法。
It works by 它的工作原理
Checking the given string
with all keys of the object and if string
start with the actual key, then a recursive call with the part string and the nested part of the trie is made. 用对象的所有键检查给定的
string
,如果string
以实际键开头,则使用部分字符串和trie的嵌套部分进行递归调用。
Otherwise, it checks how many characters are the same from the key and the string. 否则,它将检查键和字符串中有多少个相同的字符。
Then it checks the counter and creates a new node with the common part and two nodes, the old node content and a new node for the string. 然后,它检查计数器,并创建一个具有公共部分和两个节点的新节点,即旧节点内容和该字符串的新节点。
Because of the new node, the old node is not more necessary and gets deleted, as well as the iteration stops by returning true
for the update
check. 由于有了新节点,因此不再需要旧节点并将其删除,并且通过为
update
检查返回true
来停止迭代。
If no update took place, a new property with string as key and zero as value is assigned. 如果未进行更新,则分配一个新属性,该属性以字符串为键,值为零。
function insertWord(tree, string) { var keys = Object.keys(tree), updated = keys.some(function (k) { var i = 0; if (string.startsWith(k)) { insertWord(tree[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 (!updated) { tree[string] = 0; } } var words = ["maria", "mary", "marks", "michael"], tree = {}; words.forEach(insertWord.bind(null, tree)); console.log(tree); insertWord(tree, 'mara'); console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.