Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2003.07.17;
Скачать: CL | DM;

Вниз

Бинарное дерево   Найти похожие ветки 

 
SkyRanger ©   (2003-07-04 08:35) [0]

Пытаюсь тут разобраться с бинарными деревьям и не могу понять в чем проблема...

Вот код:


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
RT: TRichEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

node=record
name: string;
left, right : pointer;
end;

var
Form1: TForm1;
pnt,current:^node;
pnt_s,current_s,root: pointer;
i:integer;
s:string;

implementation
{$R *.dfm}

procedure node_list (pnt_s:pointer);
var
pnt_n:^node;
begin

pnt_n:=pnt_s;

Form1.RT.Lines.Add(pnt_n^.name);

if (pnt_n^.left <> nil) then node_list (pnt_n^.left);
if (pnt_n^.right <> nil) then node_list (pnt_n^.right);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
s:string;
i:byte;
begin

new(current);
root:=current;
current^.name:="root";
current^.left:=nil;
current^.right:=nil;
Form1.RT.Lines.Add(current^.name);

s:=" ";

for i:=1 to 3 do
Begin

if current^.left= nil then
new(pnt)
else
pnt:= current^.left;

pnt^.name:="Node #"+IntToStr(i);
Form1.RT.Lines.Add(s+"+-"+pnt^.name);
s:=s+"| ";

pnt^.left := nil;
pnt^.right:= nil;

current^.left:= pnt;

End;

Form1.RT.Lines.Add("---------");


node_list(root);

end;


end.


Результат такой:
root
+-Node #1
| +-Node #2
| | +-Node #3
---------
root
Node #3

Хотя по идее должен быть таким:

root
+-Node #1
| +-Node #2
| | +-Node #3
---------
root
Node #1
Node #2
Node #3


Пример я брал из статьи одной... Там он просто на паскале, но основу я сохранил!


 
icWasya ©   (2003-07-04 11:39) [1]

........
for i:=1 to 3 do
Begin

new(pnt);

pnt^.name:="Node #"+IntToStr(i);
Form1.RT.Lines.Add(s+"+-"+pnt^.name);
s:=s+"| ";

pnt^.left := nil;
pnt^.right:= nil;

if current^.left<> nil then current:=current^.leftnew

current^.left:= pnt;

End;

.......


 
SkyRanger ©   (2003-07-07 04:53) [2]

Спасибо ЗАРАБОТАЛО! :)



Страницы: 1 вся ветка

Текущий архив: 2003.07.17;
Скачать: CL | DM;

Наверх




Память: 0.47 MB
Время: 0.022 c
1-55571
Franzy
2003-07-02 17:16
2003.07.17
Run-time Resizeble components


14-55764
Nick-From
2003-07-03 12:17
2003.07.17
При попытке сохранить страницу из инета, появляется синее окно


6-55695
fyty
2003-05-10 21:43
2003.07.17
Terminal


1-55599
tm
2003-07-03 12:40
2003.07.17
Исчезает из формы StatusBar, при разв. окна и обрат. опер.


3-55421
DBDev
2003-06-24 10:53
2003.07.17
Коллеги, подскажите с CurrencyFormat-ом плиз.