I have a binary tree class that is created with a root node and nodes can be added to it as needed in the code, however I am having trouble in deleting the nodes because I point to them with TNodePtr and it is an incompatible type with TNode. At the moment I have this recursive method of deleing nodes which should work once the incompatible types is sorted. Thanks.
Destructor TTree.Destroy;
procedure FreeSubnodes(Node: TNodePtr);
begin
if Assigned(Node.Left) then
FreeSubnodes(Node.Left);
if Assigned(Node.Right) then
FreeSubnodes(Node.Right);
Delete(Node);
end;
begin
FreeSubnodes(Root);
inherited;
end;
Edit 04/03/2010: The error given is this: [Warning] SystemBuild.pas(50): Method 'Destroy' hides virtual method of base type 'TObject' [Error] SystemBuild.pas(84): Incompatible types
Line 84 is Delete(Node);
I declared the node like this:
type
TNodePtr = ^TNode;
TNode = Record
Data:String;
Left:TNodePtr;
Right:TNodePtr;
end;
And the tree like this:
Type
TTree = Class
Private
Root:TNodePtr;
Public
Function GetRoot:TNodePtr;
Constructor Create;
Destructor Destroy;
end;