Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Xoa;
- procedure BST_Delete(T,X);
- begin
- P:=T;
- Q:=nil; {Q là cha của P}
- while P# nil do {Tìm x trên cây}
- begin
- if x=p.data then
- break
- Q:=P;
- if x<P.data then
- P:=P.P_L
- else
- P:=P.P_R;
- end;
- if P=nil then
- write('Không tìm thấy nút cần xóa')
- else {Tìm thấy và xóa}
- begin
- if ((P.P_R # nil) and (P.P_R # nil)) then
- begin {Nút cần xóa có 2 con, đi tìm nút thay thế}
- node:=P; Q:=P;
- P:=P.P_L;
- while (P.P_R # nil) do
- begin
- Q:=P; P:=P.P_R;
- end;
- {Chuyển giá trị nút thay thế vào nút cần xóa}
- node.data:=P.data;
- end;
- {Xóa nút P đang trỏ vào, cho con trỏ child trỏ vào nút con của P}
- if P.P_R# nil then
- child:=P.P_R
- else
- child:=P.P_L;
- if T=P then
- T:=child
- else
- if Q.P_L=P then
- Q.P_L:=child
- else
- Q.P_R:=child;
- Dispose(P);
- end;
- End;
- BEGIN
- Nhập cây;
- Nhập giá trị cần xóa X;
- BST_Delete(T,x);
- readln
- END.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement