55
5 -1 Chapter 5 Trees

5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

Embed Size (px)

DESCRIPTION

5 -3 A G FE C D B HI level root: A node: A, B, C, …, H, I father of B: A sons of B: D, E left son of B: D right son of B: E depth: 3 ancestors of E: A, B descendants of B: D, E, G An example of binary tree (1)

Citation preview

Page 1: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -1

Chapter 5

Trees

Page 2: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -2

Binary trees A binary tree is a finite set of elements

that is either empty or is partitioned into 3 disjoint subsets:root

left subtreeright subtree

Page 3: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -3

A

G

FE

C

D

B

H I

level012

3

root: Anode: A, B, C, …, H, Ifather of B: Asons of B: D, Eleft son of B: Dright son of B: Edepth: 3ancestors of E: A, Bdescendants of B: D, E, G

An example of binary tree (1)

Page 4: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -4

left descendant of B: Dright descendant of B: E, Gbrother: B and C are brothers D and E are brothersleaf: a node that has no sons e.g. D, G, H, I

left subtree of A: right subtree of A:B

G

D E

H I

C

F

An example of binary tree (2)

Page 5: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -5

Not binary treesA

B C

ED F

G H I

(a)

A

B C

ED F

G

(b)

A

B C

ED F

G

H I(c)

Page 6: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -6

Strictly binary tree Each nonleaf node has nonempty left and

right subtrees.

A

B C

ED

F G

Page 7: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -7

Complete binary tree of depth 3

# of nodes in a complete binary tree of depth d:

**

A

JIH

ED

B

K NML

GF

C

O

Page 8: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -8

Each node has 3 fields.

若欲如 doubly linked list, 有雙向的 link, 則加入 "father" 而成為 4 個 field.

left son information right son

A

CB

D E

A

null

C null

B

null

E null

null

D null

Implementation of a binary tree

Page 9: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -9

#define NUMNODES 500 struct nodetype{ int info; int left; int right; int father; }; struct nodetype node[NUMNODES];

Linked array representation

Page 10: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -10

struct nodetype{ int info; struct nodetype *left; struct nodetype *right; struct nodetype *father; }; typedef struct nodetype

*NODEPTR;

Dynamic node representation

Page 11: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -11

maketree(x): Create a new tree consisting of a

single node

NODEPTR maketree(int x) { NODEPTR p;

p = getnode(); p->info = x; p->left = NULL; p->right = NULL; return(p); } /* end maketree */

X

Creation of a new tree

Page 12: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -12

setleft(p, x): create a new left son of node p

with information field x.

void setleft(NODEPTR p, int x){ if (p == NULL) printf("void insertion\n"); else if (p->left != NULL) printf("invalid insertion\n"); else p->left = maketree(x);} /* end setleft */

x

pp

Creation of a new son

Page 13: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -13

Finding all duplicate numbers in a number series. 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

build a binary search tree:smaller numbers stored in the left subtree.larger numbers stored in the right subtree.

Duplicate numbers: **

An application of binary trees

18

16

17

20

14

154

7

3 9

5

Page 14: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -14

struct nodetype{ int info; struct nodetype *left; struct nodetype *right; }; typedef struct nodetype *NODEPTR; main() { NODEPTR ptree; NODEPTR p, q; int number; scanf("%d", &number); ptree = maketree(number);

Implementation with C

Page 15: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -15

while (scanf("%d", &number) != EOF){

p = q = ptree; while (number != p->info && q != NULL){ p = q;

if (number < p->info) q = p->left; else q = p->right; } /* end while */ if (number == p->info) printf("%d is a duplicate\n", number); else if (number < p->info) setleft(p, number); else setright(p, number); } /* end while */ } /* end main */

14

154

7

9

Page 16: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -16

Traversals in a binary tree (1)

(1) preorder (depth-first order)1) root2) left subtree3) right subtree

(2) inorder (symmetric order)1) left subtree2) root3) right subtree

(3) postorder1) left subtree2) right subtree3) root

Page 17: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -17

preorder : inorder : ** postorder :

A

D

B C

FE

G IH

Traversals in a binary tree (2)

Page 18: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -18

void pretrav(NODEPTR tree){ if (tree != NULL){ printf("%d\n", tree->info); // visit the root

pretrav(tree->left); // traverse left subtree pretrav(tree->right);// traverse right subtree } /* end if */} /* end pretrav */

Preorder traversal

Page 19: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -19

void intrav(NODEPTR tree){ if (tree != NULL){ intrav(tree->left); // traverse left subtree printf("%d\n", tree->info); // visit the root

intrav(tree->right); // traverse right subtree

} /* end if */} /* end intrav */

Inorder traversal

Page 20: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -20

void posttrav(NODEPTR tree){ if (tree != NULL){ posttrav(tree->left); //traverse left subtree posttrav(tree->right);//traverse right subtree printf("%d\n", tree->info); // visit the root

} /* end if */} /* end posttrav */

Postorder traversal

Page 21: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -21

Binary search tree 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9,

14, 5 14

4

3 9

7 9

15

18

5

4 5

14

16 20

17

inorder traversal sorted: 3,4,4,5,5,7,9,9,14,14,15,16,17,18,20

Page 22: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -22

Expressions in binary trees

prefix: + A * B C( preorder traversal )

infix: A + B * C

( inorder traversal )

postfix: A B C * +( postorder traversal )

+

A *

CB

Page 23: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -23

Implicit array for an almost-complete binary tree (1)

(1) Each leaf is either at level d or at level d-1.(2) For any node n with a right descendant at level

d, all the left descendants of node n that are leaves are also at level d.

A

CB

GFE

J

D

H I

0

1 2

34 5

6

7 8 90 1 2 3 4 5 6 7 8 9A B C D E F G H I J

Page 24: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -24

An almost complete binary tree can be implemented by an array.

sons of node p : father of node p:

** not an almost complete binary tree:

A

B C

ED

GF

Implicit array for an almost-complete binary tree (2)

Page 25: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -25

A

B C

GF

ED

0 1 2 3 4 5 6 7 8 9 10

11

12

A B C D E F G

Extension to not almost complete binary trees

Page 26: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -26

Nonrecursive inorder traversal

#define MAXSTACK 100void intrav2(NODEPTR tree){ struct stack{ int top; NODEPTR item[MAXSTACK]; }s; NODEPTR p; s.top = -1; p = tree; do{ /*travel down left branches as far as possible */ /* saving pointers to nodes passed */ while (p != NULL){ push(s, p); p = p->left; } /* end while */

Page 27: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -27

/* check if finished */ if (!empty(s)){ /* at this point the left subtree is empty */ p = pop(s); printf("%d\n", p->info); /* visit the root */ p = p->right; /* traverse right subtree */ } /* end if */ } while (!empty(s) || p != NULL);} /* end intrav2 */

The recursion stack cannot be eliminated.

Page 28: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -28

Right in-threaded binary tree

A node with an empty right subtree points to its inorder successor. It can be traversed in inorder without a stack.

A

B C

FED

IHG

setleft(p, x):

setright(p, x):

**

**

Page 29: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -29

If the array implementation is used, positive pointer: normal right son negative pointer: inorder successor

dynamic implementation: struct nodetype{int info;struct nodetype *left; // pointer to left sonstruct nodetype *right; // pointer to right sonint rthread; // rthread is TRUE if // right is NULL or } // a non-NULL threadtypedef struct nodetype *NODEPTR;

Implementation of a right in-threaded binary tree

Page 30: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -30

void intrav3(NODEPTR tree) // No stack is used{ NODEPTR p, q; p = tree; do{ q = NULL while (p != NULL){

/* traverse left branch */ q = p; p = p->left; } /* end while */

Implementation with C (1)

Page 31: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -31

if (q != NULL){ printf("%d\n", q->info); p = q->right; while (q->rthread && p != NULL){ printf("%d\n", p->info); q = p; p = p->right; } /* end while */ } /* end if */ }while (q != NULL)} /* end intrav3 */

Implementation with C (2)

Page 32: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -32

Heterogeneous binary trees

The binary tree represents 3 + 4*(6-7)/5 + 3.

'+'

3

'/'

'+'

3

5'*'

4 '-'

76

Page 33: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -33

Evaluation of an expression represented by a binary tree

(1)#define OPERATOR 0#define OPERAND 1struct nodetype{ short int utype; /* OPERATOR or OPERAND */ union{ char chinfo; float numinfo; }info; struct nodetype *left; struct nodetype *right;};typedef struct nodetype *NODEPTR;

float evalbintree(NODEPTR tree){ float opnd1, opnd2; char symb;

Page 34: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -34

if (tree->utype == OPERAND) /* expression is a single */ return (tree->numinfo); /* operand */ /* tree->utype == OPERATOR */ /* evaluate the left subtree */ opnd1 = evalbintree(tree->left); /* evaluate the right subtree */ opnd2 = evalbintree(tree->right); symb = tree->chinfo; /* extract the operator */ /* apply the operator and return the result */ return(oper(symb, opnd1, opnd2));} /* end evalbintree */

Evaluation of an expression represented by a binary

tree(2)

Page 35: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -35

The Huffman code (1) Suppose we have a set of symbols: A, B, C, D

1) Each symbol is encoded by 3 bits (inefficient)

Message A B A C C D A would be encoded by 21 bits:

symbol code A 0 1 0 B 1 0 0 C 0 0 0 D 1 1 1

010 100 010 000 000 111 010

Page 36: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -36

2) Each symbol is encoded by 2 bits

Message A B A C C D A would be encoded by 14 bits:

symbol code A 00 B 01 C 10 D 11

00 01 00 10 10 11 00

The Huffman code (2)

Page 37: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -37

3) Huffman codes (variable-length codes) symbol code A 0 B 110 C 10 D 111

Message A B A C C D A would be encoded by 13 bits:

0 110 0 10 10 111 0 A frequently used symbol is encoded by a

short bit string.

The Huffman code (3)

Page 38: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -38

Huffman treeACBD,7

B,1

BD,2

CBD,4A,3

C,2

D,1

0 1

0 1

0 1

Page 39: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -39

Construction of a Huffman tree E I A D C G B F H25 15 15 12 7 6 6 4 1

25 15 15 12 7 6 6 5

25 15 15 12 11 7 6

25 15 15 13 12 11

25 23 15 15 13

28 25 23 15

38 28 25

53 38

91

symbolfrequency

Page 40: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -40

IHFBDEGCA,91

IHFBD, 38 EGCA, 53

B, 6HF, 5

D, 12HFB, 11

HFBD, 23I, 15

F, 4H, 1

E, 25 GCA, 28

G, 6

A, 15GC, 13

C, 7

Sym Freq Code Sym Freq Code Sym Freq Code

A 15 111 D 12 011 G 6 1100 B 6 0101 E 25 10 H 1 01000 C 7 1101 F 4 01001 I 15 00

Page 41: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -41

The result of Huffman coding

A Huffman tree is a strictly binary tree. The Huffman algorithm is an optimal encoding scheme.

111 01000 10 111 011 A H E A Dencodedecode

Page 42: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -42

Representing lists as binary trees (1)

n: # of elements

array linked list binary tree (balanced)

insertion ordeletion (kth element)

finding the kth element

O(n-k)

O(1) O(k) O(log2n)

O(log2n) O(1)(inserting an element followinga given element)

Page 43: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -43

nonleaf node: # of leaves in the left subtree leaf node: contents of a list element

A F nullB C D E

4

12

11

A B C D

FE

k=3

k=1Finding the kth element: k=3

Representing lists as binary trees (2)

Page 44: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -44

A complete binary tree of depth d has 2d+1-1 nodes or 2d leaf nodes.

If an almost complete binary tree is used to represent a list, the kth element can be found by accessing O(log2n) nodes.

Representing lists as binary trees (3)

Page 45: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -45

4

12

11

A B C D

FE

3

12

1

A B

D FE

(a) (b)

X

Xp

tree

Deleting elements in the list (1)

Page 46: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -46

Time: O(log2n)

2

12

1

A B

FE

1

1A

FE

(c) (d)X

Deleting elements in the list (2)

Page 47: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -47

Trees

root of the tree: AB, C, D are brothers.

degree of a node: # of sons degree of A : 3 degree of B : 2 degree of C : 0 degree of D : 1

A

G

DCB

FEFig.1

Page 48: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -48

ordered tree: the subtrees of each node form an ordered set

Fig.1 and Fig.2 are different ordered trees. In Fig.1,

oldest son of A : Byoungest son of A : D

A

G

BDC

F E

Fig.2

Ordered trees

A

G

DCB

FEFig.1

Page 49: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -49

Representation of treesoldest son information next brother

A null

B C D null

null E F null G nullnull

null

null

Page 50: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -50

Ordered tree and binary tree

An ordered tree can be represented as a binary tree.

A

B

CE

F D

G

oldest son left sonnext brother right son

ordered treeBinary tree

A

G

DCB

FE

Page 51: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -51

Forest an ordered set of ordered trees

H

GA

B C

FED

I

J K

M

L

Page 52: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -52

Representing a forest by a binary tree

corresponding binary tree

A

B

C

G

D

E

F

H I

J

M K

L

preorder traversal:A B C D E F G H I J M K L

inorder traversal:B D E F C A H G M J K L I

postorder traversal:F E D C B H M L K J I G A

Page 53: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -53

Preorder: + * A B + * C D EInorder: A * B + C * D + EPostorder:A B * C D * E + +

+

*

A +

*B

C E

D

+

*

A B

+

* E

C D

Preorder: + * A B + * C D EInorder: A B * C D * E + +Postorder:B A D C E * + * +

why same?(a)

(b)

Page 54: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -54

An example —— game trees

tic-tac-toe 井字遊戲

evaluation function: 我方成一直線的機會 ( 個數 ) 減 對方成一直線的機會 ( 個數 )

X OX

O

X OX X

O

X OX

X O

X OXX O

X OX X

O

X O XX

O

我方 : X對方 : O

2 =3-1 2 =3-1 2 2 1 =3-2

Page 55: 5 -1 Chapter 5 Trees. 5 -2 Binary trees A binary tree is a finite set of elements that is either empty or is partitioned into 3 disjoint subsets: root

5 -55

The minimax method look ahead

XX-

XO XO+

OX

OX

+

O

X

O

X+

O

X

O

X+

OXOX

+

-2

-1 0 -1 0 -2

min

O

X

O

X

XOXO

XOX

OO

XO

X

O

X

O

X

O

X

O

XOX OX

XXXX

1 0 1 0 -1 1 2

-1 1

1

+

++ + + + + +

-

min min

max