Главная страница
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.012 c
3-55446
merlot
2003-06-24 14:58
2003.07.17
Запрос с пользовательскими параметрами...


7-55866
Cooller
2003-05-07 14:27
2003.07.17
Имя дисковода


3-55432
RDA
2003-06-22 11:47
2003.07.17
Не обновляется набор данных


3-55464
RH
2003-06-25 13:02
2003.07.17
Работа с dbf-ками


14-55838
Skier
2003-06-30 09:05
2003.07.17
Иерархия классов Delphi