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

第五章 树 PowerPoint PPT Presentation


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

第五章 树. 树形结构的逻辑特征是:有且仅有一个开始结点,可有若干个终端结点,其余的内部结点都有且仅有一个前趋结点,可以有若干个后继结点,也就是说结构中的数据元素间存在着一对多的层次关系。 本章首先简单介绍树的基本概念,然后重点讨论二叉树的逻辑结构、存储结构及其运算,线索二叉树和线索二叉链表以及如何利用线索来实现遍历运算,并分析树、森林与二叉树之间的相互转换问题,最后介绍二叉树和树的典型应用。. 主要内容:. 5 . 1 树的定义 5 . 2 二叉树 二叉树的定义及性质 二叉树的存储 二叉树的遍历及实现算法 5 . 3 线索二叉树

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


5133217


5133217


5133217

  • 51

  • 52

  • 53

  • 54

  • 55


5133217

51

1

tree=DSDSD

rSr

1DRoot

2D

3

treer


5133217

tree=DSrS

D={A B C D E

F G H I}

r={<AB><AC><AD>

<BE><BF><DG><GH><GI>}

AA

ABCDBGDEFCHI


5133217


5133217

2

nn0T

1

2mm0T1T2TmSubtree


5133217

3


5133217

4

1

Root

Parent

Child

Leaf

Silbing


5133217

2

Degree

3Pathb1b2bjbibi+11ij-1b1 bj1b bjbbjAncestorbjbDescendant

4Ordered TreeUnordered Tree

5Forestmm0


5133217

52

1Binary Treenn0

n=0

n0

Left ChildRight Child


5133217

2:

3

1i2i-1i1

2k2k-1i1

3n02n2n0=n2+1


5133217

Full Binary Tree

Complete Binary Tree

a b c


5133217

4 n

5 n1ii

1ii>1i/2

2i2in2i

3i2i+1n2i+1

4iini+1ii1i-1


5133217

1

1

N1N+1bt0


5133217

2

#1


5133217

2

lchildrchild


5133217

C

typedef char DataType;

typedef struct Node

{DataType data;

struct Node *lchild,*rchild;

}BiTree;

BiTree *root; /**/

BiTreelchildrchildroot


5133217

3

1

2

3

42~3@


5133217

#define MAXSIZE 100

typedef struct/* */

{ BiTree * data[MAXSIZE];

int front,rear;

}SeQueue;

BiTree * createTree()

{ SeQueue Q,*q=&Q;/* */

BiTree *root,*s;

char ch;

root=NULL;/* */

q->front =1; q->rear = 0; /* */

ch=getchar() ; /* */

while(ch!='@') /* @ */

{s=NULL;

if(ch!='#') /*s=NULL*/

{ s=(BiTree *)malloc(sizeof(BiTree));

s->data=ch;

s->lchild=NULL;

s->rchild=NULL;

}

q->rear++;/* */

q->data[q->rear]=s; /* NULL */

if(root==NULL) root=s;/* */

else

{ if(q->data[q->front]!=NULL) /* */

{ if(q->rear%2==0) q->data[q->front]->lchild=s; /* */

else q->data[q->front]->rchild=s; /* */

}

if(q->rear%2==1) q->front ++; /* */

}

ch=getchar();/* */

}

return root;

}


5133217

Traversal

LDR6DLRLDRLRDDRLRDLRLD

DLRPreOrder

LDRInOrder

LRDPostOrder


5133217

1DLR

1

2

3

2LDR

1

2

3


5133217

3LRD

1

2

3


5133217

void PreOrder(BiTree * T) /* */

{ if (T) /* */

{ printf("%c",T->data); /* */

PreOrder(T->lchild); /* */

PreOrder(T->rchild); /* */

}

}


5133217

void InOrder (BiTree * T)/* */

{ if (T)

{ InOrder (T->lchild); /* */

printf("%c",T->data); /* */

InOrder (T->rchild); /* */

}

}

void PostOrder (BiTree * T)/* */

{if (T)

{ PostOrder (T->lchild); /* */

PostOrder (T->rchild); /* */

printf("%c",T->data); /* */

}

}


5133217


5133217


5133217

C

#define MAXSIZE 100

typedef BiTree* SElemType ;

typedef struct

{ SElemType data[MAXSIZE];/* */

int top;

}SeqStack;

void InOrder(BiTree *p)

{SeqStack *s;

s= initSeqStack();

while(1)

{while(p) { push(s,p); p=p->lchild;}

if (empty(s)) break;

p= top(s);pop(s); printf(%c, p->data); p=p->rchild;

}

}


5133217

53

Thread

01


5133217

C

typedef char DataType;

typedef struct Node

{DataType data;

struct Node *lchild,*rchild;

int ltag,rtag;

}BiThrTree;


5133217


5133217

0


5133217

prep

1*p1

2*p*prepre=NULL

1*pre1pre->rtag = =1pre- >rchild*ppre- >rchild=p

2*p1p->ltag= =1p- >lchild*prep- >lchild=pre

3pre*ppre=p


5133217

C

BiThrTree *pre=NULL; /**/

void inthreaded (BiThrTree * p) /* */

{if(p)

{inthreaded(p->lchild); /* */

if(p->lchild==NULL) p->ltag=1;/* *p */

if(p->rchild==NULL) p->rtag=1;

if(pre!=NULL) /* *p */

{

if(pre->rtag==1) pre->rchild=p;/* *pre */

if(p->ltag==1) p->lchild=pre; /* *p */

}

pre=p;/* */

inthreaded(p->rchild); /* */

}

}


5133217

*p

1*p1p->rtag==1p->lchild*p

2*p0p->rtag==0*p*p*qq->ltag= =1*q*p


5133217

C

void InOrderThread(BiThrTree *root)/* */

{BiThrTree *p;

p=root;

while(p->ltag==0) p=p->lchild; /* */

while(p)

{printf(%c,p->data); /* */

if(p->rtag==1) p=p->rchild; /* */

else

{p=p->rchild;

while(p->ltag==0) p=p->lchild;

}

}

}


5133217

*p

1*p

2*p

3*p*pq->rtag=1*qq->rchildq->rtag=0*(q->rchild*p


5133217

C

void PreOrderThread(BiThrTree *root)/* */

{BiThrTree *p;

p=root; /* */

while(p)/* */

{printf("%c",p->data); / * */

if(p->ltag==0) p=p->lchild; /* */

else

{while(p&&p->rtag==1) p=p->rchild;

if(p) p=p->rchild;

}

}

}


5133217

C

void PostOrderThread(BiThrTree *root)/* */

{BiThrTree *p;

p=root; /* */

while(p)/* */

{printf(%c,p->data);

if(p->rtag==0) p=p->rchild; /* */

else

{while(p&&p->ltag==1) p=p->lchild;

if(p) p=p->lchild;

}

}

}


5133217

54

ABFDCGEH

FBDGHECA


5133217

ABFDCGEHIJKLMNO

FBDGHEC

AJLKION


5133217

1

2


5133217

1

2

345


5133217

1

2


5133217

1


5133217

2.

nn


5133217

3.


5133217

55

1

Weighted Path Length of Node

Weighted Path Length of TreeWPL

nwiilii


5133217

WPLa=22+32+62+82=38

WPLb=81+62+23+33=35

WPLc=21+63+83+32=50

WPLd=81+23+33+62=35

Huffman Treenw1w2wnWPL


5133217

D.A.Huffman

1n{w1w2...wn}nn{T1T2...Tn}

2

32


5133217

1

202

3


5133217

5.1{529781423311}

188

2358

32


5133217

weightlchildrchildparent

N2N-12N-1tree10NULL

C

# define N 7 /**/

# define M 2*N-1/**/

typedef struct/**/

{float weight;/**/

int parent;/**/

int lchild,rchild;/*0*/

}HufmTree ;

HufmTree tree[M+1];/* 0*/


5133217

1tree2N-10

2Ntree[i]1iNN

3N-1N-1N-1tree[i]N+1i2N-1

1tree [j]1ji-1parent0p1p2tree

2tree[p1]tree[p2]tree[i]tree[i]tree[i].weighttree[p1].weighttree[p2].weighttree[i]p1tree[i]p2tree[p1]tree [p2]itree[i]


5133217

0101

01

ASC


5133217

1

201

301


5133217

5.28{abcdefgh}{529781423311}


5133217

C

typedef struct/**/

{char bits[N];/**/

int start; /*01 */

char ch; /* */

}CodeType ;

CodeType code[N+1]; /*0 */


5133217

huffmanCode(HufmTree tree[],CodeType code[])

/**/

{int i,c,p;

for(i=1;i<=N;i++)/*NN*/

{code[i].start=N;

c=i;

p=tree[i].parent;/**/

while (p!=0)/**/

{ code[i].start;

if(tree[p].lchild==c) code[i].bits[code[i].start] ='0';

else code[i].bits[code[i].start] ='1';

c=p;

p=tree[p].parent;

}

}

}


5133217

3

0


5133217

decode(HufmTree tree[],CodeType code[])

{/* 01 */

int i=m,b;/* tree[m]i=m */

int endflag=-1;/* */

int yiflag; /* */

scanf("%d",&b);/*01 */

while (b!=endflag)/*01 */

{yiflag=0;

if (b==0) i=tree[i].lchild;/*0 */

else i=tree[i].rchild;/*1 */

if(tree[i].lchild==0)/* */

{printf("%c",code[i].ch);

i=m;/* */

yiflag=1;

}

scanf("%d",&b);

}

if(yiflag!=1 ) printf( "\nERROR\n");/*01 */

}


  • Login