Главная страница
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.035 c
1-55580
Новенький
2003-07-02 22:38
2003.07.17
Ссылка


14-55744
vopros
2003-06-30 11:49
2003.07.17
Какую карту видео лучше купитб до 90$


3-55478
DmitryZ
2003-06-25 16:10
2003.07.17
Проблема работы с MySQL через dbExpress в Delphi


3-55410
Mechanikus
2003-06-23 01:33
2003.07.17
Обращение к записям через DBGrid


14-55780
Maxim Pshevlotsky
2003-06-30 05:45
2003.07.17
Зачем же нужны эти COM+ технологии для баз данных?