21
Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

Embed Size (px)

Citation preview

Page 1: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

Lecture 17Trees

CSCI – 1900 Mathematics for Computer Science

Fall 2014

Bill Pine

Page 2: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

CSCI 1900 Lecture 17 - 2

Lecture Introduction

• Reading– Rosen Sections 11.1, 11.2

• Review Graphs• Trees• Rooted Trees• Specialized Trees

Page 3: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

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

Page 4: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

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

Page 5: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

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

Page 6: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

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

Page 7: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

CSCI 1900 Lecture 17 - 7

Acyclic Graphs

A disconnected acyclic graph

Not an acyclic graph

• A graph is acyclic if it has no cycles

Page 8: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

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

Page 9: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

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

Page 10: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

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

Page 11: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

CSCI 1900 Lecture 17 - 11

Is this a tree?

Page 12: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

CSCI 1900 Lecture 17 - 12

Is this a tree?

Page 13: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

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

Page 14: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

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

Page 15: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

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

Page 16: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

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

Page 17: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

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

Page 18: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

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

Page 19: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

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?

Page 20: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

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?

Page 21: Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

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