Solved

Data Structure problem

Posted on 2003-10-28
5
161 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 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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
When I am typing into a TEdit last word repeats 7 175
Multiple image collision 13 106
Firemonkey allowing RTL on android 6 75
Delphi Firemonkey : user drawing in window 3 56
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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