Lecture 17Trees
CSCI – 1900 Mathematics for Computer ScienceFall 2014Bill Pine
CSCI 1900 Lecture 17 - 2
Lecture Introduction
• Reading– Rosen Sections 11.1, 11.2
• Review Graphs• Trees• Rooted Trees• Specialized Trees
CSCI 1900 Lecture 17 - 3
Review of Directed Graphs
• A Digraph, G=(V, A), consists of a finite set V of objects called vertices, and a finite set A of objects called arcs between the vertices
• Arcs have a direction• Example: V={1, 2, 3} A= { (1, 2), (3, 2) }
1
2
3
CSCI 1900 Lecture 17 - 4
Graphs
• A graph, G=(V, E), consists of a finite set V of objects called vertices, and a finite set E of objects called edges
• Edges have no direction• Example: V={1,2,3} E= { (1,2), (2,3) }
1
2
3
CSCI 1900 Lecture 17 - 5
Connected Graph
• A graph is connected if there is a path (sequence of edges) between any two pairs of vertices
Connected Disconnected
CSCI 1900 Lecture 17 - 6
Cycle
C3C4
• If you can start at a node and move along edges and come back to the same node the graph has a cycle
CSCI 1900 Lecture 17 - 7
Acyclic Graphs
A disconnected acyclic graph
Not an acyclic graph
• A graph is acyclic if it has no cycles
CSCI 1900 Lecture 17 - 8
Trees
• Let A be a set and let T be a relation on A. We say that T is a tree if there is a vertex v0 with the property that there exists a unique path in T from v0 to every other vertex in A, but no path from v0 to v0
-Kolman, Busby, and Ross, p. 271
Or• A tree is a connected acyclic graph
CSCI 1900 Lecture 17 - 9
Rooted Trees
• In computer science, we work primarily with rooted trees
• A rooted tree is a directed graph– Which has one vertex, v0 , called the root, with no
arcs coming into it• In-degree of v0 = 0
– Any other vertex, vi, in the tree has exactly one path to it from v0 • In-degree of vi = 1
– There may be any number of paths from any vertex
CSCI 1900 Lecture 17 - 10
Examples of Trees
• From this point forward, we will follow the computer science conventions– Tree means rooted tree– Edge means directed edge
• Using the definitions given, determine which of the following examples are trees and which are not
CSCI 1900 Lecture 17 - 11
Is this a tree?
CSCI 1900 Lecture 17 - 12
Is this a tree?
CSCI 1900 Lecture 17 - 13
Is this a tree?
CSCI 2910Client &
Server Side Prog
CSCI 2800Visual Prog
Adv Concepts
CSCI 1100Using InfoTechnology
CSCI 1710WWW Design
CSCI 1510Student in University
CSCI 1800Visual Prog I
CSCI 2150Computer
Organization
CSCI 2235Intro to Unix
CSCI 3220Intro to
Database
CSCI 3250Software
Engineering I
CSCI 3350Software
Engineering II
CSCI 4217Ethical Issues
CSCI 4800Senior
CapstoneTechnology
CSCI 4227Advanced Database
CSCI 3400Network
Fundamentals
CSCI 4417System
Administration
CSCI 1900 Lecture 17 - 14
Definitions
Level – all of the vertices located n-edges from v0 are said to be at level n
Level 0
Level 1
Level 2
Level 3
CSCI 1900 Lecture 17 - 15
More Definitions
• A vertex, v, is considered the parent of all of the vertices connected to it by edges leaving v
• A vertex, v, is considered the child of the vertex connected to the single edge entering v
• A vertex, v, is considered the sibling of all vertices at the same level with the same parent
CSCI 1900 Lecture 17 - 16
More Definitions
• A vertex vj is considered a descendant of a vertex vi if there is a path from vi to vj
• The height of a tree is the number of the largest level
• The vertices of a tree that have no children are called leaves
CSCI 1900 Lecture 17 - 17
Tree ExampleLevel 0
Level 1
Level 2
v2v1
v5
v3v4
v0
v6 v7v8
• v0 is the parent of v1 , v2 , v3 , and v4• v1 , v2 , v3 , and v4 are children of v0• v1 , v2 , v3 , and v4 are siblings• v5 , v6 , v2 , v7 , v8 , and v4 are leaves (no children)• Height of the tree is 2
CSCI 1900 Lecture 17 - 18
More Definitions
• If every vertex of a tree has at most n children, then the tree is considered an n-tree
• If every vertex of a tree with offspring has exactly n offspring, then the tree is considered a complete n-tree
• When n=2, the tree is a binary tree
CSCI 1900 Lecture 17 - 19
Examples
1. If the set A = {a, b, c, d, e} represents all of the vertices for a tree T, what is the maximum possible height of T? What is the minimum possible height of T?
2. If the set A = {a, b, c, d, e} represents all of the vertices for a tree T and T is a complete binary tree, what is the maximum height of T?
CSCI 1900 Lecture 17 - 20
Examples (cont)
3. If the height of a complete 4-tree is 3, how many leaves does this tree have?
CSCI 1900 Lecture 17 - 21
Key Concepts Summary
• Review Graphs• Trees• Rooted Trees• Specialized Trees• Reading for next time
– Kolman Section 7.2, 7.3