 # 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:

## Presentation on theme: "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:"— Presentation transcript:

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

3 Example 1 The graph at left is a tree; it is connected, and contains no simple circuits The graph at right is not a tree because it is not connected

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:

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

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

7 Example 2 The tree at left can be converted to a rooted tree by choosing any vertex as root; two examples are shown below

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

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.

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

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

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

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?

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

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

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

17 Example 5 The 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

18 More tree properties There are at most m h 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 =

19 Section 9.1 Introduction to Trees

Download ppt "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:"

Similar presentations