Главная страница
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.018 c
4-55887
DmitryDDR
2003-05-15 07:40
2003.07.17
CreateProcessWithLogonW


6-55694
Python
2003-05-13 17:11
2003.07.17
Какой протокол использовать?


14-55825
Andryk
2003-06-30 14:38
2003.07.17
Что такое красота?


4-55890
Stas
2003-05-15 15:56
2003.07.17
CreateMutex


14-55831
Prov
2003-07-01 16:41
2003.07.17
Что с рассылкой ?