Re:Binary Search Tree Implementations in Delphi 2.0
Quote
In article <31559FB1....@millennianet.com> Francesco <fran...@millennianet.com> writes:
>From: Francesco <fran...@millennianet.com>
>Subject: Binary Search Tree Implementations in Delphi 2.0
>Date: Sun, 24 Mar 1996 11:17:05 -0800
>To all Delphi 2.0 programmers:
>Could anyone be so kind as to post a BST implementation using Delphi
>2.0?
>Would you use New|Data Module to create one as a non-visual object?
>Does the following look like it will work?
> type
> PNode = ^TNode;
> TNode = class(TObject)
> right: PNode;
> left: PNode;
> data: integer;
> end;
> BSTree = class(TObject)
> private
> size: integer;
> root: PNode;
> end;
>Thanks for any comments and code examples, in advance...
>Francesco
>fran...@millennianet.com
This applies to Delphi 1 also.
The PNode = ^TNode is redundant, as objects are actually pointers to instances
of a class. (You are proposing pointers to pointers).
The following structure is all you need:
type
TNode = class(TObject)
right : TNode;
left : TNode;
data : integer;
end;
BSTree = class(TObject)
private
size: integer;
root: TNode;
end;
But also, what is special about the root?, why not just:
type
TNode = class(TObject)
right : TNode;
left : TNode;
data : integer;
end;
var
MyTree : TNode;
and then MyTree IS the root, in fact any node is the root of its sub-tree.
Robert Fletcher.