chuong2. duyet_dequi

Upload: anonymous-3ihyqdc

Post on 17-Feb-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/23/2019 Chuong2. Duyet_Dequi

    1/39

    CHNG 2. DUYT V QUI

    NI DUNG:2.1. Thut ton duyt

    2.2. Thut ton sinh

    2.3. Thut ton qui

    2.4. Thut ton quay lui

    2.5. Kh qui

    2.6. CASE STUDY

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    2/39

    2.1. Thut ton duyt

    Phng php gii quyt bi ton (Problem):

    S dng cc cng c ton hc: S dng cc nh l, mnh , lp lun v suy logic ca trong ton hc tm ra nghim ca bi ton.

    u im:d dng my tnh ha cc bi ton c thut gii (MathLab).

    Nhc im: ch thc hin c trn lp cc bi ton c thut gii.

    Lp bi ton ny rt nh so vi lp cc bi ton thc t.

    S dng my tnh v cc cng c tnh ton:

    Gii c mi bi ton c thut gii bng my tnh.

    i vi mt s bi ton cha c thut gii, ta c th s dng my tnh xem xt tt c cc kh nng c th t a ra nghim ca bi ton.

    Mt thut ton duyt cn tha mn hai iu kin:

    Khng c lp li bt k kh nng no.

    Khng c b st bt k cu hnh no.

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    3/39

    V d 1. Cho hnh vung gm 25 hnh vung n v. Hy in cc s t 0 n 9 vomi hnh vung n v sao cho nhng iu kin sau c tha mn.

    c t tri sang phi theo hng ta nhn c 5 s nguyn t c 5 ch s;

    c t trn xung di theo ct ta nhn c 5 s nguyn t c 5 ch s;

    c theo hai ng cho chnh ta nhn c 2 s nguyn t c 5 ch s;

    Tng cc ch s trong mi s nguyn t u l S cho trc.

    V d hnh vung di y vi S = 11.

    3 5 1 1 1

    5 0 0 3 3

    1 0 3 4 3

    1 3 4 2 1

    1 3 3 1 3

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    4/39

    Thut gii duyt. Chia bi ton thnh hai bi toncon nh sau:Tm X ={ x[10001,...,99999] | x l nguyn t v tng

    cc ch s l S.Chin lc vt cn c thc hin nh sau: Ly xX t vo hng 1(H1): ta in c vung 1, 2, 3, 4, 5.

    Ly xX c s u tin trng vi s 1 t voct 1 (C1): ta in c vung 6, 7, 8, 9.Ly xX c s u tin trng vi s 9, s cuicng trng vi s 5 t vo ng cho chnh 2(D2): ta in c vung 10, 11, 12.Ly xX c s th nht v s th 4 trng vi s 6 v 12 t vo hng 2 (H2): ta in c

    vung 13, 14, 15.Ly xX c s th nht, th hai, th 4 trng vi s 2, 13, 10 t vo ct 2 (C2): ta in c vung 16, 17.

    Lm tng t nh vy ta in vo hng 5 s25.

    Cui cng ta ch cn kim tra D1X v C5 X?

    1 2 3 4 5

    6 13 14 12 15

    7 16 11 18 19

    8 10 20 22 239 17 21 24 25

    3 5 1 1 1

    5 0 0 3 3

    1 0 3 4 3

    1 3 4 2 1

    1 3 3 1 3

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    5/39

    2.2. Thut ton sinh (Generation)

    Thut ton sinh c dng gii lp cc bi ton tha mn hai iu kin:

    Xc nh c mt th ttrn tpcc cuhnh cn litk cabi ton. Bit

    c cuhnh utin, bit c cuhnh cuicng.T mt cu hnh cuicng, ta xy dng c thutton sinh ra cuhnhngngay sau n theo th t.

    Thut ton:

    Thut ton Generation:

    Bc1 (Khi to):;

    Bc 2 (Bc lp):

    while ()do

    ;

    ;

    endwhile;

    End .

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    6/39

    V d1. Cho s t nhin N (N100). Hy li k tt c cc cchchia s t nhin N thnh tng ca cc s t nhin nh hn N.Cc cch chia l hon v ca nhau ch c tnh l mt cch.

    V d vi N= 5 ta c 7 cch chia nh sau:5

    4 13 2

    3 1 12 2 12 1 1 11 1 1 1 1

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    7/39

    Thut ton:Input:

    S t nhin n.Output:

    Cc cch chia n thnh tng cc s nh hn n.Actions:Bc 1(Khi to):

    k =1; X[k] = n; OK = TRUE; //a ra cch chia u tin.Bc 2(Lp):

    while (OK) { //Lp n khi no cch chia khng phi l n s 1Result(); //a ra phng n hin ti.

    Next_Division(); //Sinh ra php chia k tip}

    EndAction.Procedure Next_Division() { int i = k, j, R, S,D;

    while (i > 0 && X[i]==1 ) i--; //Lp t bn phi khi no X[i] =1if (i>0 ) { X[i] = X[i] 1; D = k - i + 1;R = D / X[i]; S = D % X[i]; k= i;

    if (R>0) { for ( j = i +1; j0 ){ k = k +1; X[k] = S; }

    }else OK =0;

    }An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    8/39

    //Hy cho bit kt qu thc hin ?

    #include #include

    #define MAX 100#define TRUE 1#define FALSE 0int n, k, X[MAX], dem =0, OK =TRUE;void Init(void ){

    coutn;k = 1; X[k] = n;

    }void Result(void) {

    cout

  • 7/23/2019 Chuong2. Duyet_Dequi

    9/39

    2.3. Thut ton qui (Recursion)

    Phngphp nh ngha bng qui: Mt i tng c nh ngha trc tiphoc gin tip thng qua chnh n c gi l php nh ngha bng qui.

    Thutton qui : Thut ton gii bi ton P trc tip hoc gin tip thng qua biton P ging nh P c gi l thut ton qui. Mt hm c gi l qui nun c gi trc tip hoc gin tip n chnh n. Mt bi ton gii c bng qui nu n tha mn hai iu kin:

    Phn tch c: C th gii cbi ton P bngbi ton P ging nhP vchkhc P d liu uvo. Vic giibi ton P cng c thc hintheo

    cch phn tch ging nhP.iu kin dng: Dy cc bi ton P ging nh P l hu hn v s dng timt bi ton xc nh no .

    Thut ton qui tng qut c th c m t nh sau:

    Thut ton Recursion ( P ) {

    1. Nu P tha mn iu kin dng:;

    2. Nu P khng tha mn iu kin dng:

    Recursion(P).

    }

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    10/39

    V d: Tm tng ca n s t nhin bng phng php qui.

    Li gii. Gi Sn l tng ca n s t nhin. Khi :

    Bcphn tch: Sn = n + S(n-1), n>1.

    iu kin dng: s 1 = 1 nu n=1;T ta c li gii ca bi ton nh sau:

    int Tong (int i ) {

    if (i ==1 ) return(1); //iu kin dng

    else return(i + Tong(i-1)); //iu kinphn tch c

    }

    V d. Tm n!.

    Li gii. Gi Sn l n!. Khi :

    Bcphn tch: Sn = n*(n-1)! nu n>0;

    iu kin dng: s0=1 nu n=0.T ta c li gii ca bi ton nh sau:

    long Giaithua (int i ) {

    if (i ==0 ) return(1); //iu kin dng

    else return(i *Giaithua(i-1)); //iu kinphn tch c

    }An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    11/39

    V d: Tm c s chung ln nht ca a v b bng phng php qui.

    Li gii. Gi d =USCLN(a,b). Khi :

    Bcphn tch:

    d = USCLN(a-b, b) nu a>b. d = USCLN(a, b-a) nu a b) return(USCLN(a-b, b));

    else return(USCLN(a, b-a));

    }}

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    12/39

    V d.Hy cho bit kt qu thc hin chngtrnh di y?

    #include

    #include

    int convert(int X, int b){

    if (X == 0) return 0;

    else return (X % b + 10 * convert(X / b));

    }

    int main(){

    int X, b, bin;

    printf("Nhap so X: "); scanf("%d", &X);

    printf("Nhap so b: "); scanf("%d", &b);

    bin = convert(X, b);

    printf("Ket qua %d la %d.\n", X, bin);

    getch(); return 0;

    }

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    13/39

    Bi tp.Hy cho bit kt qu thc hin chngtrnh di y?

    #include

    #include

    #include

    void reverse(char str[], int index, int size){

    char temp = str[index];

    str[index] = str[size];

    str[index] = temp;

    if (index>= size) return;

    reverse(str, index + 1, size-1);

    }

    int main(){

    char X[] ="ABCDEF";int size = strlen(X);reverse(X, 0, size - 1);

    printf("Ket qua is: %s\n", X);

    getch(); return 0;

    }

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    14/39

    Kch ccy nhphn (size of a tree). Ta nh ngha kch c ca mt cy l s node cthc trn cy. Bi ton t ra l cho trc mt cy hy tm kch c ca cy. V d: cydi y c kch c l 13

    40

    30 60

    25 35 50 70

    20 28 65 903832

    int Size(struct node* T) {

    if (T==NULL)return 0;

    elsereturn(Size(T->left) + 1 + Size(T->right));

    }

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    15/39

    Xc nhhai cy nhphn gingnhau. Ta nh ngha hai cy nh phn ging nhaunu chng c cng chung node v mi node c sp t ging nhau trn cy. Cho haicy nh phn bt k, hy xc nh xem hai cy c ging nhau hay khng?

    Li gii. Thut ton gii quyt bi ton c thc hin nh sau:

    in identicalTrees(struct node* T1, struct node* T2) {

    if (T1==NULL && T2==NULL) //nu chai cy T1 v T2 u rng

    return 1; //r rng chng gingnhau

    if (T1!=NULL && T2!=NULL){ //nu chai cy khc rngreturn (T1->data == T2->data &&

    identicalTrees(T1->left, T2->left) &&

    identicalTrees(T1->right, T2->right) );

    }

    return 0; //mttrong hai cy khc rng}

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    16/39

    Tm cao cacy. cao ca cy c nh ngha l ng i di nht t node gcn node l. Cho mt cy nh phn bt k, hy xc cao ca cy?

    Li gii. Thut ton gii quyt bi ton c thc hin nh sau:

    int maxDepth(struct node * T) {

    if (T==NULL)

    return 0;

    else {

    int lDepth = maxDepth(T->left);//Tm cao ca cy con triint rDepth = maxDepth(T->right); //Tm cao ca cy con phi

    if (lDepth > rDepth)

    return(lDepth);

    else

    return(rDepth);

    }

    }

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    17/39

    Cy phn chiu(Mirror Tree). Cy phn chiu ca cy nh phn T l cy nh phnM(T), trong cy con bn tri ca cy T tr thnh cy con bn phi ca M(T) v cycon bn phi ca T tr thnh cy con tri ca M(T). Bi ton c t ra l hy dchchuyn cy nh phn T cho trc thnh cy nh phn phn chiu ca T l cy M(T).

    30

    25 35

    20 28 3832

    30

    35 25

    38 32 2028

    void mirror(struct node* node) {if (node==NULL) return; //Nu cy rng th khng phi lm gelse { //Nu cy khng rng

    struct node* temp; //S dng node trung gian tempmirror(node->left); //Gi n cy phn chiu bn trimirror(node->right);//Gi n cy phn chiu bn phitemp = node->left; node->left = node->right;node->right = temp; //Tro i hai node tri v phi

    }

    }An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    18/39

    n tt ccc node l trn cy:

    Input: Output:

    430

    25 35

    20 28 3832

    unsigned int getLeafCount(struct node* node) {if(node == NULL) //Nu cy rng

    return 0; //S node l l 0

    if(node->left == NULL && node->right==NULL) //Nu cy c mt nodereturn 1; //S node l l 1else //Nu cy c t nht mt cy con

    return getLeafCount(node->left)+ getLeafCount(node->right);}

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    19/39

    Kimtra mtcy thamn iu kinnode trung gian bng tnghai node con triv phihay khng? Node khng c node l tri hoc phi cxem c gi tr0.

    10

    8 2

    5 3 2

    int isSumProperty(struct node* node) {int left_data = 0, right_data = 0;if(node == NULL || (node->left == NULL && node->right == NULL)) return 1;else {

    if(node->left != NULL)left_data = node->left->data;

    if(node->right != NULL)right_data = node->right->data;

    if((node->data == left_data + right_data)&& isSumProperty(node->left) &&isSumProperty(node->right))

    return 1;else return 0;

    }

    }An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    20/39

    2.4. Thut ton quay lui (Back track)

    Gi s ta cn xc nh b X =(x1, x2,..,xn) tha mn mt s rng buc no . ngvi mi thnh phn xi ta c ni kh nng cn la chn. ng vi mi kh nngjnidnh cho thnh phn xi ta cn thc hin:

    Kim tra xem kh nng j c c chp thun cho thnh phn xihay khng?Nu kh nng j c chp thun th nu i l thnh phn cui cng (i=n) ta ghinhn nghim ca bi ton. Nu i cha phi cui cng ta xc nh thnh phnth i +1.

    Nu khng c kh nng j no c chp thun cho thnh phn xith ta quay

    li bc trc (i-1) th li cc kh nng khc.Thut ton Back-Track ( int i ) {

    For ( j =; j

  • 7/23/2019 Chuong2. Duyet_Dequi

    21/39

    V d . Bi ton N qun hu. Trn bn c kch c NN, hy t N qun hu miqun trn 1 hng sao cho tt c cc qun hu u khng n c ln nhau.

    Li gii. Gi X =(x1, x2,..,xn) l mt nghim ca bi ton. Khi , xi = j c hiu lqun hu hng th i t ct j. cc qun hu khc khng th n c, qun

    hu th i cn khng c ly trng vi bt k ct no, khng c cng ngcho xui, khng c cng trn ng cho ngc. Ta c n ct A = (a1,..an), cXuoi[2*n-1] ng cho xui, Nguoc[2*n-1] ng cho ngc.

    Nu xi =j th A[j] = True, Xuoi[i-j+n] = True, Nguoc[i + j -1] = True.

    1

    2

    3

    4

    5

    6

    7

    15 14 13 12 11 10 9 8

    1

    2

    3

    4

    5

    6

    7

    8 9 10 11 12 13 14 15

    ng cho xui: Xuoi [i j + n] ng cho ngc: Nguoc [i + j -1]

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    22/39

    Thut ton:

    void Try (int i){

    for(int j=1; j

  • 7/23/2019 Chuong2. Duyet_Dequi

    23/39

    2. Sudoku. Cho mt hnh vung gm li cp 99 hnh vung n v trong

    mt s hnh vung in cc con s t 1 n 9. Hy in cc s t 1

    n 9 vo cc hnh vung cn li sao cho mi hng, mi ct v mi li

    con kch c 33 cha ng ch mt trong cc s t 1 n 9.

    V d vi li di y s cho ta kt qu sau:

    3 1 6 5 7 8 4 9 2

    5 2 9 1 3 4 7 6 8

    4 8 7 6 2 9 5 3 1

    2 6 3 4 1 5 9 8 7

    9 7 4 8 6 3 1 2 5

    8 5 1 7 9 2 6 4 3

    1 3 8 9 4 7 2 5 6

    6 9 2 3 5 1 8 7 4

    7 4 5 2 8 6 3 1 9

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    24/39

    Thut ton:

    bool Sudoku( int grid[N][N]){ // N = 9

    int row, col; //hng, ct cn t

    if (!FindUnassignedLocation(grid, row, col)) //nu in c tt creturn true; // thnh cng!

    for (int num = 1; num

  • 7/23/2019 Chuong2. Duyet_Dequi

    25/39

  • 7/23/2019 Chuong2. Duyet_Dequi

    26/39

  • 7/23/2019 Chuong2. Duyet_Dequi

    27/39

    3. Dy con c tng bngK (Set Sum). Cho tp cc s gm n s khcnhau. Hy tm tt c cc tp con ca tp cc s cho sao cho tng cc

    phn t ng bng K. V d di y a ra cy quay lui ca bi tonvi n=4.

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    28/39

    4. Bi ton t mu th (Graph Coloring). Cho th v hngG= v s t nhin m. Hy tm cch t mu th vi nhiu nht m

    mu sao cho khng c hai nh k nhau no c t bi cng mt mu.V d di y chra mt cch t vi 3 mu.

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    29/39

    2.5.Kh qui

    Nguyn tc lptrnh: Khng nn lm dng gii thut qui trong khi xydng ng dng. Mi bi ton gii c bng qui u c th dch chuyn

    v cc cu trc lnh lp kt hp vi cc cu trc d liu ph hp.

    Nguyn tc kh qui: Da trn b xp chng ca chng trnh dch (stack). Da trn cc cu trc lnh lp biu din li thut ton.

    Bi tp1. Kh qui gii thut di y?

    int convert(int X, int b){

    if (X == 0) return 0;

    else return (X % b + 10 * convert(X / b));

    }

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    30/39

    int convert(int X, int b){

    if (X == 0) return 0;

    else return (X % b + 10 * convert(X / b));

    }

    int Convert(int X, int b){

    int S=0, i=0;

    while(X!=0) {

    int du = X%b; //ly phn dint k = (int) pow(10, i);

    du = du* k;

    S= S + du;

    X =X / b; i++;

    }

    return(S);

    }

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    31/39

    Bi tp 2. Kh qui php duyt cy theo th t trc (NLR).Algorithm NLR ( Tree *T) {

    if (T!= NULL ) {;

    NLR(T -> left); //duyt th t trcsang bn triNLR( T -> right); //duyt th t trcsang bnphi

    }}V d : NLR(T) = 10, 8, 5, 6, 12, 3, 9, 2, 1, 4, 7.

    10

    8 9

    5 3 2 7

    1 46 12

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    32/39

  • 7/23/2019 Chuong2. Duyet_Dequi

    33/39

    Kim nghim.

    Bc Trng thi stack Cc node thm

    1 10

    2 9, 8 10

    3 9, 3, 5 10, 8

    4 9, 3, 12, 6 10, 8, 5

    5 9, 3, 12 10, 8, 5, 66 9, 3 10, 8, 5, 6, 12

    7 9 10, 8, 5, 6, 12, 3

    8 7, 2 10, 8, 5, 6, 12, 3, 9

    9 7, 4, 1 10, 8, 5, 6, 12, 3, 9, 2

    10 7, 4 10, 8, 5, 6, 12, 3, 9, 2, 1

    11 7 10, 8, 5, 6, 12, 3, 9, 2, 1, 4

    12 10, 8, 5, 6, 12, 3, 9, 2, 1, 4, 7

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    34/39

    Bi tp 3. Kh qui php duyt cy theo th t gia (LNR).Thut ton LNR ( Tree *T) {

    if (T!= ) { / /nuT khc rngLNR(T -> left; //Duyttheo th t giasang bn tri

    ;LNR( T -> right); //Duyt th t giasang bnphi

    }}V d : LNR(T) =6, 5, 12, 8, 3, 10, 1, 2, 4, 9, 7.

    10

    8 9

    5 3 2 7

    1 46 12

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    35/39

    Thutton LNR davo stack:

    Thutton LNR (Tree *T) {Bc1 (Khi to): done = false; //trngthi duytban ul false

    Stack =; // toStack rngNode = T; //Node hin ti tr nnode gcBc2 ( Lp):

    while(! done) { //lp nkhi done l trueif (Node ) { / /nunode khc rng

    Push(Stack, Node); /lanode vo stack

    Node = Node -> left; //chuynNode sang bn tri}else { //nuNode rng

    if (Stack ) { / /nuStack khc rngNode = Pop(Stack); //lynode ra khistack;

    Node = Node ->right; //Node trsang bnphi}else //nuStack rng

    done = true; //ktthc duyt}

    }

    }An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    36/39

    Bc Trng thi stack Cc node thm

    1 10

    2 10, 8

    3 10, 8, 5

    4 10, 8, 5, 6

    5 10, 8, 5 6

    6 10, 8, 12 6, 5

    7 10, 8 6, 5, 12

    8 10,3 6, 5, 12, 8

    9 10 6, 5, 12, 8, 3

    10 9 6, 5, 12, 8, 3, 10

    11 9, 2 6, 5, 12, 8, 3, 10

    12 9, 2, 1 6, 5, 12, 8, 3, 10

    13 9, 2 6, 5, 12, 8, 3, 10, 1

    14 9, 4 6, 5, 12, 8, 3, 10, 1, 2

    15 9 6, 5, 12, 8, 3, 10, 1, 2, 4

    16 7 6, 5, 12, 8, 3, 10, 1, 2, 4, 9

    17 6, 5, 12, 8, 3, 10, 1, 2, 4, 9, 7An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    37/39

    Bi tp 4. Kh qui php duyt cy theo th t sau (LRN).Thut ton LRN ( Tree *T) {

    if (T!= NULL ) {LRN (T -> left;

    LRN ( T -> right);;

    }}V d : LNR(T) =6, 12, 5, 3, 8, 1, 4, 2, 7, 9, 10.

    10

    8 9

    5 3 2 7

    1 46 12

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    38/39

    Thutton LRN davo stack:

    Thutton LRN (Tree *T) {Bc1 (Khi to): if (T = ) return; //nu T l rng

    Stack =

    ; Node = T; //tostack rngv Node tr n gcT

    Bc2 ( Lp):do { //bt u lp

    while(Node) { / /lp nkhi node l rngif (Node -> right ) //nunodephikhc rng

    Push(Stack, Node->right); /lanodephivo stack

    Push(Stack, Node); /lanode vo stackNode = Node ->left; //node trsang bn tri}Node = Pop(Stack); //anode ra khistack;if (Node->right && Top(Stack) == Noot->right){

    Pop(stack); // loinode ny khistack

    Push(Stack, Node); // anode vo stackNode = Node->right; // Trsang nodephi

    }else { ; Node = NULL; }

    } while (Stack));}

    An evaluation version of novaPDFwas used to create this PDF file.

    Purchase a license to generate PDF files without this notice.

    http://www.novapdf.com/http://www.novapdf.com/
  • 7/23/2019 Chuong2. Duyet_Dequi

    39/39

    Bc Trng thi stack Cc node thm

    1 9, 10, 3, 8, 12, 5,6

    2 9, 10, 3, 8, 12, 5 6

    3 9, 10, 3, 8, 12, 5 6

    4 9, 10, 3, 8, 5, 12 6

    5 9, 10, 3, 8, 5 6, 12

    6 9, 10, 3, 8 6, 12, 5

    7 9, 10, 8, 3 6, 12, 5

    8 9, 10, 8 6, 12, 5, 3

    9 9, 10 6, 12, 5, 3, 8

    10 10, 9 6, 12, 5, 3, 8

    11 10, 9, 4, 2,1 6, 12, 5, 3, 8

    12 10, 9, 4, 2 6, 12, 5, 3, 8, 1

    13 10, 9, 2, 4 6, 12, 5, 3, 8, 1

    14 10, 9, 2 6, 12, 5, 3, 8, 1, 4

    15 10, 9 6, 12, 5, 3, 8, 1, 4, 2

    16 10, 9, 7 6, 12, 5, 3, 8, 1, 4, 2

    17 10, 9 6, 12, 5, 3, 8, 1, 4, 2, 7

    18 10 6, 12, 5, 3, 8, 1, 4, 2, 7, 9

    19 6, 12, 5, 3, 8, 1, 4, 2, 7, 9, 10