[2分木] 二分探索木にバイナリツリーをジャンプ

どのように私の非常にシンプルなん. 私たちは、単に配列に元のバイナリツリーの要素を保存するために参照し、最終的に挿入することで、木に、配列の値を挿入 1 フェリーでのバイナリツリーノード. それはなかった 1 二分探索木.

コー​​ド:

program caynhiphan;
type MyArray = array[1..1000] of integer;  {Mang lu lai cac phan tu cua cay}
type typeKey = integer;   {Kieu cua khoa}
type nodeTree = ^node;    {Dinh nghia 1 Node cay nhi phan}
        node = record
                key : typeKey;
                left, right : nodeTree;
        end;

type Tree = nodeTree;    {Dinh nghia cay nhi phan}
var T, T1 : nodeTree;    {Cac bien cay nhi phan}
a : MyArray;
n, i:integer;


function TaoNode(key : typeKey): nodeTree; {Tao 1 node chua du lieu la khoa key}
var P:nodeTree;
begin
        new(P);
        P^.key := key;    {Gan khoa}
        P^.left := nil;   {cay con trai = rong}
        P^.right := nil;  {cay con phai = rong}
        TaoNode := P;     {tra lai Node}
end;

procedure TaoCayRong(var T: Tree);  {Tao cay rong}
begin
        T := nil;
end;

procedure NhapCayNhiPhan(var T : Tree); {Nhap cay nhi phan thong thuong}
var key, keyLeft, keyRight : typeKey;
begin
        if (T = nil) then         {Neu T rong thi nhap vao goc}
        begin
                new(T);
                write('Nhap vao goc: ');
                readln(key);
                T^.key := key;
        end;
        new (T^.left);
        new (T^.right);

        write('Nhap con trai cua ', T^.key, ' : ');  readln(keyLeft);    {Nhap vao con trai cua Node}
        T^.left^.key := keyLeft;

        write('Nhap con phai cua ', T^.key, ' : ');  readln(keyRight);   {Nhap vao con phai cua Node}
        T^.right^.key := keyRight;

        if (T^.left^.key <> 0) then NhapCayNhiPhan(T^.left)   {Neu nhap vao con trai la 0 thi coi nhu rong}
        else T^.left := nil;

        if (T^.right^.key <> 0) then NhapCayNhiPhan(T^.right) {Neu nhap vao con phai 0 thi coi nhu rong}
        else T^.right := nil;
end;

procedure DuyetTruoc (T : nodeTree);           {Duyen cay theo thu tu truoc}
begin
        if T <> nil then
        begin
                write(T^.key:5);
                DuyetTruoc(T^.left);
                DuyetTruoc(T^.right);
        end;
end;

procedure DuyetGiua(T: nodeTree);
begin
        if T <> nil then
        begin
                DuyetGiua(T^.left);
                write(T^.key:5);
                DuyetGiua(T^.right);
        end;
end;

procedure ChuyenSangMang (T : nodeTree; var a : MyArray; var n : integer);  {Chuyen cay nhi phan sang mang}
begin
        if (T <> nil) then   {Neu cay chua rong thi lam}
        begin
                inc(n);
                a[n] := T^.key;   {Gan gia tri cua cay voa mang}
                ChuyenSangMang(T^.left, a, n);     {Duyen sang con trai}
                ChuyenSangMang(T^.right, a, n);    {Duyet sang con phai}
        end;
end;


procedure ThemNodeVaoCayNPTK(P: nodeTree; var T : Tree);   {Them 1 Node vao cay nhi phan tim kiem}
begin
        if T = nil then T := P    {Neu cay rong thi gan bang P}
        else
        begin
                if P^.key > T^.key then ThemNodeVaoCayNPTK(P, T^.right)      {Neu Node P > Node hien tai -> duyet phai}
                else if P^.key < T^.key then ThemNodeVaoCayNPTK(P, T^.left)  {Neu Node P < Node hien tai -> duyet trai}
                        else;
        end;
end;

procedure ChuyenSangCayNPTK(T : Tree; var T1 : Tree); {Chuyen cay nhi phan thuong sang cay nhi phan tim kiem}
var P : nodeTree;
i:integer;
begin
        ChuyenSangMang(T, a, n);  {Chuyen cay thuong sang mang}
        TaoCayRong(T1);           {Tao cay T1 rong, T1 la cay nhi phan tim kiem can tao ra}
        for i:=1 to n do          {Them lan luot cac phan tu cua mang vao cay T1}
        begin
                P := TaoNode(a[i]);   {Tao Node}
                ThemNOdeVaoCayNPTK(P, T1);  {Them Node vao cay}
        end;
end;

begin
        TaoCayRong(T);
        NhapCayNhiPhan(T);
        writeln('Duyet truoc cay nhi phan ban dau:');
        DuyetTruoc(T);
        writeln();

        ChuyenSangCayNPTK(T, T1);

        writeln('Duyet truoc cay nhi phan tim kiem:');
        DuyetTruoc(T1);
        writeln();

        writeln('Duyet giua cay nhi phan tim kiem: ');
        DuyetGiua(T1);
        writeln();

        writeln('Done.');
        readln;
end.

バイナリツリーpharmaco-