You are on page 1of 20

nh ngha: Cy nh phn tm kim cn bng l cy m ti mi nt ca n cao ca cy con tri v ca cy con phi chnh lch khng qu mt .

V d:

nh gi: Mt cy rt kh t c trng thi cn bng hon ton v cng rt d mt cn bng v khi thm hay hy cc nt trn cy c th lm cy mt cn bng, chi ph cn bng li cy cao v phi thao tc trn ton b cy. i vi cy cn bng hon ton, trong trng hp xu nht ta ch phi tm qua log2N phn t (N l s nt trn cy). Lch s ca cy cn bng: AVL l tn vit tt ca cc tc gi ngi Nga a ra nh ngha ca cy cn bng Adelson-Velskii v Landis (1962). V l do ny, ngi ta gi cy nh phn cn bng l cy AVL. T cy AVL, ngi ta pht trin thm nhiu loi CTDL hu dng khc nh cy -en (Red-Black Tree), B-Tree,

Chiu cao ca cy AVL: Mt vn quan trng, nh cp n phn trc, l ta phi khng nh cy AVL c N nt phi c chiu cao khong log2(n). nh gi chnh xc v chiu cao ca cy AVL, ta xt bi ton: cy AVL c chiu cao h s phi c ti thiu bao nhiu nt ? Gi N(h) l s nt ti thiu ca cy AVL c chiu cao h. Ta c N(0) = 0, N(1) = 1 v N(2) = 2. Cy AVL c chiu cao h s c 1 cy con AVL chiu cao h-1 v 1 cy con AVL chiu cao h-2. Nh vy: N(h) = 1 + N(h-1) + N(h-2) (1) Ta li c: N(h-1) > N(h-2) Nn t (1) suy ra: N(h) > 2N(h-2) N(h) > 22N(h-4) N(h) > 2iN(h-2i) i =h/2 N(h)>2h/2 h < 2log2(N(h)) Nh vy, cy AVL c chiu cao O(log2(n)). T chc d liu: Ch s cn bng = lch gia cy tri v cy phi ca mt nt Cc gi tr hp l : CSCB(p) = 0 CSCB(p) = 1 CSCB(p) = -1 cao cy tri (p) = cao cy phi (p) cao cy tri (p) < cao cy phi (p) cao cy tri (p) > cao cy phi (p)

#define LH -1 //cy con tri cao hn #define EH 0 //cy con tri bng cy con phi #define RH 1 //cy con phi cao hn typedef struct tagAVLNode { char balFactor; //ch s cn bng Data key; struct tagAVLNode* struct tagAVLNode* }AVLNode; typedef AVLNode *AVLTree; Cc trng hp mt cn bng do lch tri Cy mt cn bng ti nt T TH1: left- left: TH2: left-right: pLeft; pRight;

TH3:Right- Right

TH4: Right- Left

Cc theo tc trn cy cn bng: Khi thm hay xo 1 nt trn cy, c th lm cho cy mt tnh cn bng, khi y ta phi tin hnh cn bng li. Cy c kh nng mt cn bng khi thay i chiu cao: Lch nhnh tri, thm bn tri Lch nhnh phi, thm bn phi Lch nhnh tri, hy bn phi Lch nhnh phi, hy bn tri Cn bng li cy : tm cch b tr li cy sao cho chiu cao 2 cy con cn i: Ko nhnh cao b cho nhnh thp Phi bo m cy vn l Nh phn tm kim

Cn bng li TH1:

Ci t: void LL(AVLTree &T) { AVLNode *T1=T->pLeft; T->pLeft = T1->pRight; T1->pRight=T; switch(T1-> balFactor) { case LH: T-> balFactor =EH; T1->balFactor=EH; break; case EH: T->balFactor=LH; T1->balFactor =RH; break; } T=T1; }

Cn bng li TH2:

Ci t: void LR(AVLTree &T) {AVLNode *T1=T->pLeft; AVLNode *T2=T1->pRight; T->pLeft=T2->pRight; T2->pRight=T; T1->pRight= T2->pLeft; T2->pLeft = T1 switch(T2->balFactor) {case LH: T->balFactor=RH; T1->balFactor=EH; break; case EH: T->balFactor = EH; T1>balFactor=EH; break; case RH: T->balFactor =EH; T1->balFactor= LH; break; }T2->balFactor =EH; T=T2;}

Cn bng li TH3:

Ci t: void RR(AVLTree &T) {AVLNode *T1= T->pRight; T->pRight=T1->pLeft; T1->pLeft=T; switch(T1-> balFactor) { case RH: T-> balFactor = EH; T-> balFactor = EH; break; case EH: T-> balFactor = RH; T1-> balFactor = LH; break; } T=T1; }

Cn bng ti TH4:

Ci t: void RR(AVLTree &T) { AVLNode *T1= T->pRight; AVLNode *T2=T1->pLeft; T->pRight = T2->pLeft; T2->pLeft = T; T1->pLeft = T2->pRight; T2->pRight = T1; switch(T2-> balFactor) { case RH: T-> balFactor = LH; T1-> balFactor = EH; break; case EH: T-> balFactor = EH; T1-> balFactor = EH; break; case LH: T-> balFactor = EH; T1-> balFactor = RH; break; } T2-> balFactor =EH; T=T2;}

Thm 1 nt: Thm bnh thng nh trng hp cy NPTK Nu cy tng trng chiu cao Ln ngc v gc pht hin nt b mt cn bng Tin hnh cn bng li nt bng thao tc cn bng thch hp Vic cn bng li ch cn thc hin 1 ln ni mt cn bng Hy 1 nt: Hy bnh thng nh trng hp cy NPTK Nu cy gim chiu cao: Ln ngc v gc pht hin nt b mt cn bng Tin hnh cn bng li nt bng thao tc cn bng thch hp Tip tc ln ngc ln nt cha Vic cn bng li co th lan truyn ln tn gc AGAIN! P/S

CTDL: Cy cn bng
1. CY CN BNG (AVL) nh ngha: Ch s cn bng ca mt nt l hiu ca chiu cao cy con phi v cy con tri ca n. i vi mt cy cn bng, ch s cn bng (CSCB) ca mi nt ch c th mang mt trong ba gi tr sau y: CSCB(p) = 0 <=> cao cy tri (p) = cao cy phi (p) CSCB(p) = 1 <=> cao cy tri (p) < cao cy phi (p) CSCB(p) =-1 <=> cao cy tri (p) > cao cy phi (p)

tin trong trnh by, chng ta s k hiu nh sau: p->balFactor = CSCB(p); cao cy tri (p) k hiu l hL cao cy phi (p) k hiu l hR

kho st cy cn bng, ta cn lu thm thng tin v ch s cn bng ti mi nt. Lc , cy cn bng c th c khai bo nh sau: typedef struct tagAVLNode { DataType key; char balFactor; //Ch s cn bng struct tagAVLNode* pLeft; struct tagAVLNode* pRight; }AVLNode, *AVLTree; tin cho vic trnh by, ta nh ngha mt s hng s sau: #define LH -1 //Cy con tri cao hn #define EH -0 //Hai cy con bng nhau #define RH 1 //Cy con phi cao hn 2. CC TRNG HP MT CN BNG Ta s khng kho st tnh cn bng ca 1 cy nh phn bt k m ch quan tm n cc kh nng mt cn bng xy ra khi thm hoc hy mt nt trn cy AVL. Nh vy, khi mt cn bng, lch chiu cao gia 2 cy con s l 2. Ta c 6 kh nng sau: + Trng hp 1: Cy T lch v bn tri (c 3 kh nng)

Trng hp 2: cy T lch v bn phi , Ta c cc kh nng sau:

Ta c th thy rng cc trng hp lch v bn phi hon ton i xng vi cc trng hp lch v bn tri. V vy ta ch cn kho st trng hp lch v bn tri. Trong 3 trng hp lch v bn tri, trng hp T1 lch phi l phc tp nht. Cc trng hp cn li gii quyt rt n gin. Sau , ta s kho st v gii quyt tng trng hp nu trn. + Trng hp 1.1: cy T1 lch v bn tri. Ta thc hin php quay n Left-Left

+ Trng hp 1.2: cy T1 khng lch. Ta thc hin php quay n Left-Left

+ Trng hp 1.3: cy T1 lch v bn phi. Ta thc hin php quay kp Left-Right Do T1 lch v bn phi ta khng th p dng php quay n p dng trong 2 trng hp trn v khi cy T s chuyn t trng thi mt cn bng do lch tri thnh mt cn bng do lch phi, cn p dng cch khc. Hnh v di y minh ha php quay kp p dng cho trng hp ny:

Lu rng, trc khi cn bng cy T c chiu cao h+2 trong c 3 trng hp 1.1, 1.2 v 1.3. Sau khi cn bng, trong 2 trng hp 1.1 v 1.3 cy c chiu cao h+1; cn trng hp 1.2 cy vn c chiu cao h+2. V trng hp ny cng l trng hp duy nht sau khi cn bng nt T c c ch s cn bng 0. Vi nhng xem xt trn, xt tng t cho trng hp cy T lch v bn phi, ta c th xy dng 2 hm quay n v 2 hm quay kp sau:

//Quay n Left-Left void rotateLL ( AVLTree &T) { AVLNode * T1 = T-> pLeft ; T->pLeft = T1->pRight ; T1-> pRight = T; switch( T1-> balFactor ) { case LH: T-> balFactor = EH; T1-> balFactor = EH; break ; case EH: T-> balFactor = LH; T1-> balFactor = RH; break ; } T = T1; } //Quay n Right-Right void rotateRR(AVLTree &T) { AVLNode * T1 = T-> pRight ; T->pRight = T1-> pLeft ; T1-> pLeft = T; switch ( T1-> balFactor ) {

case RH: T-> balFactor = EH; T1-> balFactor = EH; break ; case EH: T-> balFactor = RH; T1-> balFactor = LH; break ; } T = T1; }

//Quay kp Left-Right void rotateLR(AVLTree &T) { AVLNode * T1 = T-> pLeft ; AVLNode * T2 = T1-> pRight ; T-> pLeft = T2-> pRight ; T2-> pRight = T; T1-> pRight = T2-> pLeft ; T2-> pLeft = T1; switch (T2-> balFactor ) { case LH: T-> balFactor = RH; T1-> balFactor = EH;

break ; case EH: T-> balFactor = EH; T1-> balFactor = EH; break ; case RH: T-> balFactor = EH; T1-> balFactor = LH; break ; } T2-> balFactor = EH; T = T2; } //Quay kp Right-Left void rotateRL ( AVLTree &T) { AVLNode * T1 = T-> pRight ; AVLNode * T2 = T1-> pLeft ; T->pRight = T2-> pLeft ; T2->pLeft = T; T1->pLeft = T2-> pRight ; T2-> pRight = T1; switch (T2-> balFactor ) { case RH: T-> balFactor = LH; T1-> balFactor = EH; break ; case EH: T-> balFactor = EH; T1-> balFactor = EH; break ; case LH: T-> balFactor = EH; T1-> balFactor = RH; break ; } T2-> balFactor = EH; T = T2; } thun tin, ta xy dng 2 hm cn bng li khi cy b lch tri hay lch phi nh sau:

//Cn bng khi cy b lch v bn tri int balanceLeft(AVLTree &T) { switch (T-> pLeft -> balFactor ) { case LH: rotateLL (T); return 2; case EH: rotateLL (T); return 1; case RH: rotateLR (T); return 2; } return 0; } //Cn bng khi cy b lch v bn phi int balanceRight(AVLTree &T) { switch (T-> pRight -> balFactor ) { case LH: rotateRL (T); return 2; case EH: rotateRR (T); return 1; case RH: rotateRR (T); return 2; } return 0; } 3.THM MT PHN T TRN CY AVL Thm mt phn t vo cy AVL din ra tng t nh trn CNPTK. Tuy nhin, sau khi thm xong, nu chiu cao ca cy thay i, t v tr thm vo, ta phi ln ngc ln gc kim tra xem c nt no b mt cn bng khng. Nu c, ta phi cn bng li nt ny. Vic cn bng li ch cn thc hin 1 ln ti ni mt cn bng. Hm insertNode tr v gi tr -1, 0, 1 khi khng b nh, gp nt c hay thnh cng. Nu sau khi thm, chiu cao cy b tng, gia tr 2 s c tr v: int insertNode(AVLTree &T, DataType X) { int res;

if (T != NULL ) { if(T->key == X) return 0; // c else if (T->key > X) { res = insertNode(T->pLeft, X); if (res < 2) return res; switch (T->balFactor) { case RH: T->balFactor= EH; return 1; case EH: T->balFactor = LH; return 2; case LH: balanceLeft(T); return 1; } } else // if (T->key < X) { res = insertNode(T-> pRight, X); if (res < 2) return res; switch (T->balFactor) { case LH: T->balFactor = EH; return 1; case EH: T->balFactor = RH; return 2; case RH: balanceRight(T); return 1; } } } T = new TNode; if (T == NULL) return -1; //thiu b nh T->key = X; T->balFactor = EH;

T->pLeft = T->pRight = NULL; return 2; // thnh cng, chiu cao tng } 4. HY MT PHN T TRN CY AVL Cng ging nh thao tc thm mt nt, vic hy mt phn t X ra khi cy AVL thc hin ging nh trn CNPTK. Ch sau khi hy, nu tnh cn bng ca cy b vi phm ta s thc hin vic cn bng li. Tuy nhin vic cn bng li trong thao tc hy s phc tp hn nhiu do c th xy ra phn ng dy chuyn. Hm deleteNode tr v gi tr 1, 0 khi hy thnh cng hoc khng c X trong cy. Nu sau khi hu, chiu cao cy b gim, gi tr 2 s c tr v: int deleteNode(AVLTree &T, DataType X) { int res ; if (T==NULL) return 0; if (T->key > X) { res = deleteNode (T-> pLeft , X); if ( res < 2) return res ; switch ( T-> balFactor ) { case LH: T-> balFactor = EH; return 2; case EH: T-> balFactor = RH; return 1; case balanceRight (T); } } else if (T->key < X) { res = deleteNode (T-> pRight , X); if ( res < 2) return res ; switch (T-> balFactor ) {

case RH: T-> balFactor = EH; return 2; case EH: T-> balFactor = LH; return 1; case LH: return balanceLeft (T); } } else // if (T->key == X) { AVLNode * p = T; if (T-> pLeft == NULL) { T = T->pRight ; res = 2; } else if (T-> pRight == NULL) { T = T-> pLeft ; res = 2; } else // if (T-> pLeft != NULL && T-> pRight != NULL) { res = searchStandFor ( p, T -> pRight ); if( res < 2) return res; switch( T-> balFactor ) { case RH: T-> balFactor = EH; return 2; case EH: T-> balFactor = LH; return 1; case LH: return balanceLeft (T); } } delete p; return res ; } } //Tm phn t th mng int searchStandFor(AVLTree &p, AVLTree &q)

{ int res; if (q->pLeft != NULLL) { res = searchStandFor(p, q->pLeft); if (res < 2) return res; switch (q->balFactor) { case LH: q->balFactor= EH; return 2; case EH: q->balFactor= RH; return 1; case RH: return balanceRight(T); } } else { p->key = q->key; p = q; q = q->pRight; return 2; } } 5. NHN XT <+> Vi cy cn bng trung bnh 2 ln thm vo cy th cn mt ln cn bng li; 5 ln hy th cn mt ln cn bng li. <+> Vic hu 1 nt c th phi cn bng dy chuyn cc nt t gc cho n phn t b hu trong khi thm vo ch cn 1 ln cn bng cc b. <+> Mt cy cn bng khng bao gi cao hn 45% cy cn bng hon ton tng ng d s nt trn cy l bao nhiu.