Transcript
Page 1: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

More Trees

Discrete Structures (CS 173) University of Illinois 1

Magritte

Page 2: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

Rooted trees

2

root

Page 3: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

Terminology for rooted trees

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

Relations: parent/child/sibling, ancestor/descendant

3overhead

Page 4: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

Another example

4

64

7

5

2

3

1root

Page 5: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

Another example

5

6

4

7

5

2

3

1root

64

7

5

2

3

1root

Page 6: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

Decision trees

6

Page 7: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

Hierarchical data structure; HTML; XML

7

Page 8: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

Programs as trees

8

Page 9: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

Languages, parsing, parse trees

9

Page 10: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

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

Page 11: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

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

Page 12: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

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

Page 13: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

Induction on rooted trees

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

13

Page 14: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

14

Page 15: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

15

Page 16: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

16

Page 17: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

Induction proof on trees

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

17

Page 18: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

Induction proof on trees

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

18

Page 19: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

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 =

Page 20: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

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

Page 21: More Trees - courses.physics.illinois.edu · More Trees Discrete Structures (CS 173) University of Illinois 1 Magritte. Rooted trees 2 root. Terminology for rooted trees Nodes: root,

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


Top Related