Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Duyet_Cay;
- procedure Tham_truoc(T);
- begin
- if T= Nil then
- begin
- write("Cây rỗng.")
- else
- begin
- top:=0;
- push(S,top,T);
- while top>0 do {Duyệt cây}
- begin
- p:=pop(S,top);
- while p # nil do {Thăm nút xuống con trái}
- begin
- write(P.data);
- if (P.P_R # nil) then
- push(S,top,P.P_R);
- P:=P.P_L;
- end;
- end;
- end;
- End;
- procedure Tham_giua(T);
- begin
- if T= nil then
- write('Cay rong')
- else
- begin
- top:=0;
- p:=T;
- while((top>0) and (p# nil) ) do
- begin
- while p# nil do {Lưu con trái}
- begin
- push(S,top,P);
- p:=P.P_L;
- end;
- p:=pop(S,top);
- write(P.data); {Thăm nút xuống con phải}
- p:=P.P_R;
- end;
- end;
- End;
- Procedure Tham_sau(T);
- begin
- if T=nil then
- write('Cay rong')
- else
- begin
- top:=0;
- p:=T;
- while(true) do
- begin
- while p# nil do {Lưu địa chỉ gốc xuống con trái}
- begin
- push(S,top,P);
- p:=P.P_L;
- end;
- while s[top]<0 do {Thăn nút trái, phải đã duyệt}
- begin
- p:=pop(S,top);
- write(P.data);
- if top = 0 then
- return;
- end;
- {Xuống con phải và đánh dấu}
- p:=s[top].P_R;
- s[top]:=-S[top];
- end;
- end;
- End;
- BEGIN
- Nhập cây;
- Tham_truoc(T);
- Tham_giua(T);
- Tham_sau(T);
- readln
- END.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement