Dictionary 如何在Pascal中实现trie数据结构?
我想试试帕斯卡语。我已启动,但插入方法无法正常工作。以下是该计划的一部分:Dictionary 如何在Pascal中实现trie数据结构?,dictionary,pascal,trie,Dictionary,Pascal,Trie,我想试试帕斯卡语。我已启动,但插入方法无法正常工作。以下是该计划的一部分: type PVrchol = ^Vrchol; Vrchol = record endOfTheWord:boolean; deti:array['a'..'z'] of PVrchol; end; var trie:PVrchol; FUNCTION getChildnode(current:PVrchol;k:char):PVrchol; Begin
type PVrchol = ^Vrchol;
Vrchol = record
endOfTheWord:boolean;
deti:array['a'..'z'] of PVrchol;
end;
var trie:PVrchol;
FUNCTION getChildnode(current:PVrchol;k:char):PVrchol;
Begin
if current^.deti[k]<>nil then
getChildnode:=current^.deti[k]
else
getChildNode:=nil;
End;
PROCEDURE insertWord(word:string;var trie:PVrchol);
var i:integer;
current, childNode:PVrchol;
Begin
current:=trie;
childNode:=current;
for i:=1 to Length(word) do begin
childNode:=getChildnode(current,word[i]);
if childNode=nil then begin
new(childNode);
current^.deti[word[i]]:=childNode;
end;
current:=childNode;
end;
current^.endOfTheWord:=true;
End;
BEGIN
new(trie);
//there are some methods reading the words from input, and calling the insertWord procedure.
END.
insertWord过程总是得到一个单词,因此参数不会被忽略 问题可能是记录不会像类那样自动归零?尝试添加
fillchar(childnode,sizeof(childnode),#0);
在newchildnode之后,Specify无法更好地正常工作。它是做什么的?您期望什么?当它尝试添加第三个子节点时出现问题。在函数getChildNode的第行if current^.deti[k]nil中,指针指向错误的位置,程序停止运行。