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 deleting 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;
The error you see is because of a mistake I made in the answer I gave to your previous question. I wrote
Deletewhen I should have writtenDispose. I apologize. Here, then, is the correct destructor implementation:The error is not saying that
TNodePtris incompatible withTNode. The error just says “incompatible types” because the compiler doesn’t know what typeDeleteis supposed to receive. It’s a compiler-magic function that accepts several different parameter types, none of which is compatible withTNodePtr.The first compiler message you see is about your
Destroymethod hiding the method from the base class. It’s not responsible for the problem you were seeing, but you’d eventually notice a problem because your destructor would never get called. When you callFree, it will call TObject’sDestroymethod, not your new version. To make yours get called instead, you need to mark yours as overriding the inherited version. Then, whenTObject.FreecallsDestroy, control will go to your version first, and then go to the base class’s version when you callinherited. Change the declaration to this: