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

第六章 树和二叉树 PowerPoint PPT Presentation


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

第六章 树和二叉树. 树的概念和基本术语 二叉树 二叉树遍历 二叉树的计数 树与森林 霍夫曼树. 树形结构是一类重要的非线性数据结构。其中以数和二叉树最为常用,直观看来,树是以分支关系定义的层次结构。 1. 客观世界中广泛存在,族谱,各种社会组织机构。 2. 计算机领域中广泛应用。 3. 数据库系统中的应用。. 树的概念和基本术语. 树的定义 树是由 n (n  0) 个结点的有限集合。如果 n = 0 ,称为空树;如果 n > 0 ,则 有且仅有一个特定的称之为根 (Root) 的结点,它只有直接后继,但没有直接前驱;

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


3795539


3795539

  • 1.

  • 2.

  • 3.


3795539

  • n (n 0) n = 0 n > 0

  • (Root)

  • n > 1 m (m >0) T1, T2 ,, Tm(SubTree)


3795539

A

B

C

D

E

F

G

H

I

J

K

L

M

A

13

A

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

T1,T2,T3A


3795539

1

A

2

B

C

D

height

= 4

3

E

F

G

H

I

J

4

K

L

M


3795539

L

R

L

R

(Binary Tree)

2


3795539

1 i 2i 1(i 1) []

i=12 i-1=2 0=1

ji>j1j2 j-1

i-1 2i 2

2ii-122* 2i 2= 2 i-1


3795539

20 + 21 + + 2 k-1 = 2 k-1

2 k 2 k-1(k 1)

1k


3795539

3T, n0, 2 n2,n0n21.

1 n1 n e

n = n0 + n1 + n2 e = 2n2 + n1 = n - 1

2n2 + n1 = n0 + n1 + n2- 1

n2 = n0- 1 n0 = n2 + 1


3795539

1

3

2

4

5

7

6

8

9

10

11

12

13

14

15

1 (Full Binary Tree)

k2 k-1


3795539

1

1

3

3

2

2

4

5

4

5

6

6

7

1

3

2

4

5

7

6

8

9

10

11

12

2 (Complete Binary Tree)

hh h (0 h-1) h


3795539

4 n (n 0) log2(n) 1

h2

2h1- 1 < n 2h- 1 2h1 n < 2h

h1 < log2n hh

h = log2(n) 1


3795539

  • 5n0, 1, 2, , n

  • i = 0, i i

    i > 0, i (i-1)/2

  • 2*i<= n, i 2*i+12*i+1< n, i 2*i+2

  • i log2(i+1) +1

0

1

2

3

4

5

6

8

9

7


3795539

1

1

2

3

2

3

4

5

6

7

4

5

6

8

9

7

8

9

10

10

9

1 2 3 4 5 6 7 8 910

1 2 3 4 0 5 6 7 8 0 0 910


3795539

lChild data rChild

parent

data

data

rChild

lChild

rChild

lChild

lChild data parent rChild


3795539

root

root

root

A

A

A

B

B

B

D

D

C

D

C

C

E

F

E

F

E

F


3795539

root

data parent leftChild rightChild

0

1

2

3

4

5

A-1 1 -1

B 0 2 3

C 1-1 -1

D 1 4 5

E 3 -1 -1

F 3 -1 -1

A

B

C

D

E

F


3795539

typedef char TreeData;//

typedef struct node {//

TreeData data;

struct node * leftChild, * rightchild;

} BinTreeNode;

typedef BinTreeNode * BinTree;

//


3795539

Voiddestroy ( BinTreeNode *current )

{//current

if ( current != NULL ) {

destroy ( current -> leftChild );

destroy ( current -> rightChild );

delete current;

}

}


3795539

BinTreeNode *Parent ( BinTreeNode * start, BinTreeNode * cuurent )

{//

if ( start == NULL ) return NULL;

if ( start->leftChild == current ||

start->rightChild == current ) return start; //

BinTreeNode *p; //

if (( p = Parent ( start->leftChild, current ))!= NULL ) return p; //

else return Parent(start->rightChild, current); //

}


3795539

void BinaryTree Traverse ( BinTreeNode *current, ostream &out )

{//current

if ( current != NULL ) {

out << current->data << ;

Traverse ( current->leftChild, out );

Traverse ( current->rightChild, out );

}

}


3795539

V

L

R

V

L

R

VLR

LVR

LRV


Inorder traversal

-

+

/

*

a

e

f

b

-

c

d

(Inorder Traversal)

  • (L)

    (V)

    (R)

    a + b * c - d - e / f


3795539

void InOrder ( BinTreeNode *T ) {

if ( T != NULL ) {

InOrder ( T->leftChild );

cout << T->data;

InOrder ( T->rightChild );

}

}


Preorder traversal

-

+

/

*

e

a

f

b

-

c

d

(Preorder Traversal)

  • (V)

    (L)

    (R)

    - + a * b - c d / e f


3795539

  • void PreOrder ( BinTreeNode *T ) {

  • if ( T != NULL ) {

  • cout << T->data;

  • PreOrder ( T->leftChild );

  • PreOrder ( T->rightChild );

  • }

  • }


Postorder traversal

-

+

/

*

a

e

f

b

-

c

d

(Postorder Traversal)

  • (L)

    (R)

    (V)

    a b c d - * + e f / -


3795539

  • void PostOrder ( BinTreeNode * T ) {

  • if ( T != NULL ) {

  • PostOrder ( T->leftChild );

  • PostOrder ( T->rightChild );

  • cout << T->data;

  • }

  • }


3795539

  • ()

  • , RefValue@-1


3795539

A B C @ @ D E @ G @ @ F @ @ @

A

@

B

C

D

E

F

@

@

@

@

@

G

@

@


3795539


3795539

void CreateBiTree(BiTree &T, char s[], int &i) {

i++;

if (s[i]==' '||s[i]=='#') T=NULL;

else {

T=new BiTNode; T->data=s[i]; //

CreateBiTree(T->lchild,s,i); //

CreateBiTree(T->rchild,s,i); //

}

}

void CreateBiTree(BiTree &T,char s[]) {

int i=-1;

CreateBiTree(T,s,i);

}


3795539

2.()

int Count ( BinTreeNode *T ) {

if ( T == NULL ) return 0;

else return 1 + Count ( T->leftChild )

+ Count ( T->rightChild );

}


3795539

3.

int Leaf_Count(Bitree T)

{//

if(!T) return 0; //

else if(!T->lchild&&!T->rchild) return 1; //else return Leaf_Count(T-lchild)+Leaf_Count(T-rchild); //}


3795539

4.()

int Height ( BinTreeNode * T )

{

if ( T == NULL ) return -1;

else{

int m = Height ( T->leftChild );

int n = Height ( T->rightChild ) );

return (m > n) ? m+1 : n+1;

}

}


3795539

5.()

int Copy( BiTree T )

{

if ( T == NULL ) return -1;

BiTree temp=new BiTreeNode;

Temp->data=T->data;

Temp-> leftChild = Copy( T->leftChild );

Temp-> rightChild = Copy(T->rightChild );

return temp;

}


3795539

6.()

bool Similar(BiTree T1,BiTree T2) { // :

if (!T1 && !T2 ) return true;

if (!T1 || !T2) return false;

return (Similar(T1->lchild,T2->lchild) && Similar(T1->rchild,T2->rchild));

}


3795539

a

d

a

c

b

a

a

e

d

c

()

b

a

b

d

d

a b

e

c

c

a

e

c e


3795539

a

c

b

e

d

void InOrder ( BinTree T ) {

stack S; InitStack( &S ); //

BinTreeNode *p = T;//

while ( p != NULL || !StackEmpty(&S) ) {

if( p != NULL )

{ Push(&S, p); p = p->leftChild; }

if ( !StackEmpty(&S) ) { //

Pop(&S, p); //

cout << p->data << endl; //

p = p->rightChild;

}//if

}//while

return ok;

}


3795539

()

a

a

d

c

c

b

c

c

e

d

a

c

b

b

d

d

d

c

c

e

e


3795539

:()

  • void Exchange(BiTree &T) { //T

  • BiTree P;

  • if (!T) return;

  • Exchange(T->lchild); Exchange(T->rchild);

  • P=T->lchild; T->lchild=T->rchild; T->rchild=P;

  • }


Threaded binary tree

(Threaded Binary Tree)


3795539

  • fwdbkwd


3795539

  • nn+1


3795539

  • (Threaded Binary Tree)

data

RightChild

LeftChild

LeftThread

RightThread

LeftThread=0, LeftChild

LeftThread=1, LeftChild

RightThread=0, RightChild

RightThread=1, RightChild


3795539

  • Typedef enum PointerTag {Link,Thread};//Link==0:Thread==1

  • Typedef struct BiThrNode{

  • TElemType data;

  • struct BiThrNode *lchild, *rchild; ////

  • pointerTag LTag,RTag;//

  • }BiTrhNode ,*BiTrhTree;


3795539

  • lchildrchild

  • lchildrchild


3795539

A

0 1 2 3 4 5 6

data

A B C D E F G

B

C

D

parent

-1 0 0 0 1 1 3

E

F

G


3795539

#define MaxSize //

typedef char TreeData; //

typedef struct { //

TreeData data;

int parent;

} TreeNode;

typedef TreeNode Tree[MaxSize]; //


3795539

  • -

A

A

B

C

D

B

C

D

E

F

G

E

F

G

n(k-1)+1

data

child1

child2

child3

childd


3795539

data

firstChild

nextSibling

A

A

B

E

C

B

C

D

D

F

E

F

G

G

-

n+1


3795539

-

typedef char TreeData;

typedef struct node {

TreeData data;

struct node *firstChild, *nextSibling;

} TreeNode;

typedef TreeNode * Tree;


3795539

F

H

A

B

C

D

G

I

J

E

K

F

H

A

G

B

I

C

K

J

D

E

T1 T2 T3

A

B

C

F

D

H

3

G

E

I

T1 T2 T3

K

J


3795539

A

B

C

D

E

F

G

:

A

B

E

C

F

D

G


3795539

A

B

  • ABEFCDG

  • ABEFCDG

E

C

F

D

G


3795539

:

A

B

  • EFBCGDA

  • EFBCGDA

E

C

F

D

G


Huffman tree

(Huffman Tree)

  • (Path Length)

  • PL

  • () EPL

  • () IPL

  • PL = EPL + IPL


3795539

1

1

2

3

2

3

4

5

6

7

4

5

6

7

8

8

EPL = 3*1+2*3 = 9

EPL = 1*1+2*1+3*1+4*1 = 10


3795539

  • (Weighted Path Length, WPL)

  • () wi li


3795539

2

7

4

5

2

4

5

7

5

7

2

4

WPL = 2*2+ WPL = 2*1+ WPL = 7*1+

4*2+5*2+ 4*2+5*3+ 5*2+2*3+

7*2 = 36 7*3 = 46 4*3 = 35

()


3795539

n{w1,w2,},nwi

(1) n {w0, w1, w2, , wn-1} n F = { T0, T1, T2, , Tn-1 } Ti wi ,


3795539

(2) , F

F ,

F

F


3795539

6

5

7

2

4

F : {7} {5} {2} {4}

F : {7} {5} {6}

5

2

4

7

{2} {4}

F : {7} {11}

F : {18}

11

18

7

5

7

6

11

6

2

4

5

{5} {6}

{7} {11}

2

4


3795539

Weight parent leftChild rightChild

7-1 -1 -1

5-1 -1 -1

2-1 -1 -1

4-1 -1 -1

-1 -1 -1

-1 -1 -1

-1 -1 -1

7

0

1

2

3

4

5

6

5

2

4


3795539

Weight parent leftChild rightChild

7-1 -1 -1

5-1 -1 -1

2-1 -1 -1

4-1 -1 -1

6-1 -1 -1

-1 -1 -1

-1 -1 -1

0

1

2

3

4

5

6

p1

4

p2

4

i

2

3

7

5

6

2

4


3795539

Weight parent leftChild rightChild

7-1 -1 -1

5-1 -1 -1

24-1 -1

44-1 -1

6-1 23

11-1 -1 -1

-1 -1 -1

7

0

1

2

3

4

5

6

11

p1

5

5

6

2

4

p2

5

i

1

4


3795539

Weight parent leftChild rightChild

p1

7-1 -1 -1

55-1 -1

24-1 -1

44-1 -1

6523

11-1 14

18-1 -1 -1

6

0

1

2

3

4

5

6

18

7

11

5

6

2

4

p2

6

i

0

5


3795539

const int n = 20;//

const int m = 2*n -1;//

typedef struct {

float weight;

int parent, leftChild, rightChild;

} HTNode;

typedef HTNode HuffmanTree[m];


3795539

void CreateHuffmanTree ( HuffmanTree T,

float fr[ ] ) {

for ( int i = 0; i < n; i++ )

T[i].weight = fr[i];

for ( i = 0; i < m; i++ ) {

T[i].parent = -1;

T[i].leftChild = -1;

T[i].rightChild = -1;

}

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


3795539

int min1 = min2 = MaxNum;

int pos1, pos2;

for ( int j = 0; j < i; j++ )

if ( T[j].parent == -1 )

if ( T[j].weight < min1 )

{ pos2 = pos1; min2 = min1;

pos1 = j; min1 = T[j].weight;

}

else if ( T[j].weight < min2 )

{ pos2 = j; min2 = T[j].weight; }

T[i].leftChild = pos1;

T[i].rightChild = pos2;


3795539

T[i].weight =

T[pos1].weight + T[pos2].weight;

T[pos1].parent = T[pos2].parent = i;

}

}


3795539

<

<60?

<

<70?

0.10

<

<80?

0.15

<

<90?

0.25

0.15

0.35

WPL = 0.10*1+0.15*2+0.25*3+0.35*4+0.15*4

= 3.15


3795539

<

<80?

<

<

<70?

<90?

<

<60?

0.15

0.25

0.35

0.10

0.15

WPL = 0.10*3+0.15*3+0.25*2+0.35*2+0.15*2

= 0.3+0.45+0.5+0.7+0.3 = 2.25


3795539

CAST CAST SAT AT A TASA

{ C, A, S, T }() W{ 2, 7, 4, 5 }

A : 00 T : 10 C : 01 S : 11

( 2+7+4+5 ) * 2 = 36.


3795539

1

0

0

1

0

1

7

2

5

4

A

C

S

T

{ 2/18, 7/18, 4/18, 5/18 }, { 2, 7, 4, 5 }, 0 1()


3795539

1

0

7

1

0

5

0

1

2

4

A : 0 T : 10 C : 110 S : 111

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

WPL


  • Login