Ví dụ mô phỏng thuật toán chuyển cây 2-chiều chuẩn tắc củ aT về

Một phần của tài liệu TỐI ƯU HOÁ LỚP CÁC THÔNG TIN CÓ CẤU TRÚC DẠNG CÂY NHỊ NGUYÊN 1 VÀ N CHIỀU VỚI THÔNG TIN CHỨA Ở LÁ TRÊN TẬP KHÓA HỮU HẠN BẰNG MÔ HÌNH XỬ LÝ SONG SONG (Trang 85)

3. Cây chuẩn tắc một chiều trên tập khóa K

2.7.3Ví dụ mô phỏng thuật toán chuyển cây 2-chiều chuẩn tắc củ aT về

tối ƣu ( thuật toán 2.5)

Giả sử ta có cây:N2 [2, 2] [1, 7] [2, 4] d2 d3 d4 [2, 7] d5 [2,13] [1,11] [1, 14]

Trang 83

d6 d8 d9 d10 h(N2) =4. Ta có: [4/2]+1 =3. Bẻ cây N2

tại đỉnh [2,7] bằng tiên đề ax2 ngược ta được: R2 [2, 7] [2, 4] [2, 13] [2,2] d5 [1,11] [1, 14] [1, 7] d4 d6 d8 d9 d10 d2 d3 Bẻ cây con trái của R2

tại đỉnh [2,2] bằng tiên đề ax2 ngược ta được: R2 [2, 7]

[2, 2] [2, 13] [1,7] [2,4] [1,11] [1, 14]

d2 d3 d4 d5 d6 d8 d9 d10

R2 (K(2))T là cây 2 - chiều tối ưu cần tìm.

2.4.1 Mô phỏng thuật toán phân rã xây dựng cây 2 - chiều tối ưu:

Ta xét một ví dụ mô phỏng hoạt động của thuật toán phân rã. Để đơn giản ta xét cây nhị phân 2 chiều T với các tập khoá K(2) = K x K với K = {5,6,7,8,9,10,11,12,13,14,15,16,17,18} hữu hạn. Cây T có dạng như sau:

T [2, 12]

[2,7] [2, 14]

[2,11] [2,9] [2,12] [2,16]

[2,13] [1,7] [1,13] [1,14] [1,16] 2,13] [2,19] [2,20]

d5 d6 d7 d8 d9 d10 d11 d12 d13 d14 d15 [1,9] d16 d17 d18 d19 d20 d21 d22 d23 d24 d25 d26 [1,16]

d27 d28 d29 d30

Bước 1: Phân hoạch K thành 2 tập con ( để đơn giản ta cho m=2) , ta có K1 và K2 như sau: K1 = {5,6,7,8,9,10,11} , K2 = { 12,13,14,15,16,17,18}

K1(2) = K1 x K1 , K2(2) = K2 x K2

dễ thấy K1max = 11, K1min = 5, K2max = 18, K2min = 12.

Bước 2: Lần lượt thực hiện thuật toán 2.1 trên các tập khoá K1(2) và K2(2) ta thu được các cây 2 chiều chuẩn tắc N12

, N22 như sau: N1, 2 [2,7] N2, 2 [2,12] d6 [2,9] [1,14] [2,13] [1,13] [1,14] d1 [1,16] d3 [2,14] d13 [1,15] d1 [1,16] d16 d17 [1,6] [2,16] d15 d28 d16 d17 d20 d21 d22 d4 Bước 3:

Thực hiện thuật toán 2.2 để ghép các cây N1 2

, N2 2

thành cây chuẩn tắc N2 (K(2))T. Ta có cây N2 như sau:

N2 [2,7]

d6 [2,9]

[1,13] [2,12]

d13 [1,15] [1,14] [2,13] d15 d28 d1 [1,16] d3 [2,14]

Trang 85

d16 d17 [1,6] [2,16] d20 d21 d22 d4 Bước 4: áp dụng thuật toán 4 để chuyển cây chuẩn tắc N2 (adsbygoogle = window.adsbygoogle || []).push({});

( xây dựng ở bước 3) về cây tối ưu R2

(K(2))N2 (K(2))T ta có cây R2 như sau:

R2 [2, 12] [2,7] [2, 14] d6 [2,9] [2,13] [2,16] [1,13] [1,14] d3 [1,6] d22 d4 d13 [1,14] d1 [1,16] d20 d21 d15 d28 d16 d17

Khi tiến hành thuật toán xây dựng cây 2 chiều tối ưu mà không áp dụng thuật toán phân rã ta cũng thu được cùng một kết quả như trên. Thật vậy, áp dụng thuật toán 2.1 trên tập khoá ban đầu K(2) ta có cây chuẩn tắc sau:

N2’ [2,7] (K(2))T d6 [2,9] [1,13] [2,12] d13 [1,15] [1,14] [2,13] d15 d28 d1 [1,16] d3 [2,14] d16 d17 [1,6] [2,16] d20 d21 d22 d4

áp dụng thuật toán 2.5 cho cây N2’ ta thu được cây tối ưu R2’ của T trên K(2) là:

[2,7] [2, 14]

d6 [2,9] [2,13] [2,16] [1,13] [1,14] d3 [1,6] d22 d4

d13 [1,14] d1 [1,16] d20 d21

Trang 87

KẾT LUẬN

Dựa vào các kết quả trong [3] về tối ưu hóa cây nhị phân một chiều với thông tin chứa ở lá trên tập khóa hữu hạn, luận văn đã xây dựng thuật toán phân rã tìm cây nhị phân một chiều tối ưu với thông tin chứa lá trên tập khóa nhằm giảm đáng kể thời gian xây dựng cây nhị phân tìm kiếm tối ưu đồng thời xây dựng chương trình mô phỏng thuật toán phân rã, luận văn cũng đã tổng quát hóa thuật toán phân rã cho lớp các thông tin có cấu trúc dạng cây nhị phân n-chiều với thông tin chứa ở lá trên tập khóa hữu hạn và đã đạt được các kết quả sau:

Với cây nhị phân một chiều:

 Xây dựng các khái niệm: cây nhị phân một chiều với thông tin lá, hàm kết quả, sự tương đương giữa các cây nhị phân, dạng chuẩn của cây nhị phân một chiều, bảng mã của cây nhị phân, cây nhị phân một chiều tối ưu.

 Định nghĩa,các qui tắc dẫn xuất và hệ tiên đề của cây nhị phân một chiều.  Phát biểu và chứng minh các định lý về sự tương đương giữa các cây nhị

phân, sự tồn tại duy nhất của dạng cây chuẩn tắc, sự tồn tại của cây nhị phân tối ưu.

 Thuật toán kiểm tra tính tương đương giữa hai cây nhị phân bất kỳ.  Thuật toán chuyển về cây nhị phân chuẩn tắc trên tập khóa hữu hạn.  Thuật toán nối các cây nhị phân chuẩn tắc.

 Thuật toán xây dựng cây nhị phân tối ưu ( bẻ đôi cây chuẩn tắc)

 Thuật toán phân hoạch tập khóa ban đầu K thành n tập khóa con K1, K2 ,..., Kn.

 Thuật toán phân rã xây dựng cây nhị phân một chiều tối ưu với thông tin chứa ở lá trên tập khóa hữu hạn.

 Cài đặt chương trình mô phỏng thuật toán phân rã xây dựng cây nhị phân một chiều tối ưu với thông tin chứa ở lá

Với cây nhị phân n-chiều:

 Xây dựng các khái niệm: cây nhị phân n-chiều với thông tin chứa ở lá, hàm kết quả, sự tương đương giữa các cây nhị phân n-chiều, dạng chuẩn của cây nhị phân n-chiều, bảng mã của cây nhị phân n-chiều, cây nhị phân n-chiều tối ưu.

 Định nghĩa các qui tắc dẫn xuất và hệ tiên đề của cây nhị phân n-chiều.

 Phát biểu và chứng minh các định lý về sự tương đương giữa các cây nhị phân n- chiều, sự tồn tại duy nhất của dạng cây chuẩn tắc n-chiều, sự tồn tại của cây nhị phân n-chiều tối ưu.

 Thuật toán kiểm tra tính tương đương giữa hai cây nhị phân n-chiều bất kỳ.  Thuật toán chuyển về cây nhị phân n-chiều chuẩn tắc trên tập khóa hữu hạn.  Thuật toán nối các cây nhị phân n-chiều chuẩn tắc.

 Thuật toán xây dựng cây nhị phân n-chiều tối ưu( bẻ đôi cây n-chiều chuẩn tắc)

 Phân hoạch tập khóa ban đầu K(n) thành n tập khóa con Kl(n), ..., Km(n).  Thuật toán phân rã xây dựng cây nhị phân n-chiều tối ưu với thông tin chứa ở

lá trên tập khóa hữu hạn.

Trang 89 (adsbygoogle = window.adsbygoogle || []).push({});

TÀI LIỆU THAM KHẢO:

[ 1 ] Do Duc Giao: Optimization for n-dementional binary search trees .

Cybemetic and System '94 - Voi II, World Scientific (trang 1337 - 1384). [2] Do Duc Giao and A.M Thoa: Optimỉzation for one-dementional binary search trees. HNU. Joumal ofScience, Nat . Sci. 2 (1995) trang 45 - 61 .

[3] Đỗ Đức Giáo và Lê Anh Cường: Tối ưu hóa cây nhị phân một chiều với thông tin chứa ở đỉnh trong trên tập khóa vô hạn.

[4] Phạm Ngọc Hùng: Thuật toán về lớp các thông tin có cấu trúc dạng cây nhị phân n chiều trên tập khoá vô hạn - Khóa luận tốt nghiệp cử nhân hệ chính quy năm 2002 - Khoa Công Nghệ - Đại Học Quốc Gia Hà Nội.

[5] Trần Hoàng Hải: Tối ưu hoá cây nhị phân n-chiều với thông tin chứa Ở đỉnh trong trên tập khóa hữu hạn. - Khóa luận tốt nghiệp cử nhân hệ chính quy năm 2000 - Khoa Công Nghệ - Đại Học Quốc Gia Hà Nội.

[6] H.Thiele, equivalenl transformation of one demensional binary search trees with information in nodes.

PHỤ LỤC

MÔ PHỎNG THUẬT TOÁN VÀ CÀI ĐẶT CHƢƠNG TRÌNH

3.1Tổng quan về chƣơng trình

3.1.1 Ngôn ngữ sử dụng cài đặt: Pascal

Trong luận văn em đã sử dụng ngôn ngữ lập trình Pascal để thể hiện các thụat toán và cài đặt chương trình bởi nó là ngôn ngữ trong sáng, đơn giản, cấu trúc chặt chẽ, rất thuận lợi trong việc thể hiện thuật toán hơn nữa là ngôn ngữ lập trình nhiều người biết và em cũng quen dùng.

3.1.2 Lƣu trữ cây nhị phân

Vì T là cây nhị phân bất kỳ khi đưa vào nên ta dùng cách lưu trữ móc nối để lưu trữ cây. Mỗi nút của cây sẽ là một con trỏ gồm các trường như sau:

Type TREE = ^Node; Node = Record Value:integer; TT:String; Left: TREE; Right:TREE; Level:integer; X:integer; Y:integer; end; Trong đó:

Value : là khoá của nút

TT: Là thông tin được lưu tại nút Left: Là cây con trái của nút Right: Là cây con phải của nút Level: vị trí của nút so với nút cha

X, Y: Tọa độ của nút trên màn hình đồ họa

Lưu ý: chúng ta chỉ quan tâm đến 4 thuộc tính đầu, còn 3 thuộc tính sau chỉ dùng để hỗ trợ cho việc lập trình.

Ta kí hiệu lá của cây là nút có dạng như sau: l.value :=0;

Trang 91 l.left:=nil;

3.2 Chƣơng trình: Uses crt,graph; Type Pointer = ^node; Node=record value : byte;

left, right : pointer ; tt : string; level : integer; x, y : integer; end; Header_node = record title : string; link: pointer; end; tree =^header_node ; Var dic:tree;pd:pointer;t,tam,ll:byte; kmax,kmin:integer;temp:boolean; (**************************************) Procedure taotd(var header:tree);

begin

new(header);

header^.title:=' CAY NHI PHAN ' ; header^.link:=nil;

end;

(**************************************) Procedure chentu(var p:pointer;word:integer; tt:string;level:integer;x:integer;y:integer); Begin if p=nil then begin new(p); if word = 0 then begin p^.value:=word; p^.left:=nil; p^.right:=nil; p^.tt:=tt; p^.level:=level;

Trang 93 p^.x:=x; (adsbygoogle = window.adsbygoogle || []).push({});

p^.y:=y; end

else if (word<>0) then begin p^.value:=word; p^.left:=nil; p^.right:=nil; p^.tt:=''; p^.level:=level; p^.x:=x; p^.y:=y; end; end

else if level <=p^.level then begin

if p^.value=0 then writeln('la khong co con') else chentu(p^.left,word,tt,level,x,y);

end

else if level > p^.level then begin

if p^.value=0 then writeln('la khong co con') else chentu(p^.right,word,tt,level,x,y); end;

End;

(**************************************) procedure cong(var p:pointer;a,b:integer);

Begin if p <> nil then begin p^.x:=p^.x+a; p^.y:=p^.y+b; cong(p^.left,a,b);cong(p^.right,a,b); end; End; (*******************************************) Procedure tru(var p:pointer;a,b:integer);

Begin

if p <> nil then begin

p^.x:=p^.x-a; p^.y:=p^.y-b; tru(p^.left,a,b);tru(p^.right,a,b);

end; End;

(*******************************************) Procedure ax1(var p:pointer);

Var a,b:integer; Begin

if p <> nil then begin

if (p^.left=nil) and (p^.right=nil) then else

begin

if p^.left = nil then else

if p^.left^.value=0 then ax1(p^.right) else

if (p^.left^.left <> nil ) or (p^.left^.right <> nil )then begin

ax1(p^.left);

if p^.left^.value >= p^.value then begin cong(p^.left^.left,p^.left^.x - p^.left^.left^.x, p^.left^.y - p^.left^.left^.y); p^.left:=p^.left^.left; end; ax1(p^.right); end; end; end; End; (**************************************) Function ktax1(var p:pointer):boolean;

Begin

ktax1:=false; if p <> nil then begin

if (p^.left = nil) and (p^.right = nil) then else begin

if p^.left = nil then else if p^.left^.value = 0 then ktax1:=ktax1(p^.right) else

if (p^.left^.left <> nil) or (p^.left^.right <> nil) then begin

Trang 95 ktax1:=ktax1(p^.left); (adsbygoogle = window.adsbygoogle || []).push({});

if (p^.value <= p^.left^.value) then begin ktax1:=true; exit; end; ktax1:=ktax1(p^.right); end; end; end; End; (**************************************************) Procedure tdax1(var p:pointer);

Var kt:boolean; Begin ax1(p); kt:=ktax1(p); while kt=true do begin ax1(p); kt:=ktax1(p); end; End; (************************************************) Procedure ax2_thuan(var p:pointer);

Var store,tam1,tam2:pointer;

l,m,n,kc,a,b,c,d,e,f,g,h,i,k:integer; Begin

if p<> nil then begin

if (p^.left=nil) and (p^.right=nil) then else

begin

if p^.left=nil then else

if p^.left^.value=0 then ax2_thuan(p^.right)

else if (p^.left^.left <> nil ) or (p^.left^.right <> nil ) then begin

ax2_thuan(p^.left);

if (p^.left^.value <p^.value) and (p^.left^.value <> 0) then begin

a:=p^.x; b:=p^.y; c:=p^.left^.x; d:=p^.left^.y; e:=p^.left^.left^.x;

f:=p^.left^.left^.y ; g:=p^.left^.right^.x; h:=p^.left^.right^.y;

i:=p^.right^.x; k:=p^.right^.y; l:=g-c; m:=i+l; n:=i-l; kc:=n-g; tam1:=p^.right;tam2:=p^.left^.right; store:=p; cong(p^.left^.left,c-e,d-f); cong(p^.right,(a+(a-e))-i,f-d); cong(p^.left^.right,kc,0); p:=p^.left; p^.x:=a; p^.y:=b; p^.right:=store; p^.right^.x:=i; p^.right^.y:=k; p^.right^.left:=tam2; p^.right^.right:=tam1; end; ax2_thuan(p^.right); end; end; end; End; (***********************************************) Function numr(p:pointer):integer; Var num:integer; Begin num:=0;

while p^.right <> nil do begin p:=p^.right; num:=num+1; end; numr:=num; End; (***********************************************) Function numl(p:pointer):integer; Var num:integer; Begin num:=0;

while p^.left <> nil do begin p:=p^.left; num:=num+1; end; numl:=num; End; (*************************************************)

Trang 97 Procedure ax2_nghich(var p:pointer);

Var store,tam1,tam2:pointer;

dct,dcp,a,b,c,d,e,f,g,h,l,m,o:integer; Begin

if p<> nil then begin

if (p^.left=nil) and (p^.right=nil) then else

begin

if p^.right=nil then else

if p^.right^.value=0 then ax2_nghich(p^.left)

else if ((p^.right^.left <> nil ) or (p^.right^.right <> nil )) and (p^.right^.right^.value<>0) then

begin dct:=numl(p); dcp:=numr(p); if (dct=dcp) or (dcp=dct+1) then else begin ax2_nghich(p^.left);

if p^.value < p^.right^.value then begin

a:=p^.x; b:=p^.y; c:=p^.left^.x; d:=p^.left^.y; e:=p^.right^.right^.x - p^.right^.x; f:=p^.right^.right^.y - p^.right^.y; g:=p^.x-e; h:=p^.y+f; l:=p^.right^.left^.x - p^.right^.x; m:=p^.right^.left^.y - p^.right^.y; o:=(p^.left^.x-l); cong(p^.left,-e,f); tru(p^.right^.right,p^.right^.right^.x - p^.right^.x, p^.right^.right^.y - p^.right^.y); tru(p^.right^.left,p^.right^.left^.x -o,0); tam1:=p^.left; tam2:=p^.right^.left; store:=p;

p:=p^.right; p^.x:=a; p^.y:=b; p^.left:=store; (adsbygoogle = window.adsbygoogle || []).push({});

p^.left^.x:=c; p^.left^.y:=d; p^.left^.right:=tam2;

p^.left^.left:=tam1; end;

ax2_nghich(p^.right); end; end; end; end; End; (*******************************************) Procedure ax3(var p:pointer);

Var a,b:integer; Begin

if p <> nil then begin

if (p^.left=nil) and (p^.right=nil) then else

begin

if p^.right = nil then else

if p^.right^.value=0 then ax3(p^.left) else

if (p^.right^.left <> nil ) or (p^.right^.right <> nil) then begin

ax3(p^.left);

if p^.right^.value <= p^.value then begin tru(p^.right^.right,p^.right^.right^.x - p^.right^.x, p^.right^.right^.y - p^.right^.y); p^.right:=p^.right^.right; end; ax3(p^.right); end; end; end; End; (**************************************) Function ktax3(var p:pointer):boolean;

Begin

ktax3:=false; if p <> nil then begin

if (p^.left = nil) and (p^.right = nil) then else begin

if p^.right = nil then else if p^.right^.value = 0 then

Trang 99 ktax3:=ktax3(p^.left)

else

if (p^.right^.left <> nil) or (p^.right^.right <> nil) then begin

ktax3:=ktax3(p^.left);

if (p^.right^.value <= p^.value) then begin ktax3:=true; exit; end; ktax3:=ktax3(p^.right); end; end; end; End; (************************************************) Procedure tdax3(var p:pointer);

Var kt:boolean; Begin ax3(p); kt:=ktax3(p); while kt=true do begin ax3(p); kt:=ktax3(p); end; End; (*******************************************) Procedure ax4(var p:pointer);

Var a,b:integer; Begin

if p <> nil then begin

if (p^.left=nil) and (p^.right=nil) then else

begin

if (p^.left <> nil ) and (p^.right <> nil) then begin

ax4(p^.left);

if (p^.left^.value = p^.right^.value) and (p^.left^.value =0) then

begin (adsbygoogle = window.adsbygoogle || []).push({});

if (p^.left^.tt = p^.right^.tt) then begin

a:=p^.x; b:=p^.y; p:=p^.left; p^.x:=a; p^.y:=b; end; end; ax4(p^.right); end; end; end; End; (**************************************) Function ktax4(var p:pointer):boolean;

Begin

ktax4:=false; if p <> nil then begin

if (p^.left = nil) and (p^.right = nil) then else begin

if (p^.right^.left <> nil) and (p^.right^.right <> nil) then begin

ktax4:=ktax4(p^.left);

if (p^.left^.value = p^.right^.value) and (p^.left^.value=0) then

if p^.left^.tt=p^.right^.tt then begin ktax4:=true; exit; end; end; end; end; End; (************************************************) Procedure tdax4(var p:pointer);

Var kt:boolean; Begin ax4(p); kt:=ktax4(p); while kt=true do begin ax4(p); kt:=ktax4(p); end; End; (************************************************)

Trang 101 Procedure ax5(var p:pointer);

Var a,b:integer; Begin

if p <> nil then begin

if (p^.left=nil) and (p^.right=nil) then else

begin

if p^.right = nil then else

if p^.right^.value=0 then ax5(p^.left) else

if (p^.right^.left <> nil ) or (p^.right^.right <> nil) and (p^.right^.left^.value=0) then

begin

ax5(p^.left);

if (p^.right^.value > p^.value) and (p^.left^.value=0) then begin

if (p^.left^.value = p^.right^.left^.value) and (p^.left^.tt=p^.right^.left^.tt) then begin tru(p^.right,p^.right^.x - p^.x, p^.right^.y - p^.y); p:=p^.right; end; end; ax5(p^.right); end; end; end; End; (***********************************************) Function ktax5(var p:pointer):boolean;

Begin

ktax5:=false; if p <> nil then begin

if (p^.left = nil) and (p^.right = nil) then else begin

if p^.right = nil then else if p^.right^.value = 0 then ktax5:=ktax5(p^.left) else

if (p^.right^.left <> nil) or (p^.right^.right <> nil) and (p^.left^.value = 0) then

begin

ktax5:=ktax5(p^.left);

if (p^.right^.value > p^.value) and (p^.left^.value=0) then begin (adsbygoogle = window.adsbygoogle || []).push({});

if (p^.left^.value=p^.right^.left^.value) and (p^.left^.tt=p^.right^.left^.tt) then begin ktax5:=true; exit; end; end; end; end; end; End; (**************************************************) Procedure tdax5(var p:pointer);

Var kt:boolean; Begin ax5(p); kt:=ktax5(p); while kt=true do begin ax5(p); kt:=ktax5(p); end; End; (*************************************************) Procedure ax6(var p:pointer;kmax:integer);

Begin

if p <> nil then begin

if (p^.left=nil) and (p^.right=nil) then else

begin

if (p^.left <> nil) and (p^.right <> nil) then begin

ax6(p^.left,kmax);

if (p^.value > kmax) and (p^.left <> nil) then begin

Trang 103 p^.y - p^.left^.y); p:=p^.left; end; ax6(p^.right,kmax); end; end; end; End; (**************************************************) Procedure ax7(var p:pointer;kmin:integer);

Begin

if p <> nil then begin

if (p^.left=nil) and (p^.right=nil) then else

begin

if (p^.left <> nil) and (p^.right <> nil) then begin

ax7(p^.left,kmin);

if (p^.value < kmin) and (p^.right <> nil) then begin tru(p^.right,p^.right^.x - p^.x, p^.right^.y - p^.y); p:=p^.right; end; ax7(p^.right,kmin); end; end; end; End; (***************************************************) Procedure caybandau; Var f,f1:text; a,b,c,d:array[1..50] of integer; e:array[1..50] of char; i,j,x,y,z,u,k:integer; v,tt:char; Begin assign(f,'c:\bp\bin\lp.txt'); reset(f); i:=1;

begin

readln(f,x,y,z,u);

a[i]:=x; b[i]:=y;c[i]:=z; d[i]:=u; i:=i+1; end;

assign(f1,'c:\bp\bin\tt.txt'); reset(f1);

k:=1;

while not eof(f1) do begin readln(f1,v); e[k]:=v; k:=k+1; end; for j:=1 to i-1 do chentu(dic^.link,a[j],e[j],b[j],c[j],d[j]); close(f); End; (**********************************************) Procedure cayhc(var p:pointer);

Var m,i,dct,dcp:integer; Begin

m:=numr(p);

if (m div 2) =1 then begin

for i:=1 to m div 2 do

begin ax2_nghich(p); end; end

else

for i:=1 to (m div 2)-1 do begin ax2_nghich(p); end; dct:=numl(p); dcp:=numr(p); if (dct=dcp) or (dcp=dct+1) then else begin ax2_thuan(p^.right); ax2_nghich(p); end; End; (********************************************) Function testright(var p:pointer):boolean; (adsbygoogle = window.adsbygoogle || []).push({});

Var a,b,c,d,e,f:integer; Begin

testright:=true;

Trang 105 begin

if (p^.left=nil) and (p^.right=nil) then else

begin

if p^.right=nil then else begin

if p^.left^.value <>0 then begin

testright:=false;exit; end;

if (p^.right^.value <= p^.value) and

(p^.left^.value=0) and (p^.right^.value<>0) or (p^.left^.tt=p^.right^.left^.tt) then begin testright:=false; exit; end; testright:=testright(p^.right); end; end; end; End; (******************************************) Function checkright(p:pointer):boolean; Var dct,dcp:integer; Begin checkright:=false; dct:=numl(p); dcp:=numr(p); if (dct=dcp) or ( dcp=dct+1) then checkright:=true; End; (******************************************) Function timtt(var p:pointer;k:integer):string;

Begin

if p<> nil then begin

if (p^.left=nil) and (p^.right=nil) then timtt:=p^.tt else if (p^.value >= k) then timtt:=timtt(p^.left,k) else if (p^.value < k) then timtt:=timtt(p^.right,k); end;

End;

Procedure timkiem(var p:pointer); Label tieptuc; Var tt,tam,tam1:string; z,luu,i:integer; ch,ch1:char; Begin tieptuc: setfillstyle(1,2); setcolor(2);bar(100,300,540,450); floodfill(120,400,2);

setcolor(15); outtextxy(150,350,'NHAP KHOA: '); tam:='';z:=230;

repeat

ch:=readkey;

if (ord(ch)>=48) and(ord(ch)<=5) then begin if length(tam) <5 then begin setcolor(4);outtextxy(z,350,ch); tam:=tam+ch; z:=z+5; end; end;

until (tam<> '') and (ch=#13); val(tam,luu,i); tt:=timtt(p,luu); setcolor(15);

outtextxy(150,370,'THONG TIN TIM DUOC: '); setcolor(4); outtextxy(310,370,tt);

outtextxy(170,400,'CO TIEP TUC TIM KHONG (C/K): '); tam:='';

repeat

ch1:=readkey;

if (ord(ch1)=67) or (ord(ch1)=75) then begin

outtextxy(400,400,ch1);tam1:=ch1; end;

until (tam1<>'') and (ch1=#13);

if tam1='C' then goto tieptuc else exit; End;

(***********************************************) Procedure venut(var p:pointer);

Var temp:string; Begin (adsbygoogle = window.adsbygoogle || []).push({});

Trang 107 setbkcolor(8); setcolor(10); if p<> nil then begin venut(p^.left); if p^.value <> 0 then begin circle(p^.x,p^.y,8); setfillstyle(1,4); floodfill(p^.x,p^.y,10); str(p^.value,temp); outtextxy(p^.x-3,p^.y-3,temp); end

else if p^.value =0 then

outtextxy(p^.x-5,p^.y-1,p^.tt); venut(p^.right);

end; End;

(*********************************************) Procedure noinode(var p:pointer);

Var temp:string; Begin

setcolor(10); if p<> nil then begin

if (p^.left=nil) and (p^.right=nil) then else

begin

if (p^.left<> nil) then

line(p^.x,p^.y+8,p^.left^.x,p^.left^.y-8); if (p^.right<> nil) then

line(p^.x,p^.y+8,p^.right^.x,p^.right^.y-8); end; noinode(p^.left); noinode(p^.right); end; End; (*********************************************) Procedure treetu(var p:pointer);

Var tam,tam1,tam2,tam3:string; ch,ch1,ch2:char; luu,luu1,luu2,i,z,z1,z2:integer;

tr,check:boolean; khoa:byte; Begin

cayhc(p); cleardevice; venut(p); noinode(p); setcolor(15);

settextstyle(3,0,1);

outtextxy(200,450,'SAU KHI THUC HIEN AX2 NGHICH'); settextstyle(defaultfont,0,0);readln;

check:=checkright(p); if check=true then begin

cleardevice; venut(p); noinode(p);

outtextxy(200,460,'DAY LA CAY HOAN CHINH'); rectangle(1,1,639,479);

outtextxy(10,10,'CO TIM THONG KHONG ? '); outtextxy(10,20,'TRA LOI (C/K: ');

tam1:=''; repeat

ch1:=readkey;

if (ord(ch1)=67) or (ord(ch1)=75) then begin

outtextxy(120,20,ch1); tam1:=ch1; end;

until (tam1<> '') and ( ch1=#13); if tam1='C' then timkiem(p); end; End; (**********************************************) Procedure khoitao; Var gd,gm:integer; Begin gd:=detect; initgraph(gd,gm,'c:\bp\bgi'); End; (**********************************************) Procedure cactd(var p:pointer);

Label nhan;

Var tam,tam1,tam2,tam3:string; ch,ch1,ch2:char; luu,luu1,luu2,i,z,z1,z2:integer;

tr,check:boolean; khoa:byte; Begin

khoitao; venut(p); noinode(p); settextstyle(1,0,1); (adsbygoogle = window.adsbygoogle || []).push({});

outtextxy(200,450,'CAY BAN DAU'); settextstyle(defaultfont,0,0);

Trang 109 tam2:=''; z1:=310;

repeat

ch1:=readkey;

if (ord(ch1)>=48) and (ord(ch1)<=57) then begin if length(tam2)<5 then begin setcolor(4); outtextxy(z1,400,ch1); tam2:= tam2+ch1; z1:= z1+5; end; end;

until (tam2 <> '') and (ch1=#13); val(tam2,luu1,i);

setcolor(10);

outtextxy(200,410,'NHAP VAO KMIN: '); tam3:=''; z2:=310;

repeat

ch2:=readkey;

if (ord(ch2)>=48) and (ord(ch2)<=57) then begin if length(tam3)<5 then begin setcolor(4); outtextxy(z2,410,ch2); tam3:= tam3+ch2; z2:= z2+5; end; end;

until (tam3 <> '') and (ch2=#13); val(tam3,luu2,i);

ax6(p,luu1); ax7(p,luu2);

cleardevice; venut(p); noinode(p); settextstyle(1,0,1);

outtextxy(200,430,' CAY SAU KHI THUC HIEN AX6 '); outtextxy(200,450,' CAY SAU KHI THUC HIEN AX7 '); settextstyle(defaultfont,0,0);

tr:=testright(p); if tr= true then begin

outtextxy(200,240,'DAY LA CAY LECH PHAI '); outtextxy(200,260,'NHAN ENTER DE TIEP TUC '); readln;

end else begin

nhan:rectangle(1,1,639,479);

outtextxy(10,10,'Muc 1: tien de ax1: '); outtextxy(10,20,'Muc 2: tien de ax2: '); outtextxy(10,30,'Muc 3: tien de ax3: '); outtextxy(10,40,'Muc 4: tien de ax4: '); outtextxy(10,50,'Muc 5: tien de ax5: '); outtextxy(10,60,'Muc 8: Thoat '); outtextxy(10,70,'Chon muc: '); tam:=''; z:=90;

repeat

ch:=readkey;

if (ord(ch) >=48) and (ord(ch)<=57) then begin if length(tam)<5 then begin setcolor(4); outtextxy(z,70,ch); tam:=tam+ch; z:=z+5; end; end;

until (tam<> '') and (ch=#13); val(tam,luu,i); case luu of 1: begin tdax1(p); cleardevice; venut(p); noinode(P); setcolor(10); settextstyle(3,0,1);

outtextxy(200,450,'SAU KHI THUC HIEN AX1'); settextstyle(defaultfont,0,0);

tr:=testright(p); if tr = true then begin

outtextxy(200,240,'DAY LA CAY LECH PHAI'); outtextxy(200,260,'AN ENTER DE TIEP TUC'); readln;

treetu(p); end

else goto nhan; end;

Trang 111 2: begin (adsbygoogle = window.adsbygoogle || []).push({});

ax2_thuan(p); cleardevice; venut(p); noinode(P);

setcolor(14); settextstyle(3,0,1);

outtextxy(200,450,'SAU KHI THUC HIEN AX2'); settextstyle(defaultfont,0,0); goto nhan; end; 3: begin tdax3(p); cleardevice; venut(p); noinode(P); setcolor(11); settextstyle(3,0,1);

outtextxy(200,450,'SAU KHI THUC HIEN AX3'); settextstyle(defaultfont,0,0);

tr:=testright(p); if tr = true then begin

outtextxy(200,240,'DAY LA CAY LECH PHAI'); outtextxy(200,260,'AN ENTER DE TIEP TUC'); readln;

Một phần của tài liệu TỐI ƯU HOÁ LỚP CÁC THÔNG TIN CÓ CẤU TRÚC DẠNG CÂY NHỊ NGUYÊN 1 VÀ N CHIỀU VỚI THÔNG TIN CHỨA Ở LÁ TRÊN TẬP KHÓA HỮU HẠN BẰNG MÔ HÌNH XỬ LÝ SONG SONG (Trang 85)