More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of...

Post on 26-Jun-2020

2 views 0 download

Transcript of More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of...

More Trees

Discrete Structures (CS 173) University of Illinois 1

Magritte

Rooted trees

2

root

Terminology for rooted trees

Nodes: root, internal, leaf, level, tree height

Relations: parent/child/sibling, ancestor/descendant

3overhead

Another example

4

64

7

5

2

3

1root

Another example

5

6

4

7

5

2

3

1root

64

7

5

2

3

1root

Decision trees

6

Hierarchical data structure; HTML; XML

7

Programs as trees

8

Languages, parsing, parse trees

9

More terminology

m-ary tree: each node can have at most m children

full: each node has either 0 children or 𝑚 children

complete: all leaves have the same height

balanced: all leaves are approximately the same height

10

Tree terminology

Nodes: root, internal, leaf, level, tree height

Relations: parent/child/sibling, ancestor/descendant

11

root

leaves

internalsubtree

level = 0

level = 1

level = 2

level = 3

More terminology for directed trees

m-ary tree: each node has at most m children

full: each node splits 0 or 𝑚 times

complete: all leaves have the same height

12

Induction on rooted trees

A full m-ary tree with i internal nodes has mi + 1 nodes

13

14

15

16

Induction proof on trees

Claim: In a binary tree of height ℎ, the number of nodes 𝑛 ≤ 2ℎ+1 − 1.

17

Induction proof on trees

Claim: There are at most 𝑚ℎ leaves in an m-ary tree of height h

18

Useful formulas

𝑘=0

𝑛

𝑟𝑘 =1 − 𝑟𝑛+1

1 − 𝑟

𝑘=𝑚

𝑛

𝑟𝑘 =𝑟𝑚 − 𝑟𝑛+1

1 − 𝑟

𝑘=𝑖

𝑛

𝑘 =𝑛(𝑛 + 1)

2

19

𝑘=0

𝑛

2𝑘 =

𝑘=1

𝑛

2𝑘 =

𝑘=0

𝑛

2−2𝑘 =

𝑘=0

𝑛

2𝑘+2 =

𝑚𝑎 𝑏 = 𝑚𝑎𝑏2log2 𝑛 = 𝑛

log𝑎(𝑏) = log2(𝑏)/ log2 𝑎

𝑚𝑎+𝑏 = 𝑚𝑎𝑚𝑏

2log4(𝑛)+2 =

Tree induction proof

If 𝑇 is a binary tree with root 𝑟, then its rank is

(a) 0 if 𝑟 has no children

(b) 1 + 𝑞 if 𝑟 has two children, both with rank 𝑞

(c) otherwise, the maximum rank of any of the children

20

Tree induction proof

If 𝑇 is a binary tree with root 𝑟, then its rank is

(a) 0 if 𝑟 has no children

(b) 1 + 𝑞 if 𝑟 has two children, both with rank 𝑞

(c) otherwise, the maximum rank of any of the children

Claim: A tree with rank 𝑞 has at least 2𝑞 leaves.

TRY TO DO THIS AS AN EXERCISE.

21