我在 TypeScript 中遇到树数据结构问题。当我执行
updateInputArray(chatTree);
函数时,我在 totalArray.push(iteratorNode.data);
行收到“无效数组长度”错误。此外,当我尝试登录 returnStringArray()
函数时,浏览器会冻结,这表明存在潜在的无限循环。
经 printTree 函数验证,树最初的结构似乎正确。然而,当通过优先级节点遍历树时,就会出现问题。
树创建功能:
async function addChatData() {
if (Params && chatId) {
try {
const messages = (await fetchChatData({
userId: 'not important',
chatId: chatId,
getAll: false,
})) as string[];
//console.log(messages);
let i: number = 0;
let k: number = 0;
const temp: TreeNode[] = [];
for (const message of messages) {
i = 0;
try {
const versions = (await fetchChatData({
userId: 'not important',
chatId: 'not important',
messageId: message,
getAll: false,
})) as string[];
const messageNode = new TreeNode(' ', message);
chatTree.children.push(messageNode);
for (const version of versions) {
try {
const versionOfMessage = (await fetchChatData({
userId: 'not important',
chatId: 'not important',
messageId: 'not important',
versionOfMessageId: version,
getAll: true,
})) as VersionOfMessage;
//console.log('Version data: ', versionOfMessage);
const data: string = versionOfMessage.content;
const newNode = new TreeNode(data, message);
// Add the new node as a child to the message node
messageNode.addChild(newNode);
if (i === 0) temp.push(newNode); //store priority child of node
i++;
} catch (error) {
console.error(
'Could not get version of message object ',
error,
);
}
}
//messageNode.priorityChild = 0;
if (k > 0) {
//add message node to previous child to let all priority children be accessed via the returnStringArray fcn.
temp[k - 1].addChild(chatTree.children[k]);
//temp[k - 1].priorityChild = 0;
//console.log(temp);
}
k++;
} catch (error) {
console.error('Could not get version list: ', error);
}
}
} catch (error) {
console.error('Could not get message list: ', error);
}
}
updateInputArray(chatTree);
//console.log(chatTree.printTree());
}
updateInputArray函数的主要功能:
returnStringArray(): string[] {
let iteratorNode: TreeNode = this.children[this.priorityChild];
const totalArray: string[] = [];
while (iteratorNode) {
// If the node's data is not just a space, add it to the totalArray (i.e., skip messageNode data).
//console.log(iteratorNode);
if (iteratorNode.data.trim() !== '') {
totalArray.push(iteratorNode.data);
}
// Proceed to the priority child if it exists, or break the loop
if (iteratorNode.children.length > 0) {
iteratorNode = iteratorNode.children[iteratorNode.priorityChild];
} else {
break; // No more children, so we break the loop
}
}
return totalArray;
}
我完全不知道问题是什么,因此我们将不胜感激任何帮助或未来的调试步骤。
如果您有没有版本的消息,那么您的代码将在您的“树”中创建循环:
想象一个产生循环的最小示例:
在外循环的第一次迭代之后,
chatTree
有一个子节点,即节点“A”,并且temp
为空(因为没有“版本”)。 k
设置为 1。
在外循环的第二次迭代中,就在内循环完成之后,
chatTree
有两个子级(一个用于“A”,一个用于“B”),并且 temp
有第一个(也是唯一的)“ “B”的版本”节点。
现在
k
大于零,建立以下链接:
从
temp
中的节点(“B”的子节点)到...节点“B”!!这就形成了一个循环。
因此你的
returnStringArray
将进入“无限”循环,经过 32K 次迭代后,数组 totalArray
已达到可能的最大长度,从而导致你得到的错误。
结论:节点互连的逻辑是错误的。你必须修改它。在不知道其余代码的情况下,我只能概述可能的解决方案。我建议删除变量
i
、k
和 temp
,而使用 prevChild
,如下所示:
let prevChild = null; // TreeNode
for (const message of messages) {
// ...
const messageNode = new TreeNode(' ', message);
// ...
for (const version of versions) {
//...
}
if (messageNode.children.length) {
if (prevChild) {
prevChild.addChild(messageNode); // This sets the link
}
prevChild = messageNode.children[0];
}
}