Advertisement
huyhung94

Giải thuật thêm nút trên cây

Oct 7th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 0.60 KB | None | 0 0
  1. program B_S_T;
  2. procedure BST(T,x);
  3.     begin
  4.         P:=T; Q:=nil {Q luôn là cha của P}
  5.         while P# nil do
  6.             begin
  7.                 Q:=P;
  8.                 if x=P.data then
  9.                     break;
  10.                 if x<P.data then P:=P.P_L;
  11.                 if x>P.data then P:=P.P_R;
  12.             end;
  13.         if p=nil then {Không tìm thấy, bổ sung}
  14.             begin
  15.                 new(p);
  16.                 p.data:=x;
  17.                 P.P_L:=nil;
  18.                 P.P_R:=nil
  19.                 if T=nil then T:=p
  20.                 else
  21.                     if(x<Q.data) then
  22.                         Q.P_L:=P
  23.                     else Q.P_R:=P;
  24.                 write('Không tìm thấy, đã bổ sung xong.');
  25.             end;
  26.     End;
  27. BEGIN
  28.     Nhập cây;
  29.     Nhập giá trị cần bổ sung X;
  30.     BST(T,x);
  31.     readln
  32. END.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement