You are on page 1of 7

CY CN BNG AVL

MC TIU
Hon tt bi thc hnh ny, sinh vin c th: Hiu c cc thao tc quay cy (quay tri, quay phi) hiu chnh cy thnh cy cn b ng. Ci t hon chnh cy cn bng AVL.

Thi gian thc hnh: 120 pht 360 pht Lu : Sinh vin phi thc hnh bi tp v Cy nh phn v Cy nh phn tm kim trc khi lm bi ny.

TM T T
Cy cn b ng AVL l cy nh phn tm kim (NPTK) m ti mi nh ca cy, cao ca cy con tri v cy con phi khc nhau khng qu 1.
V d 1: cy cn bng AVL V d 2: cy khng cn bng

Khi thm node mi vo cy AVL c th xy ra cc trng hp mt cn bng nh sau:


Mt cn bng phi-phi (R-R) Mt cn bng phi-tri (R-L)

Mt cn bng tri-tri (L-L)

Mt cn bng tri-phi (L-R)

Ti liu hng dn thc hnh mn Cu trc d liu v gii thut HCMUS 2010

Trang

X l mt cn b ng bng cch s dng cc php quay cy a. Quay tri

b. Quay phi

X l c th cho cc trng hp mt cn bng nh sau:


MT CN BNG PHI Mt cn bng phi-phi (R-R) Mt cn bng phi-tri (R-L) - Quay tri ti node b mt cn - Quay phi ti node con phi ca bng node b mt cn bng - Quay tri ti node b mt cn bng MT CN BNG TRI Mt cn bng tri-phi (L-R) Mt cn bng tri-tri (L-L) - Quay tri ti node con tri ca - Quay phi ti node b mt cn bng node b mt cn bng Quay phi ti node b mt cn bng

Ging vi cy NPTK, cc thao tc trn cy cn bng bao gm: Thm phn t vo cy Tm kim 1 phn t trn cy Duyt cy Xa 1 phn t trn cy

NI DUNG THC HNH


Trang

Sinh vin c k pht biu bi tp v thc hin theo hng dn:


Ti liu hng dn thc hnh mn Cu trc d liu v gii thut HCMUS 2010

C bn

T chc mt cy cn bng AVL trong mi node trn cy cha thng tin d liu nguyn. Ngi dng s nhp cc gi tr nguyn t bn phm. Vi mi gi tr nguyn c nhp vo, phi to cy AVL theo ng tnh cht ca n. Nu ngi dng nhp -1 qu trnh nhp d liu s kt thc. Sau , xut thng tin cc node trn cy. Khi chng trnh kt thc, tt c cc node trn cy b xa b khi b nh. Phn tch Cc node trn cy cn bng cng ging nh cc node trn cy NPTK. Tuy nhin, do mi ln thm node vo cy chng ta cn kim tra cao ca node va thm kim sot tnh cn bng ca cy nn cn b sung thm gi tr cho bit s cn bng ti node vo cu trc ca node. C th nh sau:

struct AVLNODE { int key; int bal; // thuc tnh cho bit gi tr cn bng // 0: cn bng, 1: lch tri, 2: lch phi NODE* pLeft; NODE* pRight; };

Cc thao tc cn ci t: xoay tri cy (RotateLeft), xoay phi cy (RotateRight), thm 1 node mi vo cy (InsertNode), duyt cy theo (Traverse), xa ton b node trn cy (RemoveAll) chng trnh mch lc v r rng hn, chng ta s ci t 2 hm x l cn bng khi cy lch tri v lch phi theo bng phn loi trang 2. Nh vy trong chng trnh s c thm 2 hm BalanceLeft v BalanceRight.

Chng trnh tham kho


#include <stdio.h> struct AVLNODE { int Key; int bal; // thuc tnh cho bit gi tr cn bng // 0: cn bng, 1: lch tri, 2: lch phi AVLNODE* pLeft; AVLNODE* pRight; }; AVLNODE* CreateNode(int Data) { AVLNODE* pNode; pNode = new AVLNODE; //Xin cp pht b nh ng to mt phn t (node) mi if (pNode == NULL){ return NULL; } pNode->Key = Data; pNode->pLeft = NULL; pNode->pRight = NULL; pNode->bal = 0; //Ghi ch: gii thch ngha ca thao tc ny return pNode; } void LeftRotate(AVLNODE* &P) Ti liu hng dn thc hnh mn Cu trc d liu v gii thut HCMUS 2010

Trang

{ AVLNODE *Q; Q = P->pRight; P->pRight = Q->pLeft; Q->pLeft = P; P = Q; } void RightRotate(AVLNODE* &P) { //Ghi ch: sinh vin t code cho hm ny } void LeftBalance(AVLNODE* &P) { switch(P->pLeft->bal){ case 1: //mt cn bng tri tri RightRotate(P); P->bal = 0; P->pRight->bal = 0; break; case 2: //Ghi ch: cho bit y l trng hp mt cn bng no? LeftRotate(P->pLeft); RightRotate(P); switch(P->bal){ case 0: P->pLeft->bal= 0; P->pRight->bal= 0; break; case 1: P->pLeft->bal= 0; P->pRight->bal= 2; break; case 2: P->pLeft->bal= 1; P->pRight->bal= 0; break; } P->bal = 0; break; } } void RightBalance(AVLNODE* &P) { switch(P->pRight->bal){ case 1: //Ghi ch: cho bit y l trng hp mt cn bng no? RightRotate(P->pRight); LeftRotate(P); switch(P->bal){ case 0: P->pLeft->bal= 0; P->pRight->bal= 0; break; case 1: P->pLeft->bal= 1; P->pRight->bal= 0; break; case 2: P->pLeft->bal= 0; P->pRight->bal= 2; break; } P->bal = 0; break; case 2: //Ghi ch: cho bit y l trng hp mt cn bng no? Ti liu hng dn thc hnh mn Cu trc d liu v gii thut HCMUS 2010

Trang

LeftRotate(P); P->bal = 0; P->pLeft->bal = 0; break; } } int InsertNode(AVLNODE* &tree, int x) { int res; if(tree==NULL){ //Ghi ch: cho bit ngha ca cu lnh ny tree = CreateNode(x); if(tree==NULL){ return -1; //thm ko thnh cng v thiu b nh } return 2;//thm thnh cng v lm tng chiu cao cy } else { if(tree->Key==x){ return 0; //kha ny tn ti trong cy } else if(tree->Key > x){ res = InsertNode(tree->pLeft,x); if(res < 2) { return res; } switch(tree->bal){ //Ghi ch: gii thch ngha ca cu lnh switch ny case 0: tree->bal = 1; return 2; case 1: LeftBalance(tree); return 1; case 2: tree->bal = 0; return 1; } } else{ res = InsertNode(tree->pRight,x); if(res<2){ return res; } switch(tree->bal){ case 0: tree->bal=2; return 2; case 1: tree->bal = 0; return 1; case 2: RightBalance(tree); return 1; } } } } void Traverse(AVLNODE* t) { if(t!=NULL) { Traverse(t->pLeft); printf("Khoa: %d, can bang: %d\n", t->Key,t->bal); Traverse(t->pRight); Ti liu hng dn thc hnh mn Cu trc d liu v gii thut HCMUS 2010

Trang

} } void RemoveAll(AVLNODE* &t) { if(t!=NULL){ RemoveAll(t->pLeft); RemoveAll(t->pRight); delete t; } } int _tmain(int argc, _TCHAR* argv[]) { AVLNODE *tree; //Ghi chu: Ti sao li phi thc hin php gn pha di? tree = NULL; int Data; do { printf("Nhap vao du lieu, -1 de ket thuc: "); scanf("%d", &Data); if (Data == -1) break; InsertNode(tree, Data); }while (Data != -1); printf("\nCay AVL vua tao: \n"); Traverse(tree); RemoveAll(tree); return 0; }

Yu cu 1. Bin d ch on chng trnh nu trn. 2. Cho bit kt qu in ra mn hnh khi ngi dng nhp vo cc d liu sau: -1 10 30 30 40 35 50 32 -10 20 -5 8 -1 -1

3. Nhn xt trnh t cc node c xut ra mn hnh? Gii thch ti sao li in ra c trnh t nh nhn xt? 4. Sinh vin hon tt hm RightRotate trong source code. Gi : RightRotate tng t hm LeftRotate. 5. Bin d ch li chng trnh sau khi hon thnh cu 3 v cho bit kt qu in ra mn hnh khi ngi dng nhp vo cc d liu sau: 50 20 30 10 -5 7 15 35 57 65 55 -1 6. V hnh cy AVL c to ra t phn nhp liu cu 5.

8. Sinh vin ci t li cc hm dng cho cy nh phn v cy NPTK p dng cho cy AVL.


Ti liu hng dn thc hnh mn Cu trc d liu v gii thut HCMUS 2010

Trang

7. Hy ghi ch cc thng tin bng cch tr li cc cu hi ng vi cc dng lnh c yu cu ghi ch (//Ghi ch) trong cc hm InsertNode, BalanceLeft, BalanceRight, _tmain.

p dng Nng cao


1. Sinh vin t ci t thm chc nng cho php ngi dng nhp vo kha x v kim tra xem kha x c nm trong cy AVL hay khng. Cho dy A nh sau: 1 3 5 7 9 12 15 17 21 23 25 27 a. To cy AVL t d y A. Cho bit s php so snh cn thc hin tm phn t 21 trn cy AVL va to. b. To cy nh phn tm kim t dy A dng li on code to cy ca b i thc hnh trc). Cho bit s php so snh cn thc hin tm phn t 21 trn cy nh phn tm kim va to. c. So snh 2 kt qu trn v rt ra nhn xt? 2. Ci t chng trnh c cc s nguyn t tp tin input.txt (khng bit trc s lng s nguyn trn tp tin) v to cy AVL t d liu c c 3. Ci t cy cn bng AVL trong mi node trn cy lu thng tin sinh vin. 4. T tm hiu v ci t chc nng xa mt node ra khi cy AVL.

BI TP THM
1. Vit chng trnh cho php to, tra cu v sa cha t in Anh-Vit (sinh vin lin h vi GVLT chp file t in Anh-Vit) 2. Ci t li cc bi tp thm ca cy NPTK b ng cch d ng cy AVL

Ti liu hng dn thc hnh mn Cu trc d liu v gii thut HCMUS 2010

Trang