?
Solved

Data Structure problem

Posted on 2003-10-28
5
Medium Priority
?
163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

770 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