6
This presentation is the property of its rightful owner.
Sponsored Links
1 / 129

第6章 树和二叉树 PowerPoint PPT Presentation


  • 90 Views
  • Uploaded on
  • Presentation posted in: General

第6章 树和二叉树. 6.1 树的定义和基本术语 6.2 二叉树 6.3 二叉树的遍历 习题1 6.4 线索二叉树 6.5 树和森林 习题2 6.6 哈夫曼树及应用. 6.1 树的定义和基本术语. J. I. A. C. B. D. H. G. F. E. 一、树的概念 非线性数据结构。 树的递归定义: 树( tree) 是 n(n>=0) 个结点的有限集,当 n>0 时, (1)有且仅有一个特定的称为 根( root ) 的结点;

Download Presentation

第6章 树和二叉树

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


6

6

6.1

6.2

6.3 1

6.4

6.5 2

6.6


6

6.1

J

I

A

C

B

D

H

G

F

E

    • (tree)n(n>=0)n>0,

      • (1)(root);

      • (2)n>1,m(m>0)T1,T2,...,Tm,(subtree)


6

J

I

A

C

B

D

H

G

F

E

  • T={A, B, C, D, E, F, G, H, I, J}

    • A3

      • T1={B, E, F} , T2={C, G} , T3={D, H, I, J}

    • A

      • T1B2

      • T11={E}T12={F}T11,T12 B


6

J

I

A

C

B

D

H

G

F

E

    • 1

    • 3

    • 4

    • 5


6


6

  • 1

    • 1


6

  • 2


6

  • 2

    • 3.

      • DOSwindow

C:

1 n 1 2

11 12 11 12


6

  • 3

    • 4. A={ 123}

1

{ }

2

{1} { }

3

{1,2} {1} {2} { }

{1,2,3} {1,2} {1,3} {1} {2,3} {2} {3} { }

AA


6

  • 1

  • 2

  • 3a

  • 4b

  • 5c


6

J

I

A

C

B

D

H

G

F

E

L

K

M

( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) )


6

6.1

J

I

A

C

B

D

H

G

F

E

  • (Child)

  • (Parent)BAAB

  • (Sibling)


6

J

I

A

C

B

D

H

G

F

E

  • (Level)1

  • (Depth);

  • (Degree);

  • ;

  • (Leaf)0

  • 0


6

J

I

A

C

B

D

H

G

F

E

  • m m0

    Tree=(root, F),

    • root

    • F={T1, T2, Tm},

    • Ti={ri, Fi}

    • m>0

      RF=<root, ri>| i=1,2,m, m>0


6

  • 1InitTree(&T);

  • 2DestroyTree(&T);

  • 3CreateTree(&T, definition);

  • 4ClearTree(&T);

  • 5TreeEmpty(T);

  • 6TreeDepth(T);


6

  • 7 Root(T);

  • 8Value(T, cur_e);

  • 9 Assign(T, cur_e, value);

  • 10Parent(T, cur_e);

  • 11LeftChild(T, cur_e);

  • 12RightSibling(T, cur_e);

  • 13InsertChild(&T, &p, i, c);

  • 14DeleteChild(&T,&p, i);

  • 15TraverseTree(T, Visit( ));


6

  • 1

  • 2

  • 3


  • 6

    6.2

      • 6.2.1

      • 6.2.2

      • 6.2.3


    6 2 1

    6.2.1

    E

    D

    C

    A

    B

    G

    F

    • 2

      • 12

      • 2

        • ;

      • 3


    6

    E

    D

    E

    D

    C

    C

    B

    A

    A

    B

    G

    G

    F

    F

    (a)

    (b)

    (a)(b)

    (a)

    (b)


    6

    (b)

    (a)

    (e)

    (d)

    (c)


    6

    c

    e

    a

    d

    b

    f

    +

    /

    -

    -

    *

    • 1.

    • 2. (P129)

      • =()()()

    a+b*(c-d)-e/f


    6

    • 3.


    6 2 2

    6.2.2

    E

    D

    C

    A

    B

    G

    F

    • 1

      • i 2i-1(i1)

    • 2

      • k 2k-1 (k1)

    • 3

        • n0

        • 2n2

      • n0 = n2 +1


    6


    6

    1

    2

    3

    4

    5

    6

    7

    E

    B

    A

    D

    A

    C

    C

    B

    F

    G

      • k2k-1

    K=2

    K=3


    6

    1

    F

    E

    C

    A

    E

    D

    C

    B

    D

    A

    B

    F

    G

    2

    3

    4

    5

    6

      • n1n

    1

    2

    3

    4

    5

    6

    7


    6

    B

    A

    E

    C

    A

    D

    C

    B

    D

    A

    F

    C

    E

    E

    B

    D

    G

    G

    (a)

    (b)

    (c)

    • (b)(c)


    6

    1

    F

    A

    E

    D

    C

    B

    2

    3

    4

    5

    6

    • 4

      • nk=[log2n]+1

    • 5

      • i

        12i

        22i+1

        3[i/2]

        [ ]


    6 2 3

    6.2.3

    #define MAX_TREE_SIZE 100

    //

    typedef TElemType SqBiTree[MAX_TREE_SIZE];

    //0

    SqBiTree bt;


    6

    1

    F

    A

    E

    D

    B

    C

    2

    3

    4

    5

    6

    0 1 2 3 4 5 6

    A B C D E F


    6

    A

    A

    E

    E

    D

    B

    D

    C

    B

    C

    G

    G

    F

    F

    0 1 2 3 4 5 6 7 8 9

    A B C D E F G

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10


    6

    [1]

    [2]

    [3]

    [4]

    [5]

    [6]

    [7]

    [8]

    [9]

    .

    [16]

      • K2k-1

    A

    ^

    B

    ^

    ^

    ^

    C

    ^

    D

    E

    A

    B

    C

    D


    6

    E

    F

    D

    B

    A

    C

    A

    B

    C

    E

    D

    F

    • 1.

    T


    6

    lchild

    data

    rchild

    typedef struct BiTNode {

    TElemType data;

    struct BiTNode *lchild,*rchild;

    }BiTNode,*BiTree;


    6

    lchild

    data

    parent

    rchild

    • 2.

    typedef struct TriTNode {

    TElemType data;

    struct TriTNode *lchild,*rchild,*parent;

    }TriTNode,*TriTree;


    6

    6.3


    6

    D

    E

    C

    G

    B

    A

    F


    6

    D

    E

    C

    G

    B

    A

    F

    • L

      D

      R

    • DLRLDRLRD

      DRLRDLRLD

    • ,

      DLRLDRLRD

      ()()()


    6

    D

    E

    C

    G

    B

    A

    F

    • 1.DLR

      1

      2

      3

      1A

      2DLR

      3DLR

    A,B,D,E,G,C,F


    6

    D

    E

    C

    G

    B

    A

    F

    • 2.LDR

      1

      2

      3

      1LDR

      2A

      3LDR

    D,B,G,E,A,C,F


    6

    D

    E

    C

    G

    B

    A

    F

    • 3.LRD

      1

      2

      3

      1LRD

      2LRD

      3A

    D,G,E,B,F,C,A


    6

    c

    a

    e

    d

    f

    b

    +

    -

    *

    /

    -

    • a+b*(c-d)-e/f


    6

    c

    a

    e

    d

    f

    b

    +

    -

    *

    /

    -

      • a + b * c d e / f

      • - + a * b c d / e f

      • a b c d - * + e f /-


    6

    c

    a

    e

    d

    f

    b

    +

    -

    *

    /

    -

      • () - + a * b c d / e f

      • ()a b c d - * + e f /-


    6

    .

    • DLR

      1

      2

      3


    6

        • 1

        • 2

        • 3


    6

        • visit( )


    6

    T

    A

    B

    C

    E

    D

    F

    1.

    void PreOrderTraverse(BiTree T, Status (*Visit)(TElemType e)) {//T

    //Visit( ) if (T) {// //

    Visit(T->data)

    PreOrderTraverse(T->lchild, Visit);

    PreOrderTraverse(T->rchild, Visit); }

    }//PreOrderTraverse


    6

    • Visit

      Status PrintElement(TElemType e)

      { //e

      printf(e);

      return OK;

      }

    • PreOrderTraverse(T PrintElement)


    6

    2.

    void InOrderTraverse(BiTree T, Status(*Visit)(TElemType e))

    { //T

    //Visit( )

    if (T) { ////InOrderTraverse(T->lchild, Visit);

    Visit(T->data) InOrderTraverse(T->rchild, Visit);

    }

    }// InOrderTraverse

    : O(n) //

    : O(n)//


    6

    3.

    void PostOrderTraverse(BiTree T, Status(* Visit)(TElemType e))

    { //T//Visit( ) if (T) {//// PostOrderTraverse(T->lchild, Visit); PostOrderTraverse(T->rchild, Visit);

    Visit(T->data)

    }

    }//PostOrderTraverse


    6

    • 4.

      • 1.

      • 2. 1

      • 3.

        • 1


    6

    • void InOrderTraverse(BiTree T, Status(*Visit)(TElemType e))

      InitStack(S);Push(S, T); // T

      while (!StackEmpty(S))

      { while (Gettop(S, p)) && p ) Push(S, p->lchild); //

      Pop (S, p); //

      if (!StackEmpty(S)) //

      { Pop(S, p);

      Visit( p->data);

      Push(S, p->rchild);

      }//if

      }//while

      }//InOrderTraverse


    6

    • void InOrderTraverse(BiTree T, Status(*Visit)(TElemType e))

      InitStack(S);p=T; // T

      while ( p || !StackEmpty(S))

      { if (p) { Push(S, p); p=p->lchild } ; // else {//

      Pop(S, p);

      Visit( p->data);

      p=p->rchild;

      }//else

      }//while

      }//InOrderTraverse


    6

    • 1.

    • 2.

    • 3.


    6

    • 1.

      • n

          • n=1

          • n=


    6

    T

    A

    B

    C

    E

    D

    F

    void leaf(BiTree T) {

    //

    //n

    //

    // n=0

    if(T) { // if(T->lchild==null && T->rchild==null) n++;

    leaf(T->lchild);

    leaf(T->rchild);

    }//if

    }//leaf


    6

    • 2.

      • *


    6

    E

    E

    B

    D

    F

    D

    B

    F

    A

    A

    C

    C

    *

    *

    *

    *

    *

    *

    *

    A B D F C E

    *

    A B D * F * * * C E * * *


    6

    Status CreateBiTree(BiTree &T) {

    //

    scanf (&ch);

    if (ch= = * )T=NULL; // ch= = * T=NULL

    else { //ch! = *

    if (! (T=(BiTNode * )malloc(sizeof(BiTNode))))

    exit(OVERFLOW);

    T->data = ch; //

    CreateBiTree(T->lchild); //

    CreateBiTree(T->rchild); //

    }

    return OK;

    }//CreateBiTree


    6

    A

    A

    A

    B

    F

    B

    F

    G

    F

    G

    CBED

    G

    C

    D

    C

    D

    E

    E

    • 3

      • A B C D E F G

      • C B E D A F G


    6

    • A B C D E F G

      C B D A E G F

    A

    B

    E

    ^

    C

    D

    F

    ^

    ^

    ^

    ^

    ^

    G

    ^

    ^


    6

    void CrtBT(BiTree& T, char pre[], char in[], int ps, int is, int n )

    { // pre[ps..ps+n-1]

    // in[is..is+n-1]

    if (n==0) T=NULL;

    else {

    k=Search(in, pre[ps]); //

    if (k== -1) T=NULL;

    else { }

    } //else

    } // CrtBT


    6

    if (!(T= new BiTNode)) exit(OVERFLOW);

    T->data = pre[ps];//

    if (k==is) T->Lchild = NULL; //

    else //

    CrtBT(T->Lchild, pre[], ino[], ps+1, is, k-is );

    if (k=is+n-1) T->Rchild = NULL; //

    else //

    CrtBT(T->Rchild, pre[], ino[], ps+1+(k-is), k+1, n-(k-is)-1 );


    6


    6

    • 6.1

    • 6.15


    6

    6.4

      • 1

      • 2


    6


    6

    G

    C

    E

    A

    H

    D

    B

    F

    1.

    D,B,H,E,A,F,C,G


    6

    2.

      • 1

      • 2 nn+1


    6

    typedef enum{link,thread}PointerTag; //link=0, thread=1

    typedef struct BiThrNode{

    TElemType data;

    Struct BiThrNode *lchild, *rchild; //

    PointerTag LTag, RTag; //

    } BiThrNode*BiThrTree

    lchild LTag data RTag lchild

    • Ltag, Rtag

      • 0

      • 1


    6

    G

    1

    0

    1

    0

    0

    0

    1

    0

    C

    D

    H

    E

    B

    F

    G

    A

    0

    0

    0

    1

    1

    1

    0

    1

    C

    E

    D

    H

    A

    B

    F

    D,B,H,E,A,F,C,G


    6

      • Rtag = 1rchild

      • Rtag = 0

      • Ltag = 1lchild

      • Ltag = 0


    6

      • lchild

      • rchild:

      • lchild;

      • rchild


    6

    0

    0

    1

    1

    0

    1

    0

    1

    0

    E

    H

    F

    A

    G

    B

    C

    D

    1

    0

    1

    1

    1

    0

    0

    1

    1

    T

    D,B,H,E,A,F,C,G

    1

    2


    6

    3

    • pp=T->lchild

      • p

      • p pp

      • pp=p->rchild p


    6

    Void InOrderTraverse_Thr(BiThrTree T, Status (* Visit) (TE1emType e)) {

    // Tlchild

    // TVisit

    p=T->lchild; //p

    while(p!=T) { //

    while(p->Ltag= =Link) p= p->lchild;//

    Visit(p->data) //

    while (p->Rtag= =Thread && p->rchild!=T)

    {// p

    p=p->rchild; Visit(p->data); //

    }

    p=p->rchild; //

    }

    return OK;

    }//InOrderTraverse_Thr


    6

    • void InThreading(BiThrTree p){

      // prep

      if (p){InThreading(p->lchild); //if (!p->lchild){

      p->LTag=Thread; p->lchild=pre;} //if (!pre->rchild){

      pre->RTag=Thread; pre->rchild=p;} //pre=p; //prepInThreading(p->rchild); //}}//InThreading


    6

    Status InOrderThreading(BiThrTree &Thrt, BiThrTree T)

    { // Thrtif (!(Thrt=(BiThrTree)malloc(sizeof(BiThrNode))))

    exit (OVERFLOW); Thrt->LTag=Link; Thrt->RTag=Thread; //Thrt->rchild=Thrt; //if (!T)Thrt->lchild=Thrt; //else{ Thrt->lchild=T; pre=Thrt; InThreading(T); //pre->rchild=Thrt;

    pre->RTag=Thread; //Thrt->rchild=pre; } return OK;}//InOrderThreading


    6

    6.5

    6.5.1

    6.5.2


    6 5 1

    6.5.1

    • ;


    6

    1.


    6

    data parent

    0

    1

    2

    3

    4

    5

    6

    7

    8

    A -1

    B 0

    C 0

    D 0

    E 1

    F 1

    G 2

    H 3

    I 3

    F

    G

    D

    B

    H

    A

    I

    E

    C

    9

    0

    #define MAX_TREEE_SIZE 100

    typedef struct PTNode{

    TelemType data;

    int parent; //

    }PTNode;

    typedef struct{

    PTNode nodes[MAX_TREE_SIZE];

    int rn; //

    }Ptree;

    T.nodes

    T.r

    T.n


    6

    2.


  • 6

    • 1


    6

    • 2

    typedef struct CTNode{ //

    int child;

    struct CTNode * next;

    }* ChildPtr;

    typedef struct{

    TElemType data;

    ChildPtr firstchild; //

    }CTBox;

    typedef struct{

    CTBox nodes[MAX_TREE_SIZE];

    Int n, r;

    }CTree;//


    6

    data firstchild

    T.nodes

    3

    7

    6

    4

    2

    1

    5

    8

    G

    I

    A

    C

    E

    D

    H

    F

    B

    T.n

    T.r

    9

    0

    0 1 2 3 4 5 6 7 8 99


    6

    3.

      • firstchild

      • nextsibling

    typedef struct CSNode{

    TElemType data;

    struct CSNode *firstchild, *nextsibling;

    }CSNode, *CSTree;


    6

    I

    F

    G

    E

    D

    B

    C

    A

    E

    H

    F

    I

    A

    B

    D

    H

    G

    C

    A

    I

    D

    G

    H

    C

    B

    E

    F

    (a)

    (b)


    6

    X

    X

    X

    X


    6

    I

    A

    C

    B

    D

    H

    G

    F

    E

    F

    I

    A

    B

    D

    H

    G

    C

    E

    X

    X

    X

    X


    6

    F

    E

    F

    I

    A

    C

    B

    D

    G

    H

    C

    I

    A

    B

    D

    H

    G

    E

    • 1

      • AB E F C G D H I

    • 2

      • E F BG C H I D A


    6

    I

    A

    C

    B

    D

    H

    G

    F

    E

    • 3

      • ABCDEFGHI


    6 5 2

    6.5.2

    D

    B

    A

    I

    H

    F

    E

    C

    G

    N

    M

    L

    J

    K

    P

    O


    6

    T1 T2 T3

    E

    G

    A

    A

    F

    H

    I

    B

    C

    D

    B

    C

    J

    E

    D

    3

    G

    F

    T1 T2 T3

    H

    E

    G

    A

    I

    F

    H

    B

    J

    C

    I

    D

    J


    6

    • F = ( T1, T2, , Tn );

      T1 = (roott11, t12, , t1m);

    • B =( LB, Node(root), RB );

  • :

    • F = B = ;

      • ROOT( T1 ) Node(root);

      • (t11, t12, , t1m ) LB;

      • (T2, T3,, Tn ) RB.


  • 6

    • F = ( T1, T2, , Tn );

      T1 = (roott11, t12, , t1m);

    • B =( LB, Node(root), RB );

  • :

    • B = , F = ;

      • Node(root) ROOT( T1 );

      • LB ( t11, t12, t1m);

      • RB (T2, T3, , Tn)


  • 6

    • 1

      • ;

      • ;

      • ()


    6

    • 2

      • ;

      • ;

      • ()


    6

    A

    T1 T2 T3

    E

    G

    A

    B

    C

    F

    H

    I

    B

    C

    D

    E

    D

    G

    F

    J

    H

    3

    I

    ABCDEFGHIJ

    BCDAFEHJIG

    J


    6

    • 6.20(2)

    • 6.67


    6

    6.7

    • 6.7.1

    • 6.7.1


    6 7 1

    6.7.1

    N

    M

    L

    J

    K

    P

    O


    6

    • WPL= wi li

    • n{w1 , w2 , , wn}n wi

      WPL


    6

    2

    7

    c

    a

    4

    5

    2

    7

    4

    5

    a

    b

    c

    d

    b

    d

    4

    5

    7

    2

    a

    b

    c

    d

    • a,b,c,d7524

    (a)WPL=7*2+5*2+2*2+4*2 = 36

    (b)WPL=7*3+5*3+2*1+4*2 = 46

    (c)WPL=7*1+5*2+2*3+4*3 = 35


    6

    <60

    Y

    Y

    N

    5

    e

    <70

    Y

    N

    15

    d

    <80

    Y

    N

    40

    c

    <90

    N

    Y

    30

    10

    b

    a

    • if


  • 6

    <60

    Y

    Y

    N

    5

    e

    <70

    Y

    N

    15

    d

    <80

    Y

    N

    40

    c

    <90

    0-59 60-69 70-79 80-89 90-100

    0.05 0.15 0.40 0.30 0.10

    N

    Y

    30

    10

    b

    a

    • 100005

    • 10000

      = 100000.051+0.152+0.43+

      0.3 4+0.1 4

      =31500


    6

    • 0.05 1+0.15 2 +0.4 3+0.3 4+0.1 4


    6

    • 1n n nFn

    • 2F=

    • 3FF

    • 4 23F


    6

    100

    40

    30

    15

    10

    30

    40

    40

    30

    15

    15

    10

    10

    15

    30

    10

    30

    40

    10

    40

    15

    60

    15

    30

    60

    15

    30

    15

    30

    15

    5

    5

    5

    5

    5

    W=515403010


    6

    <80

    Y

    N

    <70

    <90

    N

    N

    Y

    Y

    100

    40

    30

    10

    <60

    b

    a

    c

    15

    30

    40

    10

    Y

    Y

    N

    5

    15

    e

    d

    0-59 60-69 70-79 80-89 90-100

    0.05 0.15 0.40 0.30 0.10

    30

    60

    15

    5

    • 10000

      WPL= 0.05 4+0.15 3+0.4 1+0.3 2+0.1 4 =2.05

      =2.05*10000*2=20500*2

    WPL=5*3+15*3+40*2+30*2+10*2=220


    6 7 2

    6.7.2


    6

    • . ABACCDA

      • ABCD

        A00B01C10D11

      • ABACCDA 00010010101100

      • 00010010101100 ABACCDA


    6

        • A0B00C1D01

        • ABACCDA 000011010


    6


    6

    100

    0

    1

    0

    0

    1

    1

    8

    29

    14

    11

    23

    0

    1

    0

    15

    29

    42

    58

    19

    1

    0

    1

    0

    1

    8

    5

    3

    7

    • . 8,

      • w=(529781423311)

    a: 0110

    b: 10

    c: 1110

    d: 1111

    e: 110

    f: 00

    g: 0111

    h: 010


    6

    • n


    6

    weight parent lchild rchild

    • WnHT

    typedef struct {

    unsigned int weight;

    unsigned int parent, lchild, rchild;

    }HTNode, *HuffmanTree;

    HTNode


    6

    0

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    100

    8

    23

    11

    14

    29

    29

    42

    15

    19

    58

    7

    3

    5

    8

    w p lch rch

    5 9 0 0

    29 14 0 0

    7 10 0 0

    8 10 0 0

    14 12 0 0

    23 13 0 0

    3 9 0 0

    11 11 0 0

    8 11 1 7

    15 12 3 4

    19 13 8 9

    29 14 5 10

    42 15 6 11

    58 15 2 12

    100 0 13 14

    w,p,lch,rch weight,parent,lchild,rchild


    6

    void HuffmanTree(HuffmanTree &HT, int * w, int n){

    //w n>0HT

    if (n<=1) return;

    m=2* n-1;

    HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));

    //

    for (p=HT, i=1; i<=n; ++i, ++p, ++w)

    * p={ * w, 0, 0, 0};

    //n

    //n


    6

    for (; i<m; ++i ++p) * p={ 0, 0, 0, 0};

    for (i=n+1; i<=m; ++i){

    //234

    Select(HT, i-1, s1, s2);

    //HT[1..i-1] parent0weight

    //s1s2

    HT[s1]. parent =i; HT[s2].parent=i;

    HT[i].lchild=s1; HT[i].rchild=s2;

    HT[i].weight=HT[s1].weight+HT[s2].weight;

    //HT[i]

    }


    W 5 29 7 8 14 23 3 11

    w=(5,29,7,8,14,23,3,11)

    0

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    W

    HT

    0

    1

    2

    3

    4

    5

    6

    7

    8

    5

    29

    7

    8

    14

    23

    3

    11

    w p lch rch

    5 9 0 0

    29 14 0 0

    7 10 0 0

    8 10 0 0

    14 12 0 0

    23 13 0 0

    3 9 0 0

    11 11 0 0

    8 11 1 7

    15 12 3 4

    19 13 8 9

    29 14 5 10

    42 15 6 11

    58 15 2 12

    100 0 13 14

    W

    HT


    6

    w p lch rch

    0

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    HT


    6

    w p lch rch

    0

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    HT

    • 5 0 0 0

    • 29 0 0 0

    • 7 0 0 0

    • 8 0 0 0

    • 14 0 0 0

    • 23 0 0 0

    • 3 0 0 0

    • 0 0 0

    11

    23

    14

    29

    5

    3

    7

    8

    FOR

    8


    6

    0

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    HT

    100

    8

    29

    14

    23

    11

    15

    42

    58

    19

    29

    3

    5

    7

    8

    5 9 0 0

    29 14 0 0

    7 10 0 0

    8 10 0 0

    14 12 0 0

    23 13 0 0

    3 9 0 0

    11 11 0 0

    8 11 1 7

    15 12 3 4

    19 13 8 9

    29 14 5 10

    42 15 6 11

    58 15 2 12

    100 0 13 14

    HT


    6

    HC

    100

    0 1 1 0

    1

    2

    3

    4

    5

    6

    7

    8

    1 0

    1 1 1 0

    1 1 1 1

    1 1 0

    8

    11

    14

    29

    23

    0 0

    15

    19

    58

    29

    42

    0 1 1 1

    0 1 0

    8

    7

    3

    5

    //

    HC=(HffmanCode)malloc((n+1*sizeof(char *)); //n

    cd=(char *)malloc(n*sizeof(char));

    //

    cd[n-1]="/0"; //


    6

    for(i=1;i<=n;++i){//

    start=n-1;//

    //

    for(c=i, f=HT[i].parent; f!=0; c=f, f=Ht[f].parent)

    if(HT[f].lchild==c)

    cd[--start]="0";

    else cd[--start]="1";

    //i

    HC[i]=(char *)malloc((n-start)*sizeof(char));

    strcpy(HC[i], &cd[start]); //cd()HC

    }

    free(cd); //

    }//HuffanCoding


    6

    1

    3

    4

    5

    6


  • Login