Loading...

PPT – Trees PowerPoint presentation | free to download - id: 6c0111-OWFlN

The Adobe Flash plugin is needed to view this content

Chapter 5

- Trees

Binary trees

- A binary tree is a finite set of elements that is

either empty or is partitioned into 3 disjoint

subsets

root left subtree right subtree

An example of binary tree (1)

- 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

level

0

A

1

C

B

2

F

E

D

3

G

H

I

An example of binary tree (2)

- left descendant of B D
- right descendant of B E, G
- brother B and C are brothers
- D and E are brothers
- leaf a node that has no sons e.g. D, G, H, I

right subtree of A

left subtree of A

B

C

D

E

F

G

H

I

Not binary trees

A

A

B

C

B

C

E

D

F

E

D

F

A

G

H

I

G

B

C

(a)

(b)

E

D

F

G

H

I

(c)

Strictly binary tree

- Each nonleaf node has nonempty left and right

subtrees.

A

C

B

E

D

F

G

Complete binary tree of depth 3

- of nodes in a complete binary tree of depth d

A

B

C

E

D

G

F

J

I

H

K

N

M

L

O

Implementation of a binary tree

- Each node has 3 fields.
- ??? doubly linked list, ???? link,
- ??? "father" ??? 4 ? field.

left son

information

right son

A

null C null

B

null E null

null D null

Linked array representation

- define NUMNODES 500
- struct nodetype
- int info
- int left
- int right
- int father
- struct nodetype nodeNUMNODES

Dynamic node representation

- struct nodetype
- int info
- struct nodetype left
- struct nodetype right
- struct nodetype father
- typedef struct nodetype NODEPTR

Creation of a new tree

- maketree(x) Create a new tree consisting of a
- single node
- NODEPTR maketree(int x)
- NODEPTR p
- p getnode()
- p-gtinfo x
- p-gtleft NULL
- p-gtright NULL
- return(p)
- / end maketree /

X

Creation of a new son

- 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-gtleft ! NULL)
- printf("invalid insertion\n")

p

p

x

An application of binary trees

- 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

Implementation with C

- 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)

- while (scanf("d", number) ! EOF)
- p q ptree
- while (number ! p-gtinfo q ! NULL)
- p q
- if (number lt p-gtinfo)
- q p-gtleft
- else
- q p-gtright
- / end while /
- if (number p-gtinfo)
- printf("d is a duplicate\n", number)
- else if (number lt p-gtinfo)
- setleft(p, number)
- else
- setright(p, number)
- / end while /
- / end main /

Traversals in a binary tree (1)

- (1) preorder (depth-first order)
- root
- left subtree
- right subtree
- (2) inorder (symmetric order)
- left subtree
- root
- right subtree
- (3) postorder
- left subtree
- right subtree
- root

Traversals in a binary tree (2)

- preorder
- inorder

- postorder

Preorder traversal

- void pretrav(NODEPTR tree)
- if (tree ! NULL)
- printf("d\n", tree-gtinfo) // visit the

root - pretrav(tree-gtleft) // traverse left

subtree - pretrav(tree-gtright)// traverse right

subtree - / end if /
- / end pretrav /

Inorder traversal

- void intrav(NODEPTR tree)
- if (tree ! NULL)
- intrav(tree-gtleft) // traverse left

subtree - printf("d\n", tree-gtinfo) // visit the

root - intrav(tree-gtright) // traverse right

subtree - / end if /
- / end intrav /

Postorder traversal

- void posttrav(NODEPTR tree)
- if (tree ! NULL)
- posttrav(tree-gtleft) //traverse left

subtree - posttrav(tree-gtright)//traverse right

subtree - printf("d\n", tree-gtinfo) // visit the

root - / end if /
- / end posttrav /

Binary search tree

- 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5

14

15

4

3

9

18

14

7

9

16

20

17

5

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

4

5

Expressions in binary trees

- prefix A B C
- ( preorder traversal )
- infix A B C
- ( inorder traversal )
- postfix A B C
- ( postorder traversal )

A

C

B

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

0

2

C

B

1

3

G

F

E

D

6

5

4

J

H

I

8

9

7

0 1 2 3 4 5 6 7 8 9

A B C D E F G H I J

Implicit array for an almost- complete binary

tree (2)

- 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

Extension to not almost complete binary trees

A

B

C

E

D

G

F

0 1 2 3 4 5 6 7 8 9 10 11 12

A B C D E F G

Nonrecursive inorder traversal

- define MAXSTACK 100
- void intrav2(NODEPTR tree)
- struct stack
- int top
- NODEPTR itemMAXSTACK
- 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-gtleft
- / end while /

- / check if finished /
- if (!empty(s))
- / at this point the left subtree is empty

/ - p pop(s)
- printf("d\n", p-gtinfo) / visit the root

/ - p p-gtright / traverse right subtree

/ - / end if /
- while (!empty(s) p ! NULL)
- / end intrav2 /
- The recursion stack cannot be eliminated.

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.

setleft(p, x)

A

C

B

F

E

D

setright(p, x)

I

H

G

Implementation of a right in-threaded binary tree

- 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 son
- struct nodetype right // pointer to right son
- int rthread // rthread is TRUE if
- // right is NULL or
- // a non-NULL thread
- typedef struct nodetype NODEPTR

Implementation with C (1)

- 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-gtleft
- / end while /

Implementation with C (2)

- if (q ! NULL)
- printf("d\n", q-gtinfo)
- p q-gtright
- while (q-gtrthread p ! NULL)
- printf("d\n", p-gtinfo)
- q p
- p p-gtright
- / end while /
- / end if /
- while (q ! NULL)
- / end intrav3 /

Heterogeneous binary trees

''

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

3

''

'/'

3

5

''

4

'-'

7

6

Evaluation of an expression represented by a

binary tree (1)

- define OPERATOR 0
- define OPERAND 1
- struct 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

Evaluation of an expression represented by a

binary tree(2)

- if (tree-gtutype OPERAND) / expression is a

single / - return (tree-gtnuminfo) / operand /
- / tree-gtutype OPERATOR /
- / evaluate the left subtree /
- opnd1 evalbintree(tree-gtleft)
- / evaluate the right subtree /
- opnd2 evalbintree(tree-gtright)
- symb tree-gtchinfo / extract the

operator / - / apply the operator and return the result /
- return(oper(symb, opnd1, opnd2))
- / end evalbintree /

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

The Huffman code (2)

- 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 (3)

- 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.

Huffman tree

Construction of a Huffman tree

symbol

E I A D C G B F H 25 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

frequency

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

The result of Huffman coding

decode

- 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 D

encode

Representing lists as binary trees (1)

- n of elements

array linked list binary tree

(balanced)

insertion or deletion (kth element)

O(1) (inserting an element following a

given element)

O(n-k)

O(log2n)

finding the kth element

O(1)

O(k)

O(log2n)

Representing lists as binary trees (2)

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

A

F

null

B

C

D

E

4

k3

Finding the kth element k3

1

2

k1

1

1

F

E

A

B

C

D

Representing lists as binary trees (3)

- A complete binary tree of depth d has 2d1-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.

Deleting elements in the list (1)

4

3

tree

1

1

2

2

F

E

F

E

1

1

1

D

X

A

B

C

D

A

B

X

p

(a)

(b)

Deleting elements in the list (2)

- Time O(log2n)

2

1

2

F

E

1

A

B

(d)

X

(c)

Trees

- root of the tree A
- B, 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

Fig.1

Ordered trees

- 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 B
- youngest son of A D

Fig.1

Fig.2

Representation of trees

oldest son

information

next brother

A

null

B

C

D

null

null

null

E

F

null

G

null

null

null

Ordered tree and binary tree

- An ordered tree can be represented as a binary

tree.

Binary tree

ordered tree

Forest

- an ordered set of ordered trees

Representing a forest by a binary tree

- corresponding binary tree

- 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

Preorder A B C D E Inorder A B C

D E PostorderA B C D E

A

B

E

C

D

(a)

why same?

A

Preorder A B C D E Inorder A B C D

E PostorderB A D C E

B

C

E

D

(b)

An example game trees

- tic-tac-toe ????
- evaluation function
- ?????????(??) ? ?????????(??)

X O

X

O

?? X ?? O

X O

X X

O

X O

X

X O

X O

X

X O

X O

X X

O

X O X

X

O

2 3-1

2 3-1

2

2

1 3-2

The minimax method

- look ahead