首先我写了这张唱片:
type
PNode = ^Tree;
Tree = record
key : Integer;
left,right : PNode;
end;
function TForm1.TreeInit(key: Integer): PNode;
var
Head : PNode;
begin
Head := nil;
New(Head);
Head.key := key;
Head.right := nil;
Head.left := nil;
Result := Head;
end;
一切都很好。然后我将 Parent 添加到 Structure :
type
PNode = ^Tree;
Tree = record
key : Integer;
left,right : PNode;
parent : PNode;
end;
现在我不知道如何以及在哪里可以初始化父级(尤其是在插入函数中)?
插入功能:
function TForm1.NodeInsert(Head: PNode; key: Integer): PNode;
begin
if Head = nil then
begin
Result := TreeInit(key);
end else
begin
if (Head.key > key) then
Head.left := NodeInsert(Head.left, key)
else
Head.right := NodeInsert(Head.right,key);
Result := Head;
end;
end;