查找树的节点

coreblood 2004-03-25 05:09:50
{*************************************************************************

查找节点的下一个可以编辑的节点

*************************************************************************}
Function FindNextNode(Node : PVirtualNode;Var VST : TVirtualStringTree) : PVirtualNode;
{*********************************************

找到某一节点下第一个可以编辑的节点

**********************************************}
Procedure FindFirstNode(Node:PVirtualNode;VST:TVirtualStringTree;Var Result : PVirtualNode);
Var
TNode : PVirtualNode;
Data : PNodeData;
begin
If Node = Nil then
Exit;
Data := VST.GetNodeData(Node);
If (Trim(Data.EditS) = 'Y') and (Node.ChildCount <= 0) then
begin
Result := Node; //>在这里我找到了第一我要的节点为为什么我的函数不 //返回?
Exit;
end;
If Node.ChildCount > 0 then
begin
TNode := Node.NextSibling;
While TNode <> Nil Do
begin
FindFirstNode(TNode,VST,Result);
TNode := TNode.NextSibling;
end;
end else
begin
TNode := Node.NextSibling;
FindFirstNode(TNode,VST,Result);
end;
end;
begin
Result := Nil;
If Node = Node.Parent.LastChild then
FindNextNode(Node.Parent,VST)
else
FindFirstNode(Node.NextSibling,VST,Result);
If Result <> Nil then
Exit;
end;
...全文
79 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
coreblood 2004-03-26
  • 打赏
  • 举报
回复
OK
谢谢 dulei115() 和 hero_yin(阿鬼)
dulei115 2004-03-26
  • 打赏
  • 举报
回复
或者不用函数,直接判断Result是否为nil
...
if Node.ChildCount > 0 then
begin
TNode := Node.FirstChild;
while TNode <> Nil do
begin
FindFirstNode(TNode,VST,Result)
if Result <> nil then
exit;
TNode := TNode.NextSibling;
end;
end
else
begin
TNode := Node.NextSibling;
FindFirstNode(TNode,VST,ResultNode);
if Result <> nil then
exit;
end;
coreblood 2004-03-25
  • 打赏
  • 举报
回复
{*******************************************************************************

查找节点的下一个可以编辑的节点

*******************************************************************************}
Function FindNextNode(Node : PVirtualNode;Var VST : TVirtualStringTree; Var ResultNode:PVirtualNode ):Boolean;
begin
Result := False;
If Node = Node.Parent.LastChild then
FindNextNode(Node.Parent,VST,ResultNode)
else
If FindFirstNode(Node.NextSibling,VST,ResultNode) then
begin
Result := True;
Exit;
end;
end;
{*********************************************

找到某一节点下第一个可以编辑的节点

**********************************************}
Function FindFirstNode(Node:PVirtualNode;VST:TVirtualStringTree; Var ResultNode:PVirtualNode ):Boolean;
Var
TNode : PVirtualNode;
Data : PNodeData;
begin
If Node = Nil then
Exit;
Data := VST.GetNodeData(Node);
If (Trim(Data.EditS) = 'Y') and (Node.ChildCount <= 0) then
begin
ResultNode := Node;
Result := True;
Exit;
end;
If Node.ChildCount > 0 then
begin
TNode := Node.FirstChild;
While TNode <> Nil Do
begin
If not FindFirstNode(TNode,VST,ResultNode) then
TNode := TNode.NextSibling
else
begin
Result := True;
Exit;
end;
end; //EndWhile
end else
begin
TNode := Node.NextSibling;
If FindFirstNode(TNode,VST,ResultNode) then
Begin
Result := True;
Exit;
end;
end;
end;
coreblood 2004-03-25
  • 打赏
  • 举报
回复
查找某个节点下面第一个可以编辑的节点已经可以调试通过了,如上
coreblood 2004-03-25
  • 打赏
  • 举报
回复
{*********************************************

找到某一节点下第一个可以编辑的节点

**********************************************}
Function FindFirstNode(Node:PVirtualNode;VST:TVirtualStringTree; Var ResultNode:PVirtualNode ):Boolean;
Var
TNode : PVirtualNode;
Data : PNodeData;
begin
If Node = Nil then
Exit;
Data := VST.GetNodeData(Node);
If (Trim(Data.EditS) = 'Y') and (Node.ChildCount <= 0) then
begin
ResultNode := Node;
Result := True;
Exit;
end;
If Node.ChildCount > 0 then
begin
TNode := Node.FirstChild;
While TNode <> Nil Do
begin
If not FindFirstNode(TNode,VST,ResultNode) then
TNode := TNode.NextSibling
else
begin
Result := True;
Exit;
end;
end; //EndWhile
end else
begin
TNode := Node.NextSibling;
If FindFirstNode(TNode,VST,ResultNode) then
Begin
Result := True;
Exit;
end;
end;
end;
hero_yin 2004-03-25
  • 打赏
  • 举报
回复
Function FindNextNodes(PNode : PVirtualNode;Var VST : TVirtualStringTree) : PVirtualNode;
Var
Node : PVirtualNode;
Data : PNodeData;
begin
Result:= Nil;
IF PNode=Nil THen Exit;
Node := PNode;
Data := VST.GetNodeData(Node);
While (Trim(Data.EditS) <> 'Y')Or(Node=PNode) Do
begin
If Node.FirstChild<>Nil Then
Begin
Node := Node.FirstChild;
Data := VST.GetNodeData(Node);
Continue;
End;

While Node.NextSibling = nil Do
Begin
If Node.Parent=Nil Then
Begin
Node:= Nil;
Break;
End;
Node := Node.Parent;
End;
Node:= Node.NextSibling;
Data := VST.GetNodeData(Node);
If (Data=Nil) Then Exit;
end;
Result:= Node;
end;

我要分!!!!!!!!
dulei115 2004-03-25
  • 打赏
  • 举报
回复
//大概改了一下,没有调试,基本就是这样了,楼主自己慢慢改吧!
function FindFirstNode(Node: PVirtualNode; VST: TVirtualStringTree; var ResultNode: PVirtualNode): Boolean;
var
TNode: PVirtualNode;
Data: PNodeData;
begin
if Node = Nil then
Exit;
Data := VST.GetNodeData(Node);
if (Trim(Data.EditS) = 'Y') and (Node.ChildCount <= 0) then
begin
ResultNode := Node;
Result := True;
Exit;
end;
if Node.ChildCount > 0 then
begin
TNode := Node.FirstChild;
while TNode <> Nil do
begin
if not FindFirstNode(TNode,VST,ResultNode) then
TNode := TNode.NextSibling
else
begin
Result := True;
exit;
end;
end;
end
else
begin
TNode := Node.NextSibling;
if FindFirstNode(TNode,VST,ResultNode) then
begin
Result := True;
exit;
end;
end;
Result := False;
end;
dulei115 2004-03-25
  • 打赏
  • 举报
回复
你应该把过程改为函数,返回值类型为Boolean
If (Trim(Data.EditS) = 'Y') and (Node.ChildCount <= 0) then
begin
Result := Node;
//这里设置函数的返回值为找到了
Exit;
end;
If Node.ChildCount > 0 then
begin
TNode := Node.FirstChild;
While TNode <> Nil Do
begin
FindFirstNode(TNode,VST,Result);
//这里判断这一个递规调用是否找到,如果找到就exit
TNode := TNode.NextSibling;
end;
end else
begin
TNode := Node.NextSibling;
FindFirstNode(TNode,VST,Result);
//这里判断这一个递规调用是否找到,如果找到就exit
end;
dulei115 2004-03-25
  • 打赏
  • 举报
回复
If Node.ChildCount > 0 then
begin
TNode := Node.FirstChild; //--> 上面这里有点问题
While TNode <> Nil Do
begin
FindFirstNode(TNode,VST,Result);//问题出在这里
//此时不管这一步找到没有都会执行下面的语句,实际的到的应该是(最后一个根节点上的)(层数最少的节点)
TNode := TNode.NextSibling;
end;
end else
begin
TNode := Node.NextSibling;
FindFirstNode(TNode,VST,Result);
end;
a
|-aa
| |-bb
|- bb
b
|-ad
| |-bb
|bb//如果找bb,找到的应该会是这个
c
cc
//规律:根节点从后往前找,c上没有,b上有,然后看b上的第一层,也是从后往前找,得到结果
coreblood 2004-03-25
  • 打赏
  • 举报
回复
{*******************************************************************************

查找节点的下一个可以编辑的节点

*******************************************************************************}
Function FindNextNode(Node : PVirtualNode;Var VST : TVirtualStringTree) : PVirtualNode;
{*********************************************

找到某一节点下第一个可以编辑的节点

**********************************************}
Procedure FindFirstNode(Node:PVirtualNode;VST:TVirtualStringTree;Var Result : PVirtualNode);
Var
TNode : PVirtualNode;
Data : PNodeData;
begin
If Node = Nil then
Exit;
Data := VST.GetNodeData(Node);
If (Trim(Data.EditS) = 'Y') and (Node.ChildCount <= 0) then
begin
Result := Node; //----->在这里我找到了第一我要的节点为为什么我的函数不返回?
Exit; //----->我想如果找到合适节点在这里就应该跳出Procedure但是....
end;
If Node.ChildCount > 0 then
begin
TNode := Node.FirstChild; //--> 上面这里有点问题
While TNode <> Nil Do
begin
FindFirstNode(TNode,VST,Result);
TNode := TNode.NextSibling;
end;
end else
begin
TNode := Node.NextSibling;
FindFirstNode(TNode,VST,Result);
end;
end;
begin
Result := Nil;
If Node = Node.Parent.LastChild then
FindNextNode(Node.Parent,VST)
else
FindFirstNode(Node.NextSibling,VST,Result);
If Result <> Nil then
Exit;
end;

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧