Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Основная";
Текущий архив: 2003.07.17;
Скачать: [xml.tar.bz2];

Вниз

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

 
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;
Скачать: [xml.tar.bz2];

Наверх





Память: 0.45 MB
Время: 0.007 c
14-55818
Юрий Федоров
2003-06-30 16:05
2003.07.17
ВОПРОС: Почему курицы перешли через дорогу?


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


14-55852
MetalFan
2003-07-02 08:38
2003.07.17
клава Sven Multimedia 800 (USB)


4-55905
DVM
2003-05-14 10:33
2003.07.17
Позиционирование элементов в Virtual List View невозможно?


14-55756
Sheng
2003-07-02 20:37
2003.07.17
Кто чем пользуется?





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский