Advertisement
huyhung94

Giải thuật duyệt cây

Oct 7th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.49 KB | None | 0 0
  1. program Duyet_Cay;
  2. procedure Tham_truoc(T);
  3.     begin
  4.         if T= Nil then
  5.             begin
  6.                 write("Cây rỗng.")
  7.             else
  8.                 begin
  9.                     top:=0;
  10.                     push(S,top,T);
  11.                     while top>0 do {Duyệt cây}
  12.                         begin
  13.                             p:=pop(S,top);
  14.                             while p # nil do {Thăm nút xuống con trái}
  15.                                 begin
  16.                                     write(P.data);
  17.                                     if (P.P_R # nil) then
  18.                                         push(S,top,P.P_R);
  19.                                     P:=P.P_L;
  20.                                 end;
  21.                         end;
  22.                 end;
  23.     End;
  24. procedure Tham_giua(T);
  25.     begin
  26.         if T= nil then
  27.             write('Cay rong')
  28.         else
  29.             begin
  30.                 top:=0;
  31.                 p:=T;
  32.                 while((top>0) and (p# nil) ) do
  33.                     begin
  34.                         while p# nil do   {Lưu con trái}
  35.                             begin
  36.                                 push(S,top,P);
  37.                                 p:=P.P_L;
  38.                             end;
  39.                         p:=pop(S,top);
  40.                         write(P.data);  {Thăm nút xuống con phải}
  41.                         p:=P.P_R;
  42.                     end;
  43.             end;
  44.     End;
  45. Procedure Tham_sau(T);
  46.     begin
  47.         if T=nil then
  48.             write('Cay rong')
  49.         else
  50.             begin
  51.                 top:=0;
  52.                 p:=T;
  53.                 while(true) do
  54.                     begin
  55.                         while p# nil do   {Lưu địa chỉ gốc xuống con trái}
  56.                             begin
  57.                                 push(S,top,P);
  58.                                 p:=P.P_L;
  59.                             end;
  60.                         while s[top]<0 do {Thăn nút trái, phải đã duyệt}
  61.                             begin
  62.                                 p:=pop(S,top);
  63.                                 write(P.data);
  64.                                 if top = 0 then
  65.                                     return;
  66.                             end;
  67.                         {Xuống con phải và đánh dấu}
  68.                         p:=s[top].P_R;
  69.                         s[top]:=-S[top];
  70.                     end;
  71.             end;
  72.     End;
  73. BEGIN
  74.     Nhập cây;
  75.     Tham_truoc(T);
  76.     Tham_giua(T);
  77.     Tham_sau(T);
  78.     readln
  79. END.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement