Solved

Data Structure problem

Posted on 2003-10-28
5
155 Views
Last Modified: 2010-04-05
{
 
I am trying to implement a data structure like this:
 
firstNode
    |
|---|---|
c1  c2  c3
 
firstNode has 3 children...
 
Each node has a value stored... when pressing a button it would show
the values with ShowMessage.
 
THE PROBLEM: the value of firstNode (the root) is OK, but when I try
to show each children value, it shows garbage (nonsense numbers)...
 
don't know what is wrong...
 
}
 
unit uTest;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
 
  TSomethingPtr = ^TSomething;
  TSomething = class(TObject)
  protected
    value: integer;
    children: array[1..3] of TSomethingPtr;
  public
    constructor Create(num: integer); overload;
  end;
 
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    firstNode: TSomething;
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
{--------------------------------------------}
 
constructor TSomething.Create(num: integer);
begin
  inherited Create;
  value := num;
end;
 
{--------------------------------------------}
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  firstNode := TSomething.Create(0);
  firstNode.children[1] := nil;
  firstNode.children[2] := nil;
  firstNode.children[3] := nil;
end;
 
{--------------------------------------------}
 
{generates 3 children nodes, each one with a different value and add
its address to the firstNode array}
 
procedure TForm1.Button1Click(Sender: TObject);
var newNode: TSomeThing;
begin
  newNode := TSomeThing.Create(1);
  firstNode.children[1] := @newNode;
 
  newNode := TSomeThing.Create(2);
  firstNode.children[2] := @newNode;
 
  newNode := TSomeThing.Create(3);
  firstNode.children[3] := @newNode;
end;
 
{--------------------------------------------}
 
{shows the value stored in firstNode, and the values stores in each children}
 
procedure TForm1.Button2Click(Sender: TObject);
var nodePtr: TSomeThingPtr; v: integer;
begin
  ShowMessage('first node = ' + IntToStr(firstNode.value));
 
  nodePtr := firstNode.children[1];
  v := nodePtr^.value;
  ShowMessage('first child = ' + IntToStr(v));
 
  nodePtr := firstNode.children[2];
  v := nodePtr^.value;
  ShowMessage('second child = ' + IntToStr(v));
 
  nodePtr := firstNode.children[3];
  v := nodePtr^.value;
  ShowMessage('third child = ' + IntToStr(v));
end;
 
{--------------------------------------------}
 
end.
0
Comment
Question by:LeoHyppolito
  • 2
  • 2
5 Comments
 

Expert Comment

by:mertero
ID: 9639779
In Button1click (when you create your node), do like this -

new(firstNode.children[1])

instead of the "Create" method.


Then you can do -

firstNode.chlidren[1]^.value = 1; // or whatever

R.

0
 

Expert Comment

by:mertero
ID: 9639781
BTW - you can get a good example on trees here -

http://www.awitness.org/delphi_pascal_tutorial/source/insert_binary_tree.html

M.
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 50 total points
ID: 9639919
try this

procedure TForm1.Button1Click(Sender: TObject);
var newNode: TSomeThing;
begin
  newNode := TSomeThing.Create(1);
  firstNode.children[1] := newNode;  //no @
 
  newNode := TSomeThing.Create(2);
  firstNode.children[2] := newNode;
 
  newNode := TSomeThing.Create(3);
  firstNode.children[3] := newNode;
end;
 

procedure TForm1.Button2Click(Sender: TObject);
var nodePtr: TSomeThingPtr; v: integer;
begin
  ShowMessage('first node = ' + IntToStr(firstNode.value));
 
  nodePtr := firstNode.children[1];
  v := nodePtr.value;    //no ^
  ShowMessage('first child = ' + IntToStr(v));
 
  nodePtr := firstNode.children[2];
  v := nodePtr.value;
  ShowMessage('second child = ' + IntToStr(v));
 
  nodePtr := firstNode.children[3];
  v := nodePtr.value;
  ShowMessage('third child = ' + IntToStr(v));
end;
 
meikl ;-)

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9639922
btw.

replace this
children: array[1..3] of TSomethingPtr;
with
children: array[1..3] of TSomething;

because an object is just a pointer
-> a pointer of pointer is not needed

meikl ;-)
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 9640911
meikl, your solution seems okay but perhaps there's a reason why Leo choose to use this technique. Let's see...

The TSomething contains three pointers to an object. Since an object is actually a pointer by itself, we now have a pointer to a pointer to some data. Let's look at this:

  newNode := TSomeThing.Create(1);
  firstNode.children[1] := @newNode;

Nice, but it has a flaw. firstNode.children[1] is of type TSomeThingPtr. You are not assigning an address of an object to this pointer but you're assigning the object itself to this pointer. Instead you first have to reserve memory space for this pointer. Thus:

  New(firstNode.children[1])
  newNode := TSomeThing.Create(1);
  firstNode.children[1]^ := @newNode;

Yeah, it looks funny, I know...
Next we have this:

  nodePtr := firstNode.children[1];
  v := nodePtr^.value;
  ShowMessage('first child = ' + IntToStr(v));

Well, since nodePtr is of type TSomeThingPtr it should provide the right value. IE, a pointer to the object. The object itself is at nodePtr^ so this code should work...
Why it didn't work at first? Well, nodePtr is the address of the object. nodePtr^ is pointing to some data inside the object but Delphi thinks you're actually pointing to the object and thus has to step a bit further to access the data.

But meikl, you're right. Your solution is a lot easier. I wonder why Leo did not use it. ;-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now