1 section 9.1 introduction to trees. 2 tree terminology tree: a connected, undirected graph that...

19
1 Section 9.1 Introduction to Trees

Upload: linette-higgins

Post on 18-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:

1

Section 9.1

Introduction to Trees

Page 2: 1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:

2

Tree terminology

• Tree: a connected, undirected graph that contains no simple circuits– must be a simple graph: no multiple edges or

loops– an undirected graph is a tree if and only if there

is a unique simple path between any 2 of its vertices

Page 3: 1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:

3

Example 1The graph at left is a tree; it isconnected, and contains nosimple circuits

The graph at right is not atree because it is not connected

Page 4: 1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:

4

Forest

• A forest is a graph containing no simple circuits, not necessarily connected

• Each connected component of a forest is a tree• For example, the second graph on the previous

slide is a forest consisting of the following trees:

Page 5: 1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:

5

Rooted tree

• A rooted tree is the directed graph that results from choosing a root vertex in a tree and directed each edge away from root– The parent of vertex x is the vertex y such that

there is a directed edge from y to x– If y is the parent of x, then x is a child of y– Vertices with the same parents are siblings

Page 6: 1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:

6

More rooted tree terminology

• Ancestors: all vertices in the path from root to a particular vertex; root is the ancestor of all vertices

• Descendants: all vertices that share an ancestor

• Leaf: vertex with no children• Internal vertex: non-leaf vertex; root is always

internal, unless it’s the only vertex in the tree

Page 7: 1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:

7

Example 2The tree at left can be converted to arooted tree by choosing any vertex asroot; two examples are shown below

Page 8: 1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:

8

M-ary tree

• A rooted tree in which every internal vertex has at most m children is called an m-ary tree– A full m-ary tree is an m-ary tree in which

every internal vertex has exactly m children– If m = 2, we call it a binary tree

Page 9: 1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:

9

Example 3

The tree at left is a full 5-ary tree

The tree at right is not a full m-ary tree; m can’t be 2, since some vertices have 3 children. If m is 3, the tree isn’t full.

Page 10: 1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:

10

Ordered rooted tree

• A rooted tree in which the children of each internal vertex are ordered is an ordered rooted tree– in an ordered binary tree, we refer to the first

child as the left child, and the second child as the right child

– the trees rooted at a vertex’s left and right child are the left and right subtrees of that vertex

Page 11: 1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:

11

Trees as Models

• Chemistry: graphs can represent molecules, with atoms as the vertices, and bonds between them as the edges

• Business: trees are often used to represent chain of command within organizations

• Computer Science: File systems are typically organized into directories containing files; the entire file system is represented as a tree with a root directory containing subdirectories, each of which may be a subtree

Page 12: 1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:

12

Properties of Trees

• A tree with n vertices has n-1 edges• A full m-ary tree with n vertices has:

– v = (n-1)/m internal vertices and– e = [n(m-1) + 1]/m leaves

• A full m-ary tree with v internal vertices has:– n = mv + 1 vertices and– e = v(m - 1) + 1 leaves

• A full m-ary tree with e leaves has– n = (me - 1) / (m - 1) vertices and– v = (e - 1) / (m - 1) internal vertices

Page 13: 1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:

13

Example 4: using tree properties to solve problems

• Suppose somebody starts a chain letter, asking each recipient to forward the message to four other people

• Some people send it on, others do not• How many people have seen the message,

including the original sender, if:– no one receives it more than once – the letter ends when 100 people have read it but didn’t

forward it

• How many people did forward the message?

Page 14: 1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:

14

Example 4 solution

• The chain letter can be represented as a 4-ary tree, with internal vertices representing people who forwarded the message, and leaves representing people who didn’t

• So we know that the number of leaves e = 100

Page 15: 1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:

15

Example 4 solution

• Using the formula from 3 slides back, the number of people who saw the letter = the number of vertices n = (me - 1) / (m - 1) or (4 * 100 - 1) / (4 - 1) = 133

• The number of internal vertices represents the number of people who forwarded the message; since we know there are 100 leaves out of 133 vertices, there are 33 internal vertices

Page 16: 1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:

16

More tree properties

• Level of a vertex: length of the unique path from root to that vertex - the level of root is 0

• Height of a rooted tree is the maximum of levels of all vertices - in other words, the length of the longest path from root to any vertex

• A rooted m-ary tree of height h is balanced if all leaves are at levels h or h-1

Page 17: 1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:

17

Example 5The tree at left is a balanced binary tree; its height is 3 and all leaves are at levels 2 and 3

The tree at right is an unbalanced binary tree; its height is 3 and there are leaves at levels 1, 2 and 3

Page 18: 1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:

18

More tree properties

There are at most mh leaves in any m-ary tree of height h

If an m-ary tree of height h has e leaves, then h

If the m-ary tree is full and balanced, then h =

emlog

emlog

Page 19: 1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:

19

Section 9.1

Introduction to Trees