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

树和二叉树 PowerPoint PPT Presentation


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

树和二叉树. 本章中主要介绍下列内容: 树的逻辑定义和存储结构 二叉树的逻辑定义、存储结构 二叉树的基本操作算法 树和二叉树的转换 哈夫曼树及其应用. 退出. 1 树 2 二叉树 3 哈夫曼树及其应用. 1 树. 5.1.1 树的定义和基本运算 1. 定义

Download Presentation

树和二叉树

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


4078651


4078651

  • 1

  • 2

  • 3


4078651

1

  • 5.1.1

    • 1.

  • nn0n=0n>1mm>0T1T2...Tm


  • 4078651

    A

    A

    B C D

    • 5-1

    E F G H I J

    K L M

    (a)

    (b)

    (c)


    4078651

    • 0

    • 0

    • 12


    4078651

    • mm0


    4078651

    • 2.

  • 1 CreateTree (T)

  • 2T ClearTree(T)

  • 3 TreeEmpty(T)

  • 4i Child(T,node,i)

  • 5 Parent(T,node)

  • 6Traverse(T)


  • 4078651

    • 5.1.2

      • 1.


  • 4078651

    A

    • 5-3

    B

    C

    D

    E

    F

    G

    H

    I

    J


    4078651

    • #define MAX_TREE_NODE_SIZE 100

    • typedef struct {

    • TEntryType info;

    • int parent;

    • } ParentNode;

    • typedef struct {

    • ParentNode item[MAX_TREE_NODE_SIZE];

    • int n; //

    • }ParentTree;


    4078651

    • int Parent(ParentTree T,int node)

    • { if (node<0||node>=T.n) return -2;

    • else return T.item[node].parent;

    • }


    4078651

    • 2.


  • 4078651

    0 A 2 1 4 ^

    1 C ^

    2 B 3 5 ^

    3 E ^

    4 D 6 ^

    5 F ^

    6 G 7 8 9 ^

    7 H ^

    8 I ^

    9 J ^

    root

    • 5-4


    4078651

    • C

    • #define MAX_TREE_NODE_SIZE 10

    • typedef struct ChildNode{

    • int child; //

    • struct ChileNode *next; //

    • }CNode;

    • typedef struct{

    • TEntryType info; //

    • CNode *firstchild; //

    • }TNode;


    4078651

    • typedef struct {

    • TNode item[MAX_TREE_NODE_SIZE];

    • int n,root;

    • //nroot

    • }ChildTree;

    • parent


    4078651

    • i

    • int Child(ChildTree T, int node, int i)

    • {

    • if (node<0||node>=T.n) return -2

    • p=T.item[node].firstchild; j=1;

    • while (p&&j!=i) { p=p->next; j++;}

    • if (!p) return -2;

    • else return p->child;

    • }


    4078651

    • 3.

  • firstchildnextsiblingitem


    4078651

    T

    A ^

    B ^ C D ^

    ^ E ^ F ^ G ^

    ^ H ^ I ^ J ^

    • 5-5


    4078651

    • C

    • typedef struct CSNode{

    • EntryType item;

    • struct CSNode *firstchild,*nextsibling;

    • }CSNode,*CSTree;

    • void AllChild(CSTree T, CSTree p)

    • //p

    • {

    • q=p->fisrtchild;

    • while (q) {

    • printf(%c,q->item); q=q->nextsibling;

    • }

    • }


    4078651

    2

    • 5.2.1

      • 1.

  • 1

  • 2

  • nn0n=0n>0


  • 4078651

    A

    • 5-6

    B C

    D E F

    G H


    4078651

    • 5

    (d)

    (b)

    (c)

    (e)

    (a)

    5-7


    4078651

    • 2.

  • 1 CreateBTree ( BT)

  • 2BT ClearBTree(BT)

  • 3 BTreeEmpty(BT)

  • 4 LeftChild(BT,node)RightChild(BT,node)

  • 5 Parent(BT,node)

  • 6Traverse(BT)


  • 4078651

    • 2

  • 5

  • 1i2i-1i1

  • 1i=12i-1=21-1=20=1

  • j1j<ij2j-1j=i-1j2j-1=2i-22ii-122i-2*2=2i-1


  • 4078651

    • 2K2K-1K1

    • 11K20,21,22,23,...,2K-12n


    4078651

    • a1annqK


    4078651

    • 3 BT0n02n2n0=n2+1

    • 1n1nB

    • 2

      • n=n0+n1+n21

  • nBn=B+1


  • 4078651

    • 1122BB=n1+2n2

      • n=n1+2n2+1 2

  • 12n0=n2+1

  • K2K-1


  • 4078651

    1

    2 3

    • 5-8

    4 5 6 7

    8 9 10 11 12 13 14 15


    4078651

    • hn1~n


    4078651

    • 4 n log2n+1log2nlog2n

    • nK2

    • 2K-1-1<n2K-1

    • 1

    • 2K-1n<2K

    • 2

    • K-1log2n<K

    • log2n =K-1K= log2n+1


    4078651

    • 5 ni 1in

    • 1i=1i i/2

    • 22i>ni2i

    • 32i+1>ni2i+1

    • 23

    • i=1n323n<3n<223

    • 1ji j2j2j+1


    4078651

    i

    i i+1

    • 5-10

    2i 2i+1 2i+2 2i+3 i+1 2i 2i+1

    2i +2


    4078651

    • i+1iiii+1

    • i+1ii 2i2i+1i+12i+22i+32(i+1)2(i+1)+1i+12(i+1)2(i+1)+1

    • n2(i+1)+1>n2(i+1)=ni+12(i+1)>ni+1


    4078651

    • 23

    • 1

    • i2in2i2ii 2i/2=i2i+1n2i+12i+1i (2i+1)/2 =i1


    4078651

    • 5.2.3

    • 1.


  • 4078651

    1

    • 5-11

    2 3

    4 5 6 7

    8


    4078651

    • 1

    • void CreateBTree(QBTree *BT,EntryType item[ ],int n)

    • {

    • if (n>=MAX_TREE_NODE_SIZE) n=MAX_TREE_NODE_SIZE-1;

    • for (i=1; i<=n;i++)

    • BT->item[i]=item[i];

    • BT->n=n;

    • }


    4078651

    • 2

    • int LeftCHild(QBTree BT,int node)

    • {

    • if (2*node>BT.n) return 0;

    • else return 2*node;

    • }

    • RightChild(BT,node)


    4078651

    • 3

    • int Parent(QBTree BT,int node)

    • {

    • if (1<=node&&node<=BT.n) return i/2;

    • else return -1;

    • }


    4078651

    • 32

  • 5-12


    4078651

    • LchildRchilditemC

    • typedef struct BTNode{

    • EntryType item;

    • struct BTNode *Lchild,*Rchlid;

    • }BTNode,*BTree;


    4078651

    BT

    A

    A

    • 5-13

    B C

    B ^ C

    D E F

    ^ D ^ E ^ F ^

    G H

    ^ G ^ ^ H ^


    4078651

    5-14


    4078651

    • 5.2.4


  • 4078651

    • 1.

  • 6

  • TLR, TRL

  • LTR, RTL

  • LRT, RLT

  • TRLRTLRLTTLRLTRLRT


  • 4078651

    • 1

    • 2


    4078651

    • 3


    4078651

    A

    ABDGCEFH

    DGBAECHF

    GDBEHFCA

    B C

    D E F

    G H

    5-15


    4078651

    • 1


    4078651

    A

    B C

    D E F

    G H

    D G B A E C H F

    5-16


    4078651

    • 2

    A

    B C

    D E F

    G H

    5-17


    4078651

    • 12

    • 1

    • void PreOrder(BTree BT)

    • {

    • if (BT) { Visit(BT);

    • PreOrder(BT->Lchild);

    • PreOrder(BT->Rchild); }

    • }


    4078651

    • 2

    • void InOrder(BTree BT)

    • {

    • if (BT) {

    • InOrder(BT->Lchild);

    • Visit(BT);

    • InOrder(BT->Rchild);

    • }

    • }


    4078651

    • 3

    • void PostOrder(BTree BT)

    • {

    • if (BT) {

    • PostOrder(BT->Lchild);

    • PostOrder(BT->Rchild);

    • Visit(BT);

    • }

    • }


    4078651

    A

    B C

    D E F

    G H

    • 2.

  • ABCDEFGH

    5-19


    4078651

    A 1

    B 3 4 C

    D 5 6 E F 7 8 G

    A 1

    B 2 3 C

    D 4 E 6 7 F

    (a)

    (b)

    5-20


    4078651

    • void LevelOreder(QBTree BT)

    • {

    • for (i=1;i<=BT.n;i++)

    • if (BT.item[i]!=#) Visite(BT.item[i]);

    • }


    4078651


    4078651

    • 1

    • 2


    4078651

    • void LevelOrder(BTree *BT)

    • {

    • if (!BT) exit;

    • InitQueue(Q); p=BT; //

    • Visite(p); EnQueue(&Q,p);

    • //

    • while (!QueueEmpty(Q)) {

    • //

    • DeQueue(&Q,&p); //

    • if (!p->Lchild) {Visite(p->Lchild);EnQueue(&Q,p->Lchild); //

    • if (!p->Rchild) {Visite(p->Rchild);EnQueue(&Q,p->Rchild); //

    • }

    • }


    4078651

    • 5.2.5

    • 1.

  • ##


  • 4078651

    • 5-12

    • BTree Pre_Create_BT( )

    • {

    • getch(ch);

    • if (ch==#) return NULL; //

    • else { BT=(BTree)malloc(sizeof(BTNode));

    • //

    • BT->data=ch;

    • BT->lchild =Pre_Create_BT( ); //

    • BT->rchild =Pre_Create_BT( ); //

    • return BT;

    • }

    • }


    4078651

    • 2.

  • 1


  • 4078651

    • 5-13

    • void Leaf(BTree BT,int *count)

    • {

    • if (BT) {

    • Leaf(BT->child,&count);

    • //

    • if (BT->lchild==NULL&&BT->rchild==NULL) (*count)++;

    • Leaf(BT->rchild,&count);

    • //

    • }

    • }


    4078651

    • 53

  • void change_left_right(BTree BT)

  • {

  • if (BT) {

  • change_left_right(BT->lchild);

  • change_left_right(BT->rchild);

  • BT->lchild<->BT->rchild;

  • }

  • }


  • 4078651

    • 54

  • 1

  • int hight(BTree BT)

  • {//h1h2BT

  • if (BT==NULL) return 0;

  • else {

  • h1=hight(BT->lchild);

  • h2=hight(BT->right);

  • return max{h1,h2}+1;

  • }

  • }


  • 4078651

    • 5.2.6

      • 1.


  • 4078651


    4078651

    • 2.


  • 4078651

    3

    • 1


    4078651

    A

    • 5-26

    B C

    D E F

    G H


    4078651


    4078651

    5

    4

    8

    5-27


    4078651

    • 6{36910711}6

    10 11

    3 6 7 9

    (a)


    4078651

    9

    3

    • 5-28

    7

    6

    6

    7

    9

    3

    10

    10

    11

    11

    (c)

    (b)


    4078651

    • WPL1=10*2+11*2+3*3+6*3+7*3+9*3=117

    • WPL2=3*1+6*2+7*3+9*4+10*5+11*5=177

    • WPL3=9*1+7*2+6*3+3*4+10*5+11*5=158


    4078651

    • 1n{w1,w2,...,wn}nn{T1,T2,...,Tn}

    • 2

    • 3


    4078651

    • 423

    • {5,29,7,8,14,23,3,11}

    • 88

    5 29 7 8 14 23 3 11


    4078651

    • 35

    8

    29 7 8 14 23 11

    3 5


    4078651

    • 78

    8

    15

    29 14 23 11

    7 8

    3 5


    4078651

    29 14 23

    15

    19

    7 8

    8

    11

    3 5


    4078651

    • 811

    • 1415

    • 1923

    42

    29

    29

    19

    14

    15

    23

    7 8

    8

    11

    3 5


    4078651

    42

    58

    • 2929

    19

    23

    29

    29

    8

    11

    14

    15

    3 5

    7 8


    4078651

    100

    • 4258

    42

    58

    19

    29

    23

    29

    11

    8

    14

    15

    3 5

    7 8


    4078651

    • 8

      • WPL=(23+29)*2+(11+14)*3+(3+5+7+8)*4=271


    4078651

    • 5.3.2

  • if (socre<60) printf(bad);

  • else if (socre<70) printf(pass);

  • else if (score<80) printf(general);

  • else if (score<90) printf(good);

  • esle printf(very good);


  • 4078651


    4078651

    <60

    bad

    <70

    • 5-29

    pass

    <80

    general

    <90

    good

    very good


    4078651

    70...79

    general

    80...89

    good

    60...69

    pass

    ...<59

    bad

    very good

    5-30


    4078651

    <80

    • 5-31

    <70

    <90

    general

    good

    <60

    very good

    bad

    pass


    4078651

    • 5.3.3

    • 12

    • 1.

    • 4ABCD00011011ABACCDA0001001010110014


    4078651

    • 2.

    • ABCD0001010000110109404A1B2A2B


    4078651

    • 1

    • 201

    • 8{0.05,0.29,0.07,0.08,0.14,0.23,0.03,0.11}

    • 1100{5,29,7,8,14,23,3,11}

    • 25-27


    4078651

    • 35-28


    4078651

    • 0000011011010010


  • Login